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.
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.
3 3 1 1 1 1 -1 1 1 1 1
Baba Win
//
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct Node
{
int x,y;
};
const int maxn=310*310+10;
Node que[maxn];
int front,rear;
int n,m;
int a[310][310];
int vis[310][310];
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
int main()
{
while(scanf("%d%d",&n,&m)==2)
{
int tx,ty;
for(int i=0;i<n;i++) for(int j=0;j<m;j++)
{
scanf("%d",&a[i][j]);
if(a[i][j]==-1) tx=i,ty=j;
}
memset(vis,0,sizeof(vis));
Node cnt,tmp;
cnt.x=tx,cnt.y=ty;
vis[tx][ty]=1;
front=rear=0;
que[rear++]=cnt;
int liantong=0;
while(front!=rear)
{
tmp=que[front++];
if(front==maxn) front=0;
if(a[tmp.x][tmp.y]>0) continue;
if(tmp.x==0||tmp.x==n-1||tmp.y==0||tmp.y==m-1) liantong=1;
if(liantong) break;
for(int i=0;i<4;i++)
{
int x=tmp.x+dx[i],y=tmp.y+dy[i];
if(x<0||x>=n||y<0||y>=m) continue;
if(vis[x][y]) continue;
vis[x][y]=1;
cnt.x=x,cnt.y=y;
que[rear++]=cnt;
if(rear==maxn) rear=0;
}
}
if(liantong)
{
printf("Ali Win\n");continue;
}
int sum=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(i==0||i==n-1||j==0||j==m-1)
{
if(vis[i][j]==1) sum+=a[i][j]-1,vis[i][j]=3;
else if(vis[i][j]==0)
{
cnt.x=i,cnt.y=j;
vis[i][j]=2;
front=rear=0;
que[rear++]=cnt;
while(front!=rear)
{
tmp=que[front++];
if(front==maxn) front=0;
sum+=a[tmp.x][tmp.y];
for(int i=0;i<4;i++)
{
int x=tmp.x+dx[i],y=tmp.y+dy[i];
if(x<0||x>=n||y<0||y>=m) continue;
if(vis[x][y]>=2) continue;
if(vis[x][y]==1)
{
sum+=a[x][y]-1;
vis[x][y]=3;
}
else
{
vis[x][y]=2;
cnt.x=x,cnt.y=y;
que[rear++]=cnt;
if(rear==maxn) rear=0;
}
}
}
}
}
}
}
if(sum&1) printf("Ali Win\n");
else printf("Baba Win\n");
}
return 0;
}
/*
7 7
1 1 1 1 1 1 1
1 1 0 0 0 0 1
1 0 0 1 0 0 1
1 0 1 1 1 0 1
1 0 0 1 0 0 1
1 0 0 -1 0 0 1
1 1 1 1 1 1 1
*/