您的位置:首页 > 产品设计 > UI/UE

HOJ---10641 Equidivisions [BFS]

2012-07-24 00:32 274 查看
Equidivisions
Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:32768KB
Total submit users: 34, Accepted users: 28
Problem 10641 : No special judgement
Problem description
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 ncontiguous 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:





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 <=i, j<= 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

Problem Source
XX Colombian National Programming Contest
数组要清零!

code:

#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <string>
#include <set>
#include <utility>
#include <queue>
#include <stack>
#include <list>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <ctype.h>
using namespace std;

int map[101][101];
int vst[101][101];
int n;
int cnt;
int dir[4][2]={
0,1,
0,-1,
1,0,
-1,0
};

typedef struct node
{
int x,y;
}Node;

bool check(node temp)
{
if(temp.x>=0&&temp.x<n&&temp.y>=0&&temp.y<n&&!vst[temp.x][temp.y])
return true;
else
return false;
}

void bfs(int x,int y)
{
int i;
cnt=1;
node pre,last;
queue<node>Que;
vst[x][y]=1;
pre.x=x;
pre.y=y;
Que.push(pre);
while(!Que.empty())
{
pre=Que.front();
Que.pop();
for(i=0;i<4;i++)
{
last.x=pre.x+dir[i][0];
last.y=pre.y+dir[i][1];
if(check(last)&&map[x][y]==map[last.x][last.y])
{
cnt++;
vst[last.x][last.y]=1;
Que.push(last);
if(cnt==n)
return;
}
}
}
}

int main()
{
int i,j;
int x,y;
char str[10002];
bool flag;
while(~scanf("%d",&n),n)
{
memset(vst,0,sizeof(vst));
memset(map,0,sizeof(map));
for(i=1;i<n;i++)
{    for(j=0;j<n;j++)
{
scanf("%d%d",&x,&y);
map[x-1][y-1]=i;
}
gets(str);
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
flag=true;
if(!vst[i][j])
{
bfs(i,j);
if(cnt!=n)
{
flag=false;
break;
}
}
}
if(!flag)
break;
}
if(flag)
printf("good\n");
else
printf("wrong\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: