标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1799 Accepted Submission(s): 378
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<string> #include<cstdlib> #include<algorithm> #include<queue> #include<vector> #include<stack> using namespace std; int n,m,map[305][305],sx,sy,vis[305][305],ans,mark[305][305]; int dx[4]={0,0,1,-1}; int dy[4]={1,-1,0,0}; bool check(int x,int y) { if(x<0||y<0||x>=n||y>=m) return false; return true; } bool dfs(int x,int y) { int xx,yy; if(x==0||y==0||x==n-1||y==m-1) return true; vis[x][y]=1; for(int i=0;i<4;i++) { xx=x+dx[i],yy=y+dy[i]; if(!check(xx,yy)||vis[xx][yy]) continue; vis[xx][yy]=1; if(map[xx][yy]>0) continue; if(dfs(xx,yy)) return true; } return false; } void bfs(int sx,int sy) { int xx,yy,x,y; queue<int> q; q.push(sx),q.push(sy); while(!q.empty()) { x=q.front(),q.pop(); y=q.front(),q.pop(); for(int i=0;i<4;i++) { xx=x+dx[i],yy=y+dy[i]; if(!check(xx,yy)) continue; if(mark[xx][yy]) continue; mark[xx][yy]=1; if(vis[xx][yy]) ans+=map[xx][yy]-1; else { ans+=map[xx][yy]; q.push(xx),q.push(yy); } } } } int main() { while(scanf("%d%d",&n,&m)!=EOF) { ans=0; memset(vis,0,sizeof(vis)); memset(mark,0,sizeof(mark)); for(int i=0;i<n;i++) for(int j=0;j<m;j++) { scanf("%d",&map[i][j]); if(map[i][j]==-1) sx=i,sy=j; } if(dfs(sx,sy)) { printf("Ali Win\n"); continue; } else { for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(!mark[i][j]) { if(i==0||j==0||i==n-1||j==m-1) { mark[i][j]=1; if(vis[i][j]) ans+=map[i][j]-1; else ans+=map[i][j],bfs(i,j); } } } } } if(ans&1) printf("Ali Win\n"); else printf("Baba Win\n"); } return 0; }
标签:
原文地址:http://www.cnblogs.com/a972290869/p/4355901.html