标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1057 Accepted Submission(s): 454
#include <cstdio> #include <cstring> #include <queue> #include <algorithm> #include <stdlib.h> using namespace std; const int N = 250; int n,m,a,b; char graph[N][N]; bool vis[N][N]; struct Node{ int x,y; }; bool check(int x,int y,char c){ if(x%2){ if(x<1||x>n||y<1||y>m||vis[x][y]||graph[x][y]==‘E‘||graph[x][y]!=c) return false; }else{ if(x<1||x>n||y<1||y>=m||vis[x][y]||graph[x][y]==‘E‘||graph[x][y]!=c) return false; } return true; } bool check2(int x,int y){ if(x%2){ if(x<1||x>n||y<1||y>m||vis[x][y]||graph[x][y]==‘E‘) return false; }else{ if(x<1||x>n||y<1||y>=m||vis[x][y]||graph[x][y]==‘E‘) return false; } return true; } int dir1[][2]={{0,-1},{0,1},{1,1},{1,0},{-1,0},{-1,1}}; ///偶数行 int dir2[][2]={{0,-1},{0,1},{-1,-1},{-1,0},{1,-1},{1,0}}; ///奇数行 void bfs(){ queue<Node> q; Node s; s.x = a,s.y = b; vis[s.x][s.y] = true; q.push(s); while(!q.empty()){ Node now = q.front(); q.pop(); Node next; if(now.x%2==0){ for(int i=0;i<6;i++){ next.x = now.x+dir1[i][0]; next.y = now.y+dir1[i][1]; if(!check(next.x,next.y,graph[now.x][now.y])) continue; vis[next.x][next.y] = true; q.push(next); } }else{ for(int i=0;i<6;i++){ next.x = now.x+dir2[i][0]; next.y = now.y+dir2[i][1]; if(!check(next.x,next.y,graph[now.x][now.y])) continue; vis[next.x][next.y] = true; q.push(next); } } } } void bfs2(int k){ queue<Node> q; Node s; s.x = 1,s.y = k; vis[s.x][s.y] = true; q.push(s); while(!q.empty()){ Node now = q.front(); q.pop(); Node next; if(now.x%2==0){ for(int i=0;i<6;i++){ next.x = now.x+dir1[i][0]; next.y = now.y+dir1[i][1]; if(!check2(next.x,next.y)) continue; vis[next.x][next.y] = true; q.push(next); } }else{ for(int i=0;i<6;i++){ next.x = now.x+dir2[i][0]; next.y = now.y+dir2[i][1]; if(!check2(next.x,next.y)) continue; vis[next.x][next.y] = true; q.push(next); } } } } int main(){ while(scanf("%d%d%d%d",&n,&m,&a,&b)!=EOF){ memset(graph,0,sizeof(graph)); for(int i=1;i<=n;i++){ scanf("%s",graph[i]+1); } memset(vis,false,sizeof(vis)); bfs(); int ans = 0; for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ if(vis[i][j]) ans++; } } if(ans<3){ printf("%d\n",0); continue; } for(int i=1;i<=m;i++){ if(graph[1][i]==‘E‘||vis[1][i]) continue; bfs2(i); } for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ if(i%2==0&&j==m) continue; if(graph[i][j]==‘E‘) continue; if(!vis[i][j]) ans++; } } printf("%d\n",ans); } }
标签:
原文地址:http://www.cnblogs.com/liyinggang/p/5741981.html