题意就不说了,解法是:
首先求出与宝藏通过空地相连的所有区域,表示为A;
然后我们只需求出将 A的外围一圈所有石子都变为1 和 A的外围之外的石子变为0 所需的步数,然后比较是基数还是偶数就可知道谁赢谁输。(因为两个人会在敲破围绕宝藏的那层石头前一直磨叽把其他能打到的石头全部敲掉~~~~)
AC代码如下:
#include <iostream>
#include <queue>
#include <cstring>
#include <cstdio>
using namespace std;
typedef struct{
int x, y;
}Node;
bool visit[301][301];
int map[301][301];
bool treasure[301][301];//宝藏的势力范围~~~~~~
int moves[][2] = { 1, 0, -1, 0, 0, 1, 0, -1 };
int N, M;
int sumstep;
Node Trea;
Node temp;
void bfs( int x, int y ){
queue<Node> q;
Node start;
start.x = x;
start.y = y;
q.push( start );
while( !q.empty() ){
Node n = q.front();
q.pop();
for( int i = 0; i < 4; i++ ){
temp.x = n.x + moves[i][0];
temp.y = n.y + moves[i][1];
if( temp.x < 0 || temp.x >= N || temp.y < 0 || temp.y >= M ){
continue;
}
if( visit[temp.x][temp.y] ){
continue;
}
visit[temp.x][temp.y] = true;
if( treasure[temp.x][temp.y] ){
sumstep += map[temp.x][temp.y] - 1;
}else{
sumstep += map[temp.x][temp.y];
q.push( temp );
}
}
}
}
bool dfs( int x, int y ){
if( x == 0 || x == N - 1 || y == 0 || y == M - 1 ){
return true;
}
for( int i = 0; i < 4; i++ ){
temp.x = x + moves[i][0];
temp.y = y + moves[i][1];
if( temp.x < 0 || temp.x >= N || temp.y < 0 || temp.y >= M ){
continue;
}
if( treasure[temp.x][temp.y] ){
continue;
}
treasure[temp.x][temp.y] = true;
if( map[temp.x][temp.y] ){
continue;
}
if( dfs( temp.x, temp.y ) ){
return true;
}
}
return false;
}
int main(){
while( scanf( "%d%d", &N, &M ) != EOF ){
for( int i = 0; i < N; i++ ){
for( int j = 0; j < M; j++ ){
cin >> map[i][j];
if( map[i][j] == -1 ){
Trea.x = i;
Trea.y = j;
}
}
}
memset( treasure, false, sizeof( treasure ) );
memset( visit, false, sizeof( visit ) );
sumstep = 0;
if( dfs( Trea.x, Trea.y ) ){
cout << "Ali Win" << endl;
continue;
}else{
for( int i = 0; i < N; i++ ){
for( int j = 0; j < M; j++ ){
if( visit[i][j] ){
continue;
}
if( i == 0 || i == N - 1 || j == 0 || j == M - 1 ){
visit[i][j] = true;
if( treasure[i][j] ){
sumstep += map[i][j] - 1;
}else{
sumstep += map[i][j];
bfs( i, j );
}
}
}
}
}
if( sumstep % 2 ){
puts( "Ali Win" );
}else{
puts( "Baba Win" );
}
}
return 0;
}