#include <iostream> #define MAXN 25 using namespace std; int r; int c; char _m[MAXN][MAXN]; bool mark[MAXN][MAXN]; int dir[4][2] = {0,1,0,-1,1,0,-1,0}; int DFS(int x,int y,int sum); int main() { //freopen("acm.acm","r",stdin); int i; int j; int place_x; int place_y; while(cin>>c>>r,c||r) { memset(mark,false,sizeof(mark)); for(i = 0; i < r; ++ i) { for(j = 0; j < c; ++ j) { cin>>_m[i][j]; if(_m[i][j] == '@') { place_x = i; place_y = j; } } } int ans = DFS(place_x,place_y,0); cout<<ans+1<<endl; } } int DFS(int x,int y,int sum) { int i; int j; int tem_i; int tem_j; for(i = 0; i < 4; ++ i) { tem_i = x+dir[i][0]; tem_j = y+dir[i][1]; if(tem_i >= 0 && tem_i < r && tem_j >= 0 && tem_j < c && _m[tem_i][tem_j] == '.' && !mark[tem_i][tem_j]) { mark[tem_i][tem_j] = true; sum = DFS(tem_i,tem_j,sum+1); } } return sum; }
关注我的公众号,当然,如果你对Java, Scala, Python等技术经验,以及编程日记,感兴趣的话。
技术网站地址: vmfor.com