hdu 4101 Ali and Baba (bfs+严密思维)

Ali and Baba

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 971    Accepted Submission(s): 225


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
 

ps:这题很锻炼人的细心能力、思维严密性,好题。

感想:
开始看到这题,感觉从宝藏出发一次bfs就能搞定,果断WA了,怎么也想不通,看到男神博客才知道哪里错了。

思路:
宝藏出发一次bfs,若能碰到边界,则肯定是Ali Win,不能则将其能碰到的墙标记上(这些墙的一部分构成一个环包围这宝藏),再从边界出发往里bfs一次。。。恩 思路全讲就没意思了, WA的看我贴的数据吧。

代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#define maxn 405
using namespace std;

int n,m,ans;
int cnt1,cnt2;
int sx,sy;
int dx[]= {-1,1,0,0};
int dy[]= {0,0,-1,1};
int mp[maxn][maxn];
int vis[maxn][maxn],vis1[maxn][maxn];
struct Node
{
    int x,y;
} cur,now,q[maxn*maxn];

bool bfs1()
{
    int i,j,nx,ny,tx,ty;
    int head=0,tail=-1;
    memset(vis,0,sizeof(vis));
    cur.x=sx;
    cur.y=sy;
    vis[sx][sy]=1;
    q[++tail]=cur;
    while(head<=tail)
    {
        now=q[head];
        head++;
        nx=now.x;
        ny=now.y;
        for(i=0; i<4; i++)
        {
            tx=nx+dx[i];
            ty=ny+dy[i];
            if(tx<1||tx>n||ty<1||ty>m) return true ;
            if(vis[tx][ty]) continue ;
            if(mp[tx][ty])
            {
                vis[tx][ty]=2;
                continue ;
            }
            cur.x=tx;
            cur.y=ty;
            vis[tx][ty]=1;
            q[++tail]=cur;
        }
    }
    return false ;
}
void bfs2()
{
    int i,j,nx,ny,tx,ty;
    int head=0,tail=-1;
    memset(vis1,0,sizeof(vis1));
    for(i=0;i<=n+1;i++)
    {
        cur.x=i;
        cur.y=0;
        vis1[i][0]=1;
        q[++tail]=cur;
        cur.y=m+1;
        vis1[i][m+1]=1;
        q[++tail]=cur;
    }
    for(j=1;j<=m;j++)
    {
        cur.x=0;
        cur.y=j;
        vis1[0][j]=1;
        q[++tail]=cur;
        cur.x=n+1;
        vis1[n+1][j]=1;
        q[++tail]=cur;
    }
    while(head<=tail)
    {
        now=q[head];
        head++;
        nx=now.x;
        ny=now.y;
        for(i=0; i<4; i++)
        {
            tx=nx+dx[i];
            ty=ny+dy[i];
            if(tx<0||tx>n+1||ty<0||ty>m+1||vis1[tx][ty]) continue ;
            if(vis[tx][ty]==2)
            {
                vis1[tx][ty]=2;
                continue ;
            }
            cur.x=tx;
            cur.y=ty;
            vis1[tx][ty]=1;
            q[++tail]=cur;
        }
    }
}
bool solve()
{
    int i,j;
    cnt1=cnt2=0;
    for(i=1; i<=n; i++)
    {
        for(j=1; j<=m; j++)
        {
            if(!vis[i][j]&&vis1[i][j]) cnt1+=mp[i][j];// 从里到外访问不到且从外到里能访问到
            else if(vis[i][j]==2&&vis1[i][j]==2) cnt2=cnt2+mp[i][j]-1;  // 环
        }
    }
    printf("%d\n",cnt1+cnt2);
    if((cnt1+cnt2)&1) return true ;
    return false ;
}
int main()
{
    int i,j;
    while(~scanf("%d%d",&n,&m))
    {
        for(i=1; i<=n; i++)
        {
            for(j=1; j<=m; j++)
            {
                scanf("%d",&mp[i][j]);
                if(mp[i][j]==-1) sx=i,sy=j;
            }
        }
        if(bfs1()) printf("Ali Win\n");
        else
        {
            bfs2();
            if(solve()) printf("Ali Win\n");
            else printf("Baba Win\n");
        }
    }
    return 0;
}
/*
环中有数
6 7
1  1  1  1  1  1  1
1  0  0  0  0  0  1
1  0  3  5  1  1  1
1  0  -1 4  0  1  1
1  0  1  0  0  1  1
1  1  1  1  1  1  1

环中有环
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

ans:
16
6
*/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值