zoukankan      html  css  js  c++  java
  • HOJ10641 Equidivisions [BFS]

    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:

    \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 <=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:

      1 #include <iostream>   
      2 #include <iomanip>   
      3 #include <fstream>   
      4 #include <sstream>   
      5 #include <algorithm>   
      6 #include <string>   
      7 #include <set>   
      8 #include <utility>   
      9 #include <queue>   
     10 #include <stack>   
     11 #include <list>   
     12 #include <vector>   
     13 #include <cstdio>   
     14 #include <cstdlib>   
     15 #include <cstring>   
     16 #include <cmath>   
     17 #include <ctime>   
     18 #include <ctype.h> 
     19 using namespace std;
     20 
     21 int map[101][101];
     22 int vst[101][101];
     23 int n;
     24 int cnt;
     25 int dir[4][2]={
     26     0,1,
     27     0,-1,
     28     1,0,
     29     -1,0
     30 };
     31 
     32 typedef struct node
     33 {
     34     int x,y;
     35 }Node;
     36 
     37 bool check(node temp)
     38 {
     39     if(temp.x>=0&&temp.x<n&&temp.y>=0&&temp.y<n&&!vst[temp.x][temp.y])
     40         return true;
     41     else
     42         return false;
     43 }
     44 
     45 void bfs(int x,int y)
     46 {
     47     int i;
     48     cnt=1;
     49     node pre,last;
     50     queue<node>Que;
     51     vst[x][y]=1;
     52     pre.x=x;
     53     pre.y=y;
     54     Que.push(pre);
     55     while(!Que.empty())
     56     {
     57         pre=Que.front();
     58         Que.pop();
     59         for(i=0;i<4;i++)
     60         {
     61             last.x=pre.x+dir[i][0];
     62             last.y=pre.y+dir[i][1];
     63             if(check(last)&&map[x][y]==map[last.x][last.y])
     64             {
     65                 cnt++;
     66                 vst[last.x][last.y]=1;
     67                 Que.push(last);
     68                 if(cnt==n)
     69                     return;
     70             } 
     71          }
     72     }
     73 }
     74 
     75 int main()
     76 {
     77     int i,j;
     78     int x,y;
     79     char str[10002];
     80     bool flag;
     81     while(~scanf("%d",&n),n)
     82     {
     83         memset(vst,0,sizeof(vst));
     84         memset(map,0,sizeof(map));
     85         for(i=1;i<n;i++)
     86         {    for(j=0;j<n;j++)
     87             {
     88                 scanf("%d%d",&x,&y);
     89                 map[x-1][y-1]=i;
     90             }
     91             gets(str);
     92         }
     93         for(i=0;i<n;i++)
     94         {
     95             for(j=0;j<n;j++)
     96             {
     97                 flag=true;
     98                 if(!vst[i][j])
     99                 {
    100                     bfs(i,j);
    101                      if(cnt!=n)
    102                     {
    103                         flag=false;
    104                         break;
    105                     }
    106                 }
    107             }
    108             if(!flag)
    109                 break;
    110         }
    111         if(flag)
    112             printf("good\n");
    113         else
    114             printf("wrong\n");
    115     }
    116     return 0;
    117 }
  • 相关阅读:
    AC自动机 HDOJ 2222 Keywords Search
    AC自动机 HDOJ 5384 Danganronpa
    贪心 HDOJ 5385 The Path
    区间DP UVA 10739 String to Palindrome
    区间DP UVA 10453 Make Palindrome
    素数专题
    判素数+找规律 BestCoder Round #51 (div.2) 1001 Zball in Tina Town
    DP专题
    贪心+模拟 ZOJ 3829 Known Notation
    概率DP ZOJ 3822 Domination
  • 原文地址:https://www.cnblogs.com/XBWer/p/2605811.html
Copyright © 2011-2022 走看看