码迷,mamicode.com
首页 > 其他好文 > 详细

(bfs+dfs) hdu 4101

时间:2015-03-21 18:39:03      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:

Ali and Baba

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


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
 

 

Source
 

 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<vector>
#include<stack>
using namespace std;
int n,m,map[305][305],sx,sy,vis[305][305],ans,mark[305][305];
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
bool check(int x,int y)
{
    if(x<0||y<0||x>=n||y>=m)
        return false;
    return true;
}
bool dfs(int x,int y)
{
    int xx,yy;
    if(x==0||y==0||x==n-1||y==m-1)
        return true;
    vis[x][y]=1;
    for(int i=0;i<4;i++)
    {
        xx=x+dx[i],yy=y+dy[i];
        if(!check(xx,yy)||vis[xx][yy]) continue;
        vis[xx][yy]=1;
        if(map[xx][yy]>0) continue;
        if(dfs(xx,yy)) return true;
    }
    return false;
}
void bfs(int sx,int sy)
{
    int xx,yy,x,y;
    queue<int> q;
    q.push(sx),q.push(sy);
    while(!q.empty())
    {
        x=q.front(),q.pop();
        y=q.front(),q.pop();
        for(int i=0;i<4;i++)
        {
            xx=x+dx[i],yy=y+dy[i];
            if(!check(xx,yy)) continue;
            if(mark[xx][yy]) continue;
            mark[xx][yy]=1;
            if(vis[xx][yy])
                ans+=map[xx][yy]-1;
            else
            {
                ans+=map[xx][yy];
                q.push(xx),q.push(yy);
            }
        }
    }
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        ans=0;
        memset(vis,0,sizeof(vis));
        memset(mark,0,sizeof(mark));
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
            {
                scanf("%d",&map[i][j]);
                if(map[i][j]==-1)
                    sx=i,sy=j;
            }
        if(dfs(sx,sy))
        {
            printf("Ali Win\n");
            continue;
        }
        else
        {
            for(int i=0;i<n;i++)
            {
                for(int j=0;j<m;j++)
                {
                    if(!mark[i][j])
                    {
                        if(i==0||j==0||i==n-1||j==m-1)
                        {
                            mark[i][j]=1;
                            if(vis[i][j])
                                ans+=map[i][j]-1;
                            else
                                ans+=map[i][j],bfs(i,j);
                        }
                    }
                }
            }
        }
        if(ans&1)
            printf("Ali Win\n");
        else
            printf("Baba Win\n");
    }
    return 0;
}

  

(bfs+dfs) hdu 4101

标签:

原文地址:http://www.cnblogs.com/a972290869/p/4355901.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!