HDU ACM 1312 Red and Black(BFS)
http://acm.hdu.edu.cn/showproblem.php?pid=1312
题意:给一幅图,算出有从起点出发最多能走多少步.
一个简单的搜索题,用的是广搜
View Code
1 #include "iostream" 2 #include "queue" 3 using namespace std; 4 struct FUN 5 { 6 int x; 7 int y; 8 }; 9 int point[4][2]={{0,1},{0,-1},{1,0},{-1,0}}; 10 int mark_x; 11 int mark_y; 12 int n; 13 int m; 14 int j; 15 int i; 16 char c; 17 char map[30][30]={0}; 18 int BFS(int x,int y) 19 { 20 int sum=1; 21 queue <FUN> q; 22 FUN a,b; 23 a.x=x; 24 a.y=y; 25 q.push(a); 26 map[x][y]='#'; 27 while(!q.empty()) 28 { 29 b=q.front(); 30 q.pop(); 31 for(i=0;i<4;i++) 32 { 33 a.x = b.x + point[i][0]; 34 a.y = b.y + point[i][1]; 35 if(map[a.x][a.y]!='#'&& a.x>0 && a.y>0 && a.x<=m && a.y<=n) 36 { 37 sum++; 38 q.push(a); 39 map[a.x][a.y]='#'; 40 41 } 42 } 43 } 44 return sum; 45 } 46 int main() 47 { 48 49 while(cin>>n>>m,n+m) 50 { 51 for(i=1;i<=m;i++) 52 { 53 for(j=1;j<=n;j++) 54 { 55 cin>>c; 56 map[i][j]=c; 57 if(c=='@') 58 { 59 mark_x=i; 60 mark_y=j; 61 } 62 } 63 } 64 cout<<BFS(mark_x,mark_y)<<endl; 65 } 66 return 0; 67 }