6 9 ....#. .....# ...... ...... ...... ...... ...... #@...# .#..#. 11 9 .#......... .#.#######. .#.#.....#. .#.#.###.#. .#.#..@#.#. .#.#####.#. .#.......#. .#########. ........... 11 6 ..#..#..#.. ..#..#..#.. ..#..#..### ..#..#..#@. ..#..#..#.. ..#..#..#.. 7 7 ..#.#.. ..#.#.. ###.### ...@... ###.### ..#.#.. ..#.#.. 0 0
45 59 6 13
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
char color[25][25];
int ans;
int w, h;
int x, y;
int dx[]= {1, -1, 0, 0};
int dy[]= {0, 0, 1, -1};
void dfs(int x, int y, int w, int h)
{
ans++;
color[x][y]='#';
for(int i=0; i<4; i++)
{
x=x+dx[i];
y=y+dy[i];
if(x>=0 && x<h && y>=0 && y<w && color[x][y]=='.')
dfs(x, y, w, h);
x-=dx[i]; //要特别注意还原数据
y-=dy[i];
}
}
int main()
{
while(scanf("%d%d", &w, &h)!=EOF)
{
if(w==0 && h==0)
break;
ans=0;
memset(color, 0, sizeof(color));
for(int i=0; i<h; i++)
{
scanf("%s", color[i]);
for(int j=0; j<w; j++)
{
if(color[i][j]=='@')
{
x=i;
y=j;
break;
}
}
}
dfs(x, y, w, h);
cout<<ans<<endl;
}
return 0;
}
HDU 1312:Red and Black,布布扣,bubuko.com
原文:http://blog.csdn.net/u013487051/article/details/37350929