hdu 4101 Ali and Baba 求包含-1的外围

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

 

//

 

#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
*/

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值