Description
Input
Output
Sample Input
6 9 ....#. .....# ...... ...... ...... ...... ...... #@...# .#..#. 11 9 .#......... .#.#######. .#.#.....#. .#.#.###.#. .#.#..@#.#. .#.#####.#. .#.......#. .#########. ........... 11 6 ..#..#..#.. ..#..#..#.. ..#..#..### ..#..#..#@. ..#..#..#.. ..#..#..#.. 7 7 ..#.#.. ..#.#.. ###.### ...@... ###.### ..#.#.. ..#.#.. 0 0
Sample Output
45 59 6 13
#include <iostream> using namespace std; char a[25][25]; int n,m,total; int dr[4] = {0,1,0,-1};//行变化 int dc[4] = {1,0,-1,0};//列变化 //上面的原来一直不会用,知道的话非常方便 bool judge(int x,int y) { if(x<1 || x>n || y<1 || y>m) return 1; if(a[x][y]==‘#‘) return 1; return 0; } void dfs(int r,int c) { total++; a[r][c]=‘#‘; //走过一次,“。”变为“#”,避免重复 for(int k=0; k<4; k++) { int lr = r + dr[k]; int lc = c + dc[k]; if(judge(lr,lc)) continue; dfs(lr,lc); } } int main() { while(cin>>m>>n&&m&&n) { int i,j,x,y; total=0; for(i=1; i<=n; i++) for(j=1; j<=m; j++) { cin>>a[i][j]; if(a[i][j]==‘@‘) //这里必须用变量x,y x=i,y=j; } dfs(x,y); cout<<total<<endl; } return 0; }
原文:http://www.cnblogs.com/hfc-xx/p/4666884.html