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 forma1 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; }
相关文章推荐
- 搜索(bfs) HOJ 1448 Dungeon Master
- HOJ 1440 骑士巡游 BFS DFS
- HOJ 13101 The Triangle Division of the Convex Polygon(数论求卡特兰数(模不为素数))
- HOJ 1440 Knight Moves -------简单搜索 BFS 求l两点之间最小的到达步数
- HOJ 1030 Labyrinth----------------两次BFS求树的直径(图的最长路)
- HOJ 1448 - Dungeon Master(BFS)
- HOJ 2979 Escape from Pyramids --------BFS求最小的步数
- HOJ 2226&POJ2688 Cleaning Robot(BFS+TSP(状态压缩DP))
- HOJ 位图[BFS]
- bfs hoj Going to know him
- HOJ 2226&POJ2688 Cleaning Robot(BFS+TSP(状态压缩DP))
- HOJ---12363 Robots on a grid [DP+BFS()]
- HOJ 2581 Go-------------------BFS 围棋中所占地盘的计算
- HOJ---11491 A Knight and a Queen[超大数组+BFS]
- HOJ 12808 杀死提莫(BFS)
- POJ 3194 Equidivisions 笔记
- HOJ 3130 Qie-Gao -----------BFS
- 【HOJ 1448 Dungeon Master(BFS)】
- hoj 1440 bfs
- NYOJ592-spiral grid(蛇形填数+筛法素数+BFS)