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
首先,我们需要从值为-1的位置由内向外搜索一次,标记出包围-1并且值不为0的最近一圈区域(临界区域),如果发现此次搜索出了边界,那么肯定是Ali赢,否则进行以下步骤:从外围开始由外向内搜索,如果遇到非A区域的石头,总计和ans加上当前位置的HP,如果遇到的是A区域的石头,总计和ans加上当前位置HP-1(必须要留下一个HP,否则对方就赢了),最后求得的ans也就是Ali(先走)能够实施的合法步骤数,ans如果为奇数那么Ali赢,否则Baba赢。
-
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
int map[303][303],n,m;
bool vis[303][303],vis2[303][303];
int dir[4][2]={{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int ans;
struct node
{
int x;
int y;
node(int x=0,int y=0)
{
this->x=x;
this->y=y;
}
};
bool dfs(int x,int y) //搜索临界区
{
if(x==0 || y==0 || x==n-1|| y==m-1) //搜到边界
return true;
for (int i=0; i<4; i++)
{
int nx=x+dir[i][0];
int ny=y+dir[i][1];
if(nx>=0 && nx<n && ny>=0 && ny<m && !vis[nx][ny])
{
vis[nx][ny]=true;
if(map[nx][ny])
continue;
if(dfs(nx, ny))
return true;
}
}
return false;
}
bool check(int x,int y)
{
if(x>=0 && x<n && y>=0 && y<m && !vis2[x][y])
return true;
return false;
}
int bfs(int x,int y)
{
queue<node>q;
q.push(node(x,y));
vis2[x][y]=1;
node t,temp;
while (!q.empty())
{
t=q.front();
q.pop();
for (int i=0; i<4; i++)
{
temp=t;
temp.x+=dir[i][0];
temp.y+=dir[i][1];
if(check(temp.x,temp.y))
{
vis2[temp.x][temp.y]=true;
if(!vis[temp.x][temp.y])
{
ans+=map[temp.x][temp.y];
q.push(temp);
}
else if(vis[temp.x][temp.y] && map[temp.x][temp.y]>0)
ans+=map[temp.x][temp.y]-1;
}
}
}
return ans;
}
int main()
{
int sx,sy;
while (scanf("%d%d",&n,&m)!=EOF)
{
for (int i=0; i<n; i++)
{
for (int j=0; j<m; j++)
{
scanf("%d",&map[i][j]);
vis[i][j]=vis2[i][j]=false;
if(map[i][j]==-1)
{
sx=i;
sy=j;
}
}
}
if(dfs(sx, sy))
{
puts("Ali Win");
continue;
}
ans=0;
for (int i=0; i<n; i++)
{
for (int j=0; j<m; j++)
{
if(!vis2[i][j])
{
if(i==0|| j==0||i==n-1|| j==m-1) //从每个边界开始处理
{
vis2[i][j]=true;
if(!vis[i][j])
{
ans+=map[i][j];
bfs(i,j);
}
else if(vis[i][j])
ans+=map[i][j]-1;
}
}
}
}
if(ans&1)
puts("Ali Win");
else
puts("Baba Win");
}
return 0;
}