HDU 4101 Ali and Baba

26 篇文章 0 订阅

Ali and Baba
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
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
2011 Alibaba-Cup Campus Contest
Recommend
lcy

//代码大概就是两遍BFS  还没调出来 明天再说 
#include<iostream>
#include<cstring>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
struct coordinate{ int x,y; }now,nex;
const int N = 505;
int a[N][N],vis[N][N],vis_2[N][N],n,m,ok,sum;
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};

inline int read(int &x){
    x=0; int f=1; char c=getchar();
    while(c>'9'||c<'0'){ if(c=='-') f=-1; c=getchar(); }
    while(c>='0'&&c<='9'){ x=x*10+c-'0'; c=getchar(); } x*=f;
}
inline bool Judge(coordinate p){
    if(p.x>=1&&p.x<=n&&p.y>=1&&p.y<=m) return true;
    else return false;
}

queue<coordinate> q;

void BFS(int x,int y){//先判断 Ali 能否直接从某个边界出 出发 得到宝藏
    memset(vis,0,sizeof vis ); now.x=x,now.y=y;
    q.push(now); vis[x][y]=1;
    while(!q.empty()){
        now=q.front(); q.pop();
        if(now.x==1||now.x==n||now.y==1||now.y==m){ ok=1; return ; }
        for(int i=0;i<4;i++){
            nex.x=now.x+dx[i],nex.y=now.y+dy[i];
            if(nex.x>=1&&nex.x<=n&&nex.y>=1&&nex.y<=m&&vis[nex.x][nex.y]==0){
                vis[nex.x][nex.y]=1;
                if(a[nex.x][nex.y]==0) q.push(nex);
            }
        }
    }
    return ;
}

void BFS_2(int x,int y){
    while(!q.empty()) q.pop();
    memset(vis_2,0,sizeof vis_2 );
    now.x=x,now.y=y; q.push(now);
    vis_2[x][y]=1;
    while(!q.empty()){
        now=q.front();q.pop();
        for(int i=0;i<4;++i){
            nex.x=now.x+dx[i],nex.y=now.y+dy[i];
            if(nex.x>=0&&nex.x<=n+1&&nex.y>=0&&nex.y<=m+1&&vis_2[nex.x][nex.y]==0&&vis_2[nex.x][nex.y]!=2){
                if(vis[nex.x][nex.y]==0){ vis_2[nex.x][nex.y]=1,q.push(nex); }
                if(vis[nex.x][nex.y]==1){ vis_2[nex.x][nex.y]=2,sum+=a[nex.x][nex.y]-1; }
            }
        }
    }
    for(int i=0;i<=n+1;++i)
        for(int j=0;j<=m;++j)
            if(vis_2[i][j]==1) sum+=a[i][j];
}

int main(){
    while(~scanf("%d%d",&n,&m)){
        int sx,sy;
        memset(a,0,sizeof a );
        for(int i=1;i<=n;++i)
            for(int j=1;j<=m;++j){
                scanf("%d",&a[i][j]);
                if(a[i][j]==-1) sx=i,sy=j;
            }
        ok=0,sum=0;
        BFS(sx,sy);
        if(ok){ printf("Ali Win\n");continue; }
        BFS_2(0,0);
        if(sum%2==0) printf("Baba Win\n");
        else printf("Ali Win\n");
    }
    return 0;
}
#include<iostream>
#include<cstring>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
struct coordinate{ int x,y; }now,nex;
const int N = 505;
int a[N][N],vis[N][N],vis_2[N][N],n,m,ok,sum;
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};

inline int read(int &x){
    x=0; int f=1; char c=getchar();
    while(c>'9'||c<'0'){ if(c=='-') f=-1; c=getchar(); }
    while(c>='0'&&c<='9'){ x=x*10+c-'0'; c=getchar(); } x*=f;
}

inline bool Judge(coordinate p){
    if(p.x>=1&&p.x<=n&&p.y>=1&&p.y<=m) return true;
    else return false;
}

void BFS(int x,int y){//先判断 Ali 能否直接从某个边界出 出发 得到宝藏
    queue<coordinate> q;
    memset(vis,0,sizeof vis ); now.x=x,now.y=y;
    q.push(now); vis[x][y]=1;
    while(!q.empty()){
        now=q.front(); 
        if(now.x==1||now.x==n||now.y==1||now.y==m){ ok=1; return ; }
        q.pop();
        for(int i=0;i<4;i++){
            nex.x=now.x+dx[i];nex.y=now.y+dy[i];
            if(nex.x>=1&&nex.x<=n&&nex.y>=1&&nex.y<=m&&vis[nex.x][nex.y]==0){
                vis[nex.x][nex.y]=1;
                if(a[nex.x][nex.y]==0) q.push(nex);
            }
        }
    }
    return ;
}

void BFS_2(int x,int y){
    queue<coordinate> q;
    while(!q.empty()) q.pop();
    memset(vis_2,0,sizeof vis_2 );
    now.x=x,now.y=y; q.push(now);
    vis_2[x][y]=1;
    while(!q.empty()){
        now=q.front();q.pop();
        for(int i=0;i<4;++i){
            nex.x=now.x+dx[i],nex.y=now.y+dy[i];
            x=nex.x,y=nex.y;
            if(nex.x>=0&&nex.x<=n+1&&nex.y>=0&&nex.y<=m+1&&vis_2[x][y]==0&&vis_2[nex.x][nex.y]!=2){
                if(vis[x][y]==0){ vis_2[x][y]=1,q.push(nex); }
                if(vis[x][y]==1){ vis_2[x][y]=2,sum+=a[x][y]-1; }
            }
        }
    }
    for(int i=0;i<=n+1;++i)
        for(int j=0;j<=m+1;++j)
            if(vis_2[i][j]==1) sum+=a[i][j];
}

int main(){
    while(~scanf("%d%d",&n,&m)){
        int sx,sy;
        memset(a,0,sizeof a );
        for(int i=1;i<=n;++i)
            for(int j=1;j<=m;++j){
                scanf("%d",&a[i][j]);
                if(a[i][j]==-1) sx=i,sy=j;
            }
        ok=0,sum=0;
        BFS(sx,sy);
        if(ok){ printf("Ali Win\n");continue; }
        BFS_2(0,0);
        if(sum%2==0) printf("Baba Win\n");
        else printf("Ali Win\n");
    }
    return 0;
}

这篇代码和上一篇没有什么区别,只是队列定义在了函数里面,但是这样确确实实能AC啊

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

七情六欲·

学生党不容易~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值