#include<cstdio>
using namespace std;
int startx,starty,tx,ty,w,h,head=0,tail=0,block;
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
char mapp[25][25]={};
int book[25][25]={};
struct node{
int x,y,step;
}que[626];
void bfs(int sx,int sy){
block=1;
for(int i=0;i<25;i++){
for(int j=0;j<25;j++){
book[i][j]=0;
}
}
book[sx][sy]=1;
que[tail].x=sx;
que[tail].y=sy;
que[tail].step=1;
tail++;
while(head<tail){
for(int i=0;i<4;i++){
tx=que[head].x+dir[i][0];
ty=que[head].y+dir[i][1];
if(tx>=0&&tx<h&&ty>=0&&ty<w&&book[tx][ty]==0&&mapp[tx][ty]!='#'){
que[tail].x=tx;
que[tail].y=ty;
que[tail].step=que[head].step+1;
book[tx][ty]=1;
tail++;
block++;
}
}
head++;
}
}
int main(){
head=0,tail=0;
while(scanf("%d%d",&w,&h)!=EOF){
if(w==0&&h==0){
break;
}
for(int i=0;i<25;i++){
for(int j=0;j<25;j++){
mapp[i][j]=0;
}
}
for(int i=0;i<h;i++){
scanf("%s",&mapp[i]);
for(int j=0;j<w;j++){
if(mapp[i][j]=='@'){
startx=i;
starty=j;
}
}
}
bfs(startx,starty);
printf("%d\n",block);
}
return 0;
}
这段代码对应的题目:
(poj1979)
Description
There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can't move on red tiles, he can move only on black tiles.
Write a program to count the number of black tiles which he can reach by repeating the moves described above.
Input
The input consists of multiple data sets. A data set starts with a line containing two positive integers W and H; W and H are the numbers of tiles in the x- and y- directions, respectively. W and H are not more than 20.
There are H more lines in the data set, each of which includes W characters. Each character represents the color of a tile as follows.
'.' - a black tile
'#' - a red tile
'@' - a man on a black tile(appears exactly once in a data set)
The end of the input is indicated by a line consisting of two zeros.
Output
For each data set, your program should output a line which contains the number of tiles he can reach from the initial tile (including itself).
内容为全英文,请见谅
样例输入和样例输出被CSDN误判为无意义内容,因此此处不展示,可前往http://poj.org/problem?id=1979查看
用了G++和C++提交,都是runtime error(原因不明),请帮忙看看,谢谢