Ali and Baba
Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 1359 accepted submission (s): 287
Problem descriptionthere 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. 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.
Inputthe 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
Source2011 Alibaba-cup campus contest
Recommendlcy | we have carefully selected several similar problems for you: 4110 4102 4103 4104
To give a matrix, 0 indicates null to walk,-1 indicates the treasure position (only one), and other positive integers indicate the number of stones in the position. Two people, A and B, take turns playing games. A plays first. When it is a's turn, a wins if there is a path between the treasure and the outside. Assume that you cannot directly reach the position of the treasure. You can take a stone from the outside to a position. If there is no stone in the position, it becomes empty.
Idea: Find the circle surrounding the treasure, and then count the number of stones outside the circle (the circle turns to 1). If it is an odd number, Ali win; otherwise, Baba win
First, find the circle boundary from the treasure BFs. If it can reach the outside, Ali win. Otherwise, from the outside BFs, if it is a circle boundary, CNT + = n-1; otherwise, it is assumed to be a stone, all statistics. Then infer the parity;
Problems:
The idea was wrong at the beginning. The beginning was to directly count the number of stones in the circle, subtract them, and then infer the parity.
The nm range is incorrect. For example, 100
Because the second BFS is the first BFs to be copied, some errors have not been noticed !! For example, the NX result is X, resulting in WA for a long time.
#include <stdio.h>#include <iostream>#include <vector>#include <algorithm>#include <cstring>#include <string>#include <map>#include <cmath>#include <queue>#include <set>using namespace std;//#define WIN#ifdef WINtypedef __int64 LL;#define iform "%I64d"#define oform "%I64d\n"#define oform1 "%I64d"#elsetypedef long long LL;#define iform "%lld"#define oform "%lld\n"#define oform1 "%lld"#endif#define S64I(a) scanf(iform, &(a))#define P64I(a) printf(oform, (a))#define S64I1(a) scanf(iform1, &(a))#define P64I1(a) printf(oform1, (a))#define FOR(i, s, t) for(int (i)=(s); (i)<(t); (i)++)const int INF = 0x3f3f3f3f;const double eps = 10e-9;const double PI = (4.0*atan(1.0));const int maxn = 300 + 20;const int moveX[] = {-1, 1, 0, 0};const int moveY[] = {0, 0, -1, 1};int G[maxn][maxn];int TG[maxn][maxn];int vis[maxn][maxn];int n, m;struct Node { int x, y; Node(int xx=0, int yy=0) { x = xx; y = yy; }};queue<Node> Q;int bfs(int sx, int sy) { memset(TG, 0, sizeof(TG)); memset(vis, 0, sizeof(vis)); while(!Q.empty()) Q.pop(); Q.push(Node(sx, sy)); vis[sx][sy] = 1; int res = 0; while(!Q.empty()) { int x = Q.front().x; int y = Q.front().y; Q.pop(); for(int i=0; i<4; i++) { int nx = x + moveX[i]; int ny = y + moveY[i]; if(nx < 1 || nx > n || ny < 1 || ny > m) return 1; if(vis[nx][ny]) continue; vis[nx][ny] = 1; if(G[nx][ny] > 0) {TG[nx][ny] = 1;} else Q.push(Node(nx, ny)); } } return 0;}int bfs1(int sx, int sy) { memset(vis, 0, sizeof(vis)); while(!Q.empty()) Q.pop(); Q.push(Node(sx, sy)); vis[sx][sy] = 1; int res = 0; while(!Q.empty()) { int x = Q.front().x; int y = Q.front().y; Q.pop(); for(int i=0; i<4; i++) { int nx = x + moveX[i]; int ny = y + moveY[i]; if(nx < 0 || nx > n+1 || ny < 0 || ny > m+1) continue; if(vis[nx][ny]) continue; vis[nx][ny] = 1; if(TG[nx][ny] == 1) { res += G[nx][ny] - 1; //printf("%d %d -> %d = %d\n", nx, ny, G[nx][ny]-1, res); } else { Q.push(Node(nx, ny)); if(G[nx][ny] > 0) res += G[nx][ny]; //if(G[nx][ny] > 0) printf("%d %d -> %d = %d\n", nx, ny, G[nx][ny], res); } } } return res;}int main() { while(scanf("%d%d", &n, &m) != EOF) { if(!n || !m) while(1); /*if(!n || !m) { puts("Ali Win"); continue; }*/ int sum = 0; int sx, sy; memset(G, 0, sizeof(G)); for(int i=1; i<=n; i++) { for(int j=1; j<=m; j++) { scanf("%d", &G[i][j]); if(G[i][j] > 0) sum += G[i][j]; if(G[i][j] == -1) { sx = i; sy = j; } } } int t = bfs(sx, sy); if(t) { puts("Ali Win"); continue; } t = bfs1(0, 0); //printf("-->%d\n", t); if(t&1) puts("Ali Win"); else puts("Baba Win"); } return 0;}