ACM PKU 1164 The Castle http://acm.pku.edu.cn/JudgeOnline/problem?id=1164

建图比较难!建议开结构体数组!
#include <iostream>
#include<queue>
using namespace std;

int val[4] = {8,4,2,1};
bool flag[4];

struct room
{
 int m;
 int c;
 bool e;bool w;
    bool s;bool n;
// room(){};
 /*room(int a ,int b, bool x,bool y,bool z,bool t)
 {
  m = a;
  c = b;
  e = x;
  w = y;
  s = z;
  n = t;
 }*/
};

room num[50][50];
bool sign[50][50];
queue<room>myqueue;

int main ()
{
 int row,column;
 cin >> row >> column;
    int a;
 int i,j;
 memset(sign,0,sizeof(sign));
 for(i = 0;i < row ; i++)
  for (j = 0;j < column; j++)
  {
   int k;
   cin >> a;

   memset(flag,0,sizeof(flag));
   for (k= 0;k < 4; k++)
   {
    if (a - val[k] < 0)
    {
    
     flag[k] = 1;
    }
    else 
     a = a - val[k];
   } 

   num[i][j].m = i;
   num[i][j].c = j;
   num[i][j].s = flag[0];
   num[i][j].e = flag[1];
   num[i][j].n = flag[2];
   num[i][j].w = flag[3];         
  }

  int house = 0;
  int Max = 0;
  int Sum;
  
  for (i = 0;i < row; i++)
   for (j = 0;j < column; j++)
   {
    if (sign[i][j] == 0)
    {
     house++;
        Sum = 0;
     myqueue.push(num[i][j]);
     sign[i][j] = 1;
     while(!myqueue.empty())
     {
     if (myqueue.front().c > 0 && myqueue.front().w == 1)
     {
      if (sign[myqueue.front().m][myqueue.front().c-1]==0)
      {
      myqueue.push(num[myqueue.front().m][myqueue.front().c-1]);
      sign[myqueue.front().m][myqueue.front().c-1] = 1;
 
      }
     }
     if (myqueue.front().c < column-1 && myqueue.front().e == 1)
     {
      if (sign[myqueue.front().m][myqueue.front().c+1]==0)
      {
      myqueue.push(num[myqueue.front().m][myqueue.front().c+1]);
       sign[myqueue.front().m][myqueue.front().c+1] =1;
    
     }
     }
     if (myqueue.front().m > 0 &&myqueue.front().n == 1)
     {
      if (sign[myqueue.front().m-1][myqueue.front().c] == 0)
      {
      myqueue.push(num[myqueue.front().m-1][myqueue.front().c]);
       sign[myqueue.front().m-1][myqueue.front().c] = 1;
   
      }
     }
     if (myqueue.front().m < row-1  && myqueue.front().s == 1)
     {
      if (sign[myqueue.front().m+1][myqueue.front().c]==0)
      {
      myqueue.push(num[myqueue.front().m+1][myqueue.front().c]);
       sign[myqueue.front().m+1][myqueue.front().c] = 1;
       
      }
     }
     myqueue.pop();
     Sum++;
     }
     if (Sum > Max) Max = Sum;
    }
    
   }
   cout<< house <<endl;
   cout<< Max <<endl;
   return 0;
}

posted on 2011-05-06 18:48  _Clarence  阅读(59)  评论(0编辑  收藏

导航

统计