HDU 4101Ali and Baba (bfs)

来源:互联网 发布:淘宝卖家怎么激活 编辑:程序博客网 时间:2024/05/22 05:17

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 31 1 11 -1 11 1 1

Sample Output

Baba Win

题目大意

两个人轮流玩游戏,Ali先手,-1代表宝藏所在位置,其他位置会存在一个HP值的石头,玩家每敲击一次HP值减1,0代表空地,只有格子为0时玩家才能通过这个位置去敲击相邻的石头。相邻定义为两个格子共边。

解题思路

仔细想一想会发现由于每一次都要从必胜状态去考虑,所以两位玩家在破坏宝藏外最后一层之前会把外围的所有石头都先消灭掉,即不到不得已,没有玩家会先去破坏宝藏外的最后一道屏障。那么得到宝藏外所有石头的HP之和,判断其奇偶便可知是先手胜还是后手胜。那么先由里向外bfs一次标记出宝藏外的最后一道屏障,再bfs一次由外向里搜,如果是外围石头则ans+=HP,如果是最后一道屏障ans+=(HP-1),要将最后一个留下。

代码实现

#include <iostream>#include<cstdio>#include<cstring>#include<queue>using namespace std;#define maxn 305int maps[maxn][maxn],ans;int m,n;bool visit1[maxn][maxn],visit2[maxn][maxn];struct node{    int x,y;}trea,now,ne;int dire[4][2]={{-1,0},{1,0},{0,-1},{0,1}};bool bfs1(){    queue<node>qu1;    qu1.push(trea);    visit1[trea.x][trea.y]=1;    while(!qu1.empty())    {        now=qu1.front();        qu1.pop();        for(int i=0;i<4;i++)        {            ne.x=now.x+dire[i][0];            ne.y=now.y+dire[i][1];            if((ne.x==m||ne.x==1||ne.y==n||ne.y==1)&&(maps[ne.x][ne.y]==0)) return 1;            if(visit1[ne.x][ne.y]) continue;            if(maps[ne.x][ne.y]==0)                qu1.push(ne);            visit1[ne.x][ne.y]=1;        }    }    return 0;}void bfs2(){    queue<node>qu2;    qu2.push(trea);    if(visit2[trea.x][trea.y]) return ;    visit2[trea.x][trea.y]=1;    while(!qu2.empty())    {        now=qu2.front();        qu2.pop();        ans+=maps[now.x][now.y];        for(int i=0;i<4;i++)        {            ne.x=now.x+dire[i][0];            ne.y=now.y+dire[i][1];            if(ne.x>m||ne.x<1||ne.y>n||ne.y<1)continue;            if(visit2[ne.x][ne.y]) continue;            if(visit1[ne.x][ne.y])            {                ans+=(maps[ne.x][ne.y]-1);                visit2[ne.x][ne.y]=1;                continue;            }            qu2.push(ne);            visit2[ne.x][ne.y]=1;        }    }}int main(){    while(~scanf("%d %d",&m,&n))    {        memset(visit1,0,sizeof(visit1));        memset(visit2,0,sizeof(visit2));        for(int i=1;i<=m;i++)        {            for(int j=1;j<=n;j++)            {                scanf("%d",&maps[i][j]);                if(maps[i][j]==-1)                {                    trea.x=i;                    trea.y=j;                }            }        }        if(bfs1())        {            printf("Ali Win\n");            continue;        }        ans=0;        for(int i=1;i<=n;i++)        {            trea.x=1,trea.y=i;            bfs2();            trea.x=m,trea.y=i;            bfs2();        }        for(int i=1;i<=m;i++)        {            trea.x=i,trea.y=1;            bfs2();            trea.x=i,trea.y=n;            bfs2();        }        if(ans%2==1)            printf("Ali Win\n");        else            printf("Baba Win\n");    }    return 0;}
原创粉丝点击