Untitled
unknown
plain_text
a year ago
1.5 kB
8
Indexable
#include <iostream>
using namespace std;
int t, n;
const int MN = 5;
int a[MN][MN];
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
int dfs(int x, int y, int color) {
if (!(0 <= x && x < n && 0 <= y && y < n) || a[x][y] != color || a[x][y] == -1) {
return 0;
}
int cnt = 1;
a[x][y] = -1;
for (int i = 0; i < 4; ++i) {
cnt += dfs(x + dx[i], y + dy[i], color);
}
return cnt;
}
int main()
{
cin >> t;
for (int test_case = 1; test_case <= t; ++test_case) {
cin >> n;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
a[i][j] = 0;
}
}
int color = 1;
for (int i = 0; i < n - 1; ++i) {
for (int j = 0; j < n; ++j) {
int x, y;
cin >> x >> y;
x--;
y--;
a[x][y] = color;
}
color++;
}
bool ans = true;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (a[i][j] != -1) {
if (dfs(i, j, a[i][j]) != n) {
i = n;
ans = false;
break;
}
}
}
}
cout << "Case #" << test_case << '\n';
if (ans) {
cout << "good\n";
}
else {
cout << "wrong\n";
}
}
return 0;
}Editor is loading...
Leave a Comment