简单题, BFS
View Code
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
using namespace std;
#define maxn 105
struct Point
{
int x, y;
Point()
{
}
Point(int xx, int yy): x(xx), y(yy)
{
}
}q[maxn * maxn];
int n;
int map[maxn][maxn];
bool vis[maxn][maxn];
int dir[4][2] = {{1, 0},{0, 1},{-1, 0},{0, -1}};
void input()
{
memset(map, 0, sizeof(map));
for (int i = 1; i < n; i++)
for (int j = 0; j < n; j++)
{
int a, b;
scanf("%d%d", &a, &b);
a--;
b--;
map[a][b] = i;
}
}
int bfs(Point s)
{
int front, rear;
front = rear = 0;
q[rear++] = s;
vis[s.x][s.y] = true;
int ret = 1;
while (front != rear)
{
Point p = q[front++];
for (int i = 0; i < 4; i++)
{
Point a(p.x + dir[i][0], p.y + dir[i][1]);
if (a.x >= 0 && a.y >= 0 && a.x < n && a.y < n && !vis[a.x][a.y] && map[a.x][a.y] == map[p.x][p.y])
{
vis[a.x][a.y] = true;
q[rear++] = a;
ret++;
}
}
}
return ret;
}
bool work()
{
memset(vis, 0, sizeof(vis));
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (!vis[i][j])
{
int x = bfs(Point(i, j));
if (x != n)
return false;
}
return true;
}
int main()
{
//freopen("t.txt", "r", stdin);
while (scanf("%d", &n), n)
{
input();
if (work())
printf("good\n");
else
printf("wrong\n");
}
return 0;
}