题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1312
6 9 ....#. .....# ...... ...... ...... ...... ...... #@...# .#..#. 11 9 .#......... .#.#######. .#.#.....#. .#.#.###.#. .#.#..@#.#. .#.#####.#. .#.......#. .#########. ........... 11 6 ..#..#..#.. ..#..#..#.. ..#..#..### ..#..#..#@. ..#..#..#.. ..#..#..#.. 7 7 ..#.#.. ..#.#.. ###.### ...@... ###.### ..#.#.. ..#.#.. 0 0
45 59 6 13
题解: 简单深搜,都不用任何剪枝——走过的点标记一下就好~~ 直接递归
AC代码:
#include<iostream>
#define maxn 25
using namespace std;
char chess[maxn][maxn],ch;
int sx,sy,w,h;
int dfs(int x,int y){
int sum=0;
if(x<0||x>=h||y<0||y>=w||chess[x][y]=='#')return 0;
chess[x][y]='#';
return 1+dfs(x+1,y)+dfs(x,y+1)+dfs(x-1,y)+dfs(x,y-1);
}
int main()
{
while(cin>>w>>h&&(w||h))
{
for(int i=0;i<h;i++)
for(int j=0;j<w;j++){
cin>>ch; chess[i][j]=ch;
if(ch=='@'){
chess[i][j]='.'; sx=i; sy=j;
}
}
cout<<dfs(sx,sy)<<endl;
}
}
原文:http://blog.csdn.net/mummyding/article/details/43083989