poj3194 Equidivisions

来源:互联网 发布:指纹算法 编辑:程序博客网 时间:2024/06/06 05:02

Equidivisions
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 2665 Accepted: 1555

Description

An equidivision of an n × n square array of cells is a partition of the n2 cells in the array in exactly n sets, each one with n contiguous cells. Two cells are contiguous when they have a common side.

A good equidivision is composed of contiguous regions. The figures show a good and a wrong equidivision for a 5 × 5 square:

Note that in the second example the cells labeled with 4 describe three non-contiguous regions and cells labeled with 5 describe two non-contiguous regions. You must write a program that evaluates if an equidivision of the cells in a square array is good or not.

Input

It is understood that a cell in an n × n square array is denoted by a pair (ij), with 1 ≤ ij ≤ n. The input file contains several test cases. Each test case begins with a line indicating n, 0 < n < 100, the side of the square array to be partitioned. Next, there are n − 1 lines, each one corresponding to one partition of the cells of the square, with some non-negative integer numbers. Consecutive integers in a line are separated with a single blank character. A line of the form

a1a2a3a4

means that cells denoted with the pairs (a1a2), (a3a4), … belong to one of the areas in the partition. The last area in the partition is defined by those cells not mentioned in the n − 1 given lines. If a case begins with n = 0 it means that there are no more cases to analyze.

Output

For each test case good must be printed if the equidivision is good, in other case, wrong must be printed. The answers for the different cases must preserve the order of the input.

Sample Input

21 2 2 151 1 1 2 1 3 3 2 2 22 1 4 2 4 1 5 1 3 14 5 5 2 5 3 5 5 5 42 5 3 4 3 5 4 3 4 451 1 1 2 1 3 3 2 2 22 1 3 1 4 1 5 1 4 24 5 5 2 5 3 5 5 5 42 4 1 4 3 5 4 3 4 40

Sample Output

wronggoodwrong

Source

Colombia 2006


简单dfs

#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;int map[105][105];int vis[105][105];int cnt,n;int dx[4]={0,1,0,-1};int dy[4]={1,0,-1,0};void DFS(int x,int y,int color){    if (vis[x][y]==1) return;    if (x<0 || x>=n || y<0 || y>=n) return;    if (map[x][y]!=color) return;    cnt++;    vis[x][y]=1;    for (int i=0;i<4;i++)    {        DFS(x+dx[i],y+dy[i],color);    }}int main(){    int i,j,x,y;    while(1)    {        scanf("%d",&n);        if (n==0) break;        memset(map,0,sizeof(map));        memset(vis,0,sizeof(vis));        for (i=1;i<n;i++)        {            for (j=0;j<n;j++)            {                scanf("%d%d",&x,&y);                map[x-1][y-1]=i;            }        }        for (i=0;i<n;i++)        {            for (j=0;j<n;j++)            {                cnt=0;                if (vis[i][j]==1) continue;                DFS(i,j,map[i][j]);                if (cnt!=n) break;            }            if (j<n) break;        }        if (i<n) printf("wrong\n");        else printf("good\n");    }    return 0;}




0 0
原创粉丝点击