标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 15291 Accepted Submission(s): 8787
#include<cstdio> #include<cstring> #include<cmath> using namespace std; int n,m; char grid[120][120]; int vis[120][120]; struct node{ int x,y; }; void in_put() { memset(vis,0,sizeof(vis)); for(int i=1;i<=n;++i) scanf("%s",grid[i]+1); } int check(node v) { if(!vis[v.x][v.y]&&v.x>=1&&v.x<=n&&v.y>=1&&v.y<=m&&grid[v.x][v.y]==‘@‘) return 1; else return 0; } void dfs(node v) { node nex; vis[v.x][v.y]=1;grid[v.x][v.y]=‘?‘; nex.x=v.x+1;nex.y=v.y;if(check(nex)) dfs(nex); nex.x=v.x+1;nex.y=v.y+1;if(check(nex)) dfs(nex); nex.x=v.x+1;nex.y=v.y-1;if(check(nex)) dfs(nex); nex.x=v.x;nex.y=v.y-1;if(check(nex)) dfs(nex); nex.x=v.x;nex.y=v.y+1;if(check(nex)) dfs(nex); nex.x=v.x-1;nex.y=v.y-1;if(check(nex)) dfs(nex); nex.x=v.x-1;nex.y=v.y+1;if(check(nex)) dfs(nex); nex.x=v.x-1;nex.y=v.y;if(check(nex)) dfs(nex); } int main() { while(scanf("%d%d",&n,&m)) { int cnt=0;node now; if(!n&&!m) break; in_put(); for(int i=1;i<=n;++i) for(int j=1;j<=m;++j) if(grid[i][j]==‘@‘) {now.x=i;now.y=j;dfs(now);cnt++;} printf("%d\n",cnt); } }
2
标签:
原文地址:http://www.cnblogs.com/orchidzjl/p/4376679.html