Red and Black(广搜实现HD1312)

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312

代码:

#include <iostream>
#include<queue>

using namespace std;
char map[21][21];
int row,col;
int startX,startY;
int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
struct Node
{
  int x,y;
};
Node N,P;
 int num;
void bfs()
{

   queue<Node> q;
   N.x=startX;
   N.y=startY;
   num=1;
   q.push(N);

   while(!q.empty())
   {
     N=q.front();
     q.pop();

     for(int i=0;i<4;i++)
     {
       int tx=N.x+dir[i][0];
       int ty=N.y+dir[i][1];
       if(tx>=0&&tx<row&&ty>=0&&ty<col&&map[tx][ty]=='.')
       {
         P.x=tx;
         P.y=ty;
         num++;
         map[tx][ty]='#';
         q.push(P);
       }
     }

   }

}
int main()
{
   while(cin>>col>>row&&col!=0&&row!=0)
   {
     for(int i=0;i<row;i++)
       for(int j=0;j<col;j++)
       {
         cin>>map[i][j];
         if(map[i][j]=='@')
         {
           startX=i;
           startY=j;
         }
       }
       bfs();
    cout<<num<<endl;
   }
}

[ Copy to Clipboard ]    [ Save to File]



Home | Top 	Hangzhou Dianzi University Online Judge 3.0
Copyright © 2005-2013 HDU ACM Team. All Rights Reserved.
Designer & Developer : Wang Rongtao LinLe GaoJie GanLu
Total 0.001803(s) query 1, Server time : 2013-05-24 21:51:57, Gzip enabled		Administration	

 

posted @ 2013-05-24 21:51  supersnow0622  Views(150)  Comments(0)    收藏  举报