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