标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 22543 Accepted Submission(s): 12980
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <string> #include <stack> #include <queue> #include <algorithm> const int inf = (1<<31)-1; const int MAXN = 1e2+10; const int MMAXN = 5e4+10; using namespace std; struct step{ int x; int y; }; int n,m; queue<step>q; /*step q[MMAXN]; int l,r;*/ char G[MAXN][MAXN]; int mov[8][2]={0,1,0,-1,1,0,-1,0,1,1,-1,-1,-1,1,1,-1}; /*int mov[4][2]={0,1,1,0,1,-1,1,1};*/ int check(int x,int y){ if(x<0||y<0||x>=n||y>=m)return 0; if(G[x][y]==‘*‘)return 0; else return 1; } int main() { int tk; step t,fro; int nx,ny; while(scanf("%d%d",&n,&m),n){ tk = 0; for(int i=0;i<n;i++){ scanf("%s",G[i]); } for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ if(G[i][j]==‘@‘){ tk++; t.x = i; t.y = j; q.push(t); G[i][j]=‘*‘; while(!q.empty()){ fro = q.front(); q.pop(); // G[fro.x][fro.y] = ‘*‘; for(int i=0;i<8;i++){ nx = fro.x+mov[i][0]; ny = fro.y+mov[i][1]; if(check(nx,ny)){ t.x = nx; t.y = ny; q.push(t); G[nx][ny] = ‘*‘; } } } /* G[i][j] = ‘*‘; l = r = 0; q[r++] = t; while(l<r){ fro = q[l]; l++; // G[fro.x][fro.y] = ‘*‘; for(int i=0;i<8;i++){ nx = fro.x+mov[i][0]; ny = fro.y+mov[i][1]; if(check(nx,ny)){ t.x = nx; t.y = ny; q[r++] = t; G[nx][ny] = ‘*‘; } } }*/ } } } cout<<tk<<endl; } //cout << "Hello world!" << endl; return 0; }
标签:
原文地址:http://www.cnblogs.com/EdsonLin/p/5433311.html