您的位置:首页 > 其它

Red and Black(hdu 1312)

2014-03-13 14:13 218 查看

dfs+递归调用

#include <stdio.h>
int w,h;
char z[21][21];
int f(int x,int y){
if(x<0||x>=w||y<0||y>=h)
return 0;
if(z[x][y]=='#')
return 0;
else{
z[x][y]='#';
return 1+f(x-1,y)+f(x+1,y)+f(x,y-1)+f(x,y+1);
}
}
int main()
{
int i,j;
while(~scanf("%d%d",&h,&w)&&w&&h){
for(i=0;i<w;i++)
scanf("%s",z[i]);
for(i=0;i<w;i++)
for(j=0;j<h;j++)
if(z[i][j]=='@')
printf("%d\n",f(i,j));
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: