(HDU4101) Ali and Baba

Ali and Baba

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


 

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

题目大意:给一个n*m的maze,-1为宝藏,0为空地,大于0的数表示石头的hp,Ali先出手,每次可以攻击一个石头,之后石头hp减一,当石头hp=0时,当前位置变为空地,当从maze的边有一条空地到-1,则当前行动的人胜利。

有两种情况:1.一开始就有空地从边界直通-1,则Ali胜利。2.-1被包裹,则将包围圈最先打破的输。

对于第一种情况只要从-1向外bfs就可以了,若能搜到边界则Ali胜否则BaBa胜。

对于第二种情况我需要得到边界外的石头的HP和边界上各石头hp-1的和(因为要找最先敲破边界的人,俩人肯定先敲边界外的石头,当边界外的石头都被敲完人们才会考虑敲边界上的石头,当边界上的石头的hp都为1时,轮到谁敲边界谁输,若有石头的hp大于1,则俩人肯定都要敲边界石头hp大于1的,直到边界上的石头的hp都为一就知道胜负了)

所以终点是怎么找边界,这里用两个bfs两个bfs交叉的地方就是边界。

代码:(代码参考他人,忘了是谁的了)

#include<iostream>
#define maxn 405
using namespace std;
int n, m,ans;
int mp[maxn][maxn];
int vis[maxn][maxn], vis1[maxn][maxn];//vis标记从-1向外搜的情况,vis1标记从外向里搜边界的情况
int sx, sy;
int dx[] = { -1,1,0,0 };
int dy[] = { 0,0,-1,1 };
int cnt1, cnt2;
struct Node {
	int x, y;
}cur,now,q[maxn*maxn];



bool bfs1() {
	int i, 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];
			//可以到外面返回true代表ali胜
			if (tx<1 || tx>n || ty<1 || ty>m)return true;
			if (vis[tx][ty])continue;
			//对-1外包围的一层石头标记为2
			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 (int 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;
			//边界外的石头标记1
			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++)
		{
			//边界外的石头的hp
			if (!vis[i][j] && vis1[i][j])cnt1 += mp[i][j];
			//边界上的所有石头hp-1的和
			else if (vis[i][j] == 2 && vis1[i][j] == 2)cnt2 = cnt2 + mp[i][j] - 1;
		}
	}
	//偶数ali胜否则baba胜
	if ((cnt1 + cnt2) & 1)return true;
	return false;
}

int main() {
	int i, j;
	while (cin >> n >> m)
	{
		//数据输入
		for (int i = 1; i <= n; i++)
		{
			for (int j = 1; j <= m; j++)
			{
				cin >> mp[i][j];
				if (mp[i][j] == -1)sx = i, sy = j;
			}
		}
		//第一种情况
		if (bfs1())cout << "Ali Win" << endl;
		else {
		//第二种情况,偶数ali胜否则baba胜
			bfs2();
			if (solve())cout << "Ali Win" << endl;
			else cout << "Baba Win" << endl;
		}
	}
	return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值