天天看點

HDU 4101 Ali and Baba

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      

巧妙的題,關鍵在于算出包住-1的外圍那一圈的石頭,兩次dfs搞定

#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define rep(i,j,k) for (int i = j; i <= k; i++)
const int N = 310;
int n, m, tot;
int a[4] = { 0,0,1,-1 };
int b[4] = { 1,-1,0,0 };
int mp[N][N], f[N][N];

bool dfs(int x, int y)
{
    f[x][y] = 1;
    rep(i, 0, 3)
    {
        int X = x + a[i], Y = y + b[i];
        if (X<1 || X>n || Y<1 || Y>m) return true;
        if (f[X][Y]) continue;
        f[X][Y] = 1;
        if (!mp[X][Y] && dfs(X, Y)) return true;
    }
    return false;
}

void Dfs(int x, int y)
{
    f[x][y] = 2;
    rep(i, 0, 3)
    {
        int X = x + a[i], Y = y + b[i];
        if (X<0 || X>n + 1 || Y<0 || Y>m + 1) continue;
        if (f[X][Y] == 2) continue;
        if (f[X][Y] == 0)
        {
            f[X][Y] = 2;
            Dfs(X, Y);
            tot += mp[X][Y];
        }
        else 
        {
            f[X][Y] = 2;
            tot += mp[X][Y] - 1;
        }
    }
}

int main()
{
    while (~scanf("%d%d", &n, &m))
    {
        tot = 0;
        memset(mp, 0, sizeof(mp));
        memset(f, 0, sizeof(f));
        rep(i, 1, n) rep(j, 1, m) scanf("%d", &mp[i][j]);
        rep(i, 1, n) rep(j, 1, m)
        {
            if (mp[i][j] == -1)
            {
                if (dfs(i, j)) printf("Ali Win\n");
                else
                {
                    tot = -1;     Dfs(0, 0);
                    printf("%s", (tot & 1) ? "Baba Win\n" : "Ali Win\n");
                }
            }
        }
    }
    return 0;
}