HDU 4101 Ali and Baba
来源:互联网 发布:java重写tostring方法 编辑:程序博客网 时间:2024/05/22 09:47
Ali and Baba
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Problem Description
There is a rectangle area (with N rows and M columns) in front of Ali and Baba, each grid might be one of the following:
1. Empty area, represented by an integer 0.
2. A Stone, represented by an integer x (x > 0) which denote the HP of this stone.
3. Treasure, represented by an integer -1.
Now, Ali and Baba get the map of this mysterious area, and play the following game:
Ali and Baba play alternately, with Ali starting. In each turn, the player will choose a stone that he can touch and hit it. After this operation, the HP of the stone that been hit will decrease by 1. If some stone’s HP is decreased to 0, it will become an empty area. Here, a player can touch a stone means
there is path consist of empty area from the outside to the stone. Note that two grids are adjacent if and only if they share an edge.
The player who hits the treasure first wins the game.
Input
The input consists several testcases.
The first line contains two integer N and M (0 < N,M <= 300), the size of the maze.
The following N lines each contains M integers (less than 100), describes the maze, where a positive integer represents the HP of a stone, 0 reperents an empty area, and -1 reperents the treasure.
There is only one grid contains the treasure in the maze.
Output
“Ali Win” or “Baba Win” indicates the winner of the game.
Sample Input
3 3
1 1 1
1 -1 1
1 1 1
Sample Output
Baba Win
Source
2011 Alibaba-Cup Campus Contest
Recommend
lcy
//代码大概就是两遍BFS 还没调出来 明天再说 #include<iostream>#include<cstring>#include<cstring>#include<cstdio>#include<queue>using namespace std;struct coordinate{ int x,y; }now,nex;const int N = 505;int a[N][N],vis[N][N],vis_2[N][N],n,m,ok,sum;int dx[]={0,0,1,-1};int dy[]={1,-1,0,0};inline int read(int &x){ x=0; int f=1; char c=getchar(); while(c>'9'||c<'0'){ if(c=='-') f=-1; c=getchar(); } while(c>='0'&&c<='9'){ x=x*10+c-'0'; c=getchar(); } x*=f;}inline bool Judge(coordinate p){ if(p.x>=1&&p.x<=n&&p.y>=1&&p.y<=m) return true; else return false;}queue<coordinate> q;void BFS(int x,int y){//先判断 Ali 能否直接从某个边界出 出发 得到宝藏 memset(vis,0,sizeof vis ); now.x=x,now.y=y; q.push(now); vis[x][y]=1; while(!q.empty()){ now=q.front(); q.pop(); if(now.x==1||now.x==n||now.y==1||now.y==m){ ok=1; return ; } for(int i=0;i<4;i++){ nex.x=now.x+dx[i],nex.y=now.y+dy[i]; if(nex.x>=1&&nex.x<=n&&nex.y>=1&&nex.y<=m&&vis[nex.x][nex.y]==0){ vis[nex.x][nex.y]=1; if(a[nex.x][nex.y]==0) q.push(nex); } } } return ;}void BFS_2(int x,int y){ while(!q.empty()) q.pop(); memset(vis_2,0,sizeof vis_2 ); now.x=x,now.y=y; q.push(now); vis_2[x][y]=1; while(!q.empty()){ now=q.front();q.pop(); for(int i=0;i<4;++i){ nex.x=now.x+dx[i],nex.y=now.y+dy[i]; if(nex.x>=0&&nex.x<=n+1&&nex.y>=0&&nex.y<=m+1&&vis_2[nex.x][nex.y]==0&&vis_2[nex.x][nex.y]!=2){ if(vis[nex.x][nex.y]==0){ vis_2[nex.x][nex.y]=1,q.push(nex); } if(vis[nex.x][nex.y]==1){ vis_2[nex.x][nex.y]=2,sum+=a[nex.x][nex.y]-1; } } } } for(int i=0;i<=n+1;++i) for(int j=0;j<=m;++j) if(vis_2[i][j]==1) sum+=a[i][j];}int main(){ while(~scanf("%d%d",&n,&m)){ int sx,sy; memset(a,0,sizeof a ); for(int i=1;i<=n;++i) for(int j=1;j<=m;++j){ scanf("%d",&a[i][j]); if(a[i][j]==-1) sx=i,sy=j; } ok=0,sum=0; BFS(sx,sy); if(ok){ printf("Ali Win\n");continue; } BFS_2(0,0); if(sum%2==0) printf("Baba Win\n"); else printf("Ali Win\n"); } return 0;}
#include<iostream>#include<cstring>#include<cstring>#include<cstdio>#include<queue>using namespace std;struct coordinate{ int x,y; }now,nex;const int N = 505;int a[N][N],vis[N][N],vis_2[N][N],n,m,ok,sum;int dx[4]={0,0,1,-1};int dy[4]={1,-1,0,0};inline int read(int &x){ x=0; int f=1; char c=getchar(); while(c>'9'||c<'0'){ if(c=='-') f=-1; c=getchar(); } while(c>='0'&&c<='9'){ x=x*10+c-'0'; c=getchar(); } x*=f;}inline bool Judge(coordinate p){ if(p.x>=1&&p.x<=n&&p.y>=1&&p.y<=m) return true; else return false;}void BFS(int x,int y){//先判断 Ali 能否直接从某个边界出 出发 得到宝藏 queue<coordinate> q; memset(vis,0,sizeof vis ); now.x=x,now.y=y; q.push(now); vis[x][y]=1; while(!q.empty()){ now=q.front(); if(now.x==1||now.x==n||now.y==1||now.y==m){ ok=1; return ; } q.pop(); for(int i=0;i<4;i++){ nex.x=now.x+dx[i];nex.y=now.y+dy[i]; if(nex.x>=1&&nex.x<=n&&nex.y>=1&&nex.y<=m&&vis[nex.x][nex.y]==0){ vis[nex.x][nex.y]=1; if(a[nex.x][nex.y]==0) q.push(nex); } } } return ;}void BFS_2(int x,int y){ queue<coordinate> q; while(!q.empty()) q.pop(); memset(vis_2,0,sizeof vis_2 ); now.x=x,now.y=y; q.push(now); vis_2[x][y]=1; while(!q.empty()){ now=q.front();q.pop(); for(int i=0;i<4;++i){ nex.x=now.x+dx[i],nex.y=now.y+dy[i]; x=nex.x,y=nex.y; if(nex.x>=0&&nex.x<=n+1&&nex.y>=0&&nex.y<=m+1&&vis_2[x][y]==0&&vis_2[nex.x][nex.y]!=2){ if(vis[x][y]==0){ vis_2[x][y]=1,q.push(nex); } if(vis[x][y]==1){ vis_2[x][y]=2,sum+=a[x][y]-1; } } } } for(int i=0;i<=n+1;++i) for(int j=0;j<=m+1;++j) if(vis_2[i][j]==1) sum+=a[i][j];}int main(){ while(~scanf("%d%d",&n,&m)){ int sx,sy; memset(a,0,sizeof a ); for(int i=1;i<=n;++i) for(int j=1;j<=m;++j){ scanf("%d",&a[i][j]); if(a[i][j]==-1) sx=i,sy=j; } ok=0,sum=0; BFS(sx,sy); if(ok){ printf("Ali Win\n");continue; } BFS_2(0,0); if(sum%2==0) printf("Baba Win\n"); else printf("Ali Win\n"); } return 0;}
这篇代码和上一篇没有什么区别,只是队列定义在了函数里面,但是这样确确实实能AC啊
- HDU 4101 Ali and Baba
- HDU 4101 Ali and Baba
- HDU 4101 Ali and Baba
- HDU 4101 Ali and Baba
- BFS-hdu-4101-Ali and Baba
- hdu 4101 - Ali and Baba(搜索)
- HDU:4101 Ali and Baba(搜索+博弈)
- hdu 4101 Ali and Baba(bfs,dfs)
- HDU 4101Ali and Baba (bfs)
- hdu 4101 Ali and Baba (bfs+严密思维)
- HDU 4101 Ali and Baba 博弈, 连通块
- hdu 4101 Ali and Baba【BFS好题】
- [题解] HDU 4101 Ali and Baba (BFS)
- !HDU 4101 Ali and Baba-博弈-(bfs&dfs扫描二维点)
- HDU4101:Ali and Baba(BFS)
- HDU4101 Ali and Baba (bfs+dfs+博弈)
- Ali
- ali
- unique函数
- 将一个二维数组顺时针旋转90°
- CodeForces 632C C. The Smallest String Concatenation
- 【BZOJ 1604】Cow Neighborhoods(哈夫曼距离+Multiset)
- 一个跳动的爱心送给你,你接受吗?哈哈
- HDU 4101 Ali and Baba
- 面试心得
- 实验1 熟悉实验环境
- 写BootLoader
- TensorFlow-Bitcoin-Robot-一个基于 TensorFlow LSTM 模型的 Bitcoin 价格预测机器人
- 学习搭建springboot框架
- [猜结论][乱搞][博弈]Codechef . Palindromic Game
- angularJs 一些API
- 学生信息管理系统—课程设置窗体中怎么一次性添加或删除课程