Saturday, May 26, 2012

UVA 11110 Equidivisions Solution

Problem Statement

An equidivision of an n x 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 5x5 square:
\epsfbox{table1.eps}       \epsfbox{table2.eps}
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 x n square array is denoted by a pair (i, j), with 1$ \le$i, j$ \le$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
a1 a2 a3 a4 ...
means that cells denoted with the pairs (a1, a2), (a3, a4), ... 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 

2
1 2 2 1
5
1 1 1 2 1 3 3 2 2 2
2 1 4 2 4 1 5 1 3 1
4 5 5 2 5 3 5 5 5 4
2 5 3 4 3 5 4 3 4 4
5
1 1 1 2 1 3 3 2 2 2
2 1 3 1 4 1 5 1 4 2
4 5 5 2 5 3 5 5 5 4
2 4 1 4 3 5 4 3 4 4
0

Sample Output 

wrong
good
wrong

Solution

#include <cstdio>
#include <iostream>
#include <queue>
#include <cstring>

using namespace std;

#define sz 101

int N,table[sz][sz];

typedef struct{
 int i,j;
}node;

node cur,tmp;
queueq;
int di[] = {1,-1,0,0},dj[] = {0,0,1,-1};
int dfs(int i,int j){
 int cnt = 0;
 cur.i = i;
 cur.j = j;
 int key = table[i][j];
 table[cur.i][cur.j] = -1;
 q.push(cur);
 while(!q.empty()){
  cur = q.front();
  q.pop();
  cnt++;
  for(int ii = 0; ii < 4; ii++){
   tmp = cur;
   tmp.i += di[ii];
   tmp.j += dj[ii];
   if(tmp.i > 0 && tmp.i <= N && tmp.j > 0 && tmp.j <= N && table[tmp.i][tmp.j] != -1 && table[tmp.i][tmp.j] == key){
    q.push(tmp);
    table[tmp.i][tmp.j] = -1;
   }
  }
 }
 return cnt;
}
bool calc(){
 for(int i = 1; i <= N; i++){
  for(int j = 1; j <= N; j++){
   if(table[i][j] != -1 && dfs(i,j) != N)
    return false;
  }
 }
 return true;
}

int main(){
 int i,j,a,b;
 char ch;

 //freopen("i.txt","r",stdin);
 
 while(scanf("%d",&N) && N){
  memset(table,0,sizeof(table));
  bool wrongInput = true;
  for(i = 1; i < N; i++){
   while(scanf("%d%d%c",&a,&b,&ch)){
    if(a < 0 || a > N || b < 0 || b > N || (table[a][b] > 0 && table[a][b] != i)){
     wrongInput = false;
    }
    else
     table[a][b] = i;
    if(ch == '\n' || ch == '\0'){
     break;
    }
   }
  }
  if(wrongInput && calc())
   printf("good\n");
  else
   printf("wrong\n");
 }
 
 return 0;
}

No comments:

Post a Comment