首页 > 其他分享 >HDU 4101 Ali and Baba

HDU 4101 Ali and Baba

时间:2022-11-09 20:38:01浏览次数:52  
标签:Baba HDU Ali int rep stone 4101 mp


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;
}




标签:Baba,HDU,Ali,int,rep,stone,4101,mp
From: https://blog.51cto.com/u_15870896/5838726

相关文章

  • HDU 4198 Quick out of the Harbour
    ProblemDescriptionCaptainClearbearddecidedtogototheharbourforafewdayssohiscrewcouldinspectandrepairtheship.Now,afewdayslater,......
  • HDU 2589 正方形划分
    ProblemDescription一个边长为L的正方形可以分成L*L个小正方形.有N个石子放在N个小正方形里,能否将它分成N个正方形,使得每个正方形里恰有一个石子且这N个正方......
  • HDU 3085 Nightmare Ⅱ
    ProblemDescriptionLastnight,littleerriyuehadahorriblenightmare.Hedreamedthatheandhisgirlfriendweretrappedinabigmazeseparately.Mo......
  • HDU 3313 Key Vertex
    ProblemDescriptionYouneedwalkingfromvertexStovertexTinagraph.IfyouremoveonevertexwhichstopsyoufromwalkingfromStoT,thatverte......
  • HDU 3316 Mine sweeping
    ProblemDescriptionIthinkmostofyouareusingsystemnamedofxporvistaorwin7.Andthesesystemisconsistofafamousgamewhatisminesweeping......
  • HDU 3355 Hop — Don’t Walk!
    ProblemDescriptionKERMITTHEFROGisaclassicvideogamewithasimplecontrolandobjectivebutrequiresagooddealofthinking.Youcontrolanani......
  • HDU 3427 Clickomania
    ProblemDescriptionClickomaniaisapuzzleinwhichonestartswitharectangulargridofcellsofdifferentcolours.Ineachstep,aplayerselects("......
  • HDU 2209 翻纸牌游戏
    ProblemDescription有一种纸牌游戏,很有意思,给你N张纸牌,一字排开,纸牌有正反两面,开始的纸牌可能是一种乱的状态(有些朝正,有些朝反),现在你需要整理这些纸牌。但是麻烦的是,......
  • HDU 2216 Game III
    ProblemDescriptionZjtandSarawilltakepartinagame,namedGameIII.ZjtandSarawillbeinamaze,andZjtmustfindSara.Therearesomestrang......
  • HDU 1010 Tempter of the Bone
    DescriptionThedoggiefoundaboneinanancientmaze,whichfascinatedhimalot.However,whenhepickeditup,themazebegantoshake,andthedoggie......