zoukankan      html  css  js  c++  java
  • hdu 4101

    Ali and Baba

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


    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
     

    7 7
    0 2 1 1 1 1 1
    1 0 0 0 0 0 1
    1 0 2 2 1 1 1
    1 0 0 0 0 0 1
    1 0 -1 5 3 4 1
    1 0 0 0 0 0 1
    1 1 1 1 1 1 1

    此时ans = 9   Ali Win.

    写得很挫,其实思路对的话,写起来很简单的

      1 #include<iostream>
      2 #include<stdio.h>
      3 #include<cstring>
      4 #include<queue>
      5 #include<cstdlib>
      6 using namespace std;
      7 
      8 int n,m;
      9 int a[302][302];
     10 bool use[302][302];
     11 bool hash[302][302];
     12 
     13 int map1[4][2]={ {1,0},{0,1},{-1,0},{0,-1}};
     14 struct node
     15 {
     16     int x,y;
     17 };
     18 queue<node>Q;
     19 bool pd()
     20 {
     21     int i;
     22     for(i=1;i<=m;i++)
     23         if(use[1][i]==true) return true;
     24     for(i=1;i<=m;i++)
     25         if(use[n][i]==true) return true;
     26 
     27     for(i=1;i<=n;i++)
     28         if(use[i][1]==true) return true;
     29     for(i=1;i<=n;i++)
     30         if(use[i][m]==true) return true;
     31     return false;
     32 }
     33 void bfs(int x,int y)
     34 {
     35     int i;
     36     node t,cur;
     37     t.x=x;
     38     t.y=y;
     39     Q.push(t);
     40     use[x][y]=true;
     41 
     42     while(!Q.empty())
     43     {
     44         cur=Q.front();
     45         Q.pop();
     46 
     47         for(i=0;i<4;i++)
     48         {
     49             t=cur;
     50             t.x=t.x+map1[i][0];
     51             t.y=t.y+map1[i][1];
     52             if(t.x>=1&&t.x<=n && t.y>=1&&t.y<=m)
     53             {
     54                 if(use[t.x][t.y]==false && a[t.x][t.y]==0)
     55                 {
     56                     use[t.x][t.y]=true;
     57                     Q.push(t);
     58                 }
     59             }
     60         }
     61     }
     62 }
     63 void dbfs(int x,int y)//second serch
     64 {
     65     int i;
     66     node t,cur;
     67     bool flag;
     68     t.x=x;
     69     t.y=y;
     70     hash[x][y]=true;
     71     Q.push(t);
     72 
     73     while(!Q.empty())
     74     {
     75         cur=Q.front();
     76         Q.pop();
     77         flag=false;
     78         for(i=0;i<4;i++)
     79         {
     80             t=cur;
     81             t.x=t.x+map1[i][0];
     82             t.y=t.y+map1[i][1];
     83             if(t.x>=1&&t.x<=n && t.y>=1&&t.y<=m)
     84             {
     85                if(use[t.x][t.y]==true)
     86                {
     87                    flag=true;
     88                    break;
     89                }
     90             }
     91         }
     92         if(flag==true) continue;
     93         for(i=0;i<4;i++)
     94         {
     95             t=cur;
     96             t.x=t.x+map1[i][0];
     97             t.y=t.y+map1[i][1];
     98             if(t.x>=1&&t.x<=n && t.y>=1&&t.y<=m)
     99             {
    100                 if(hash[t.x][t.y]==false && use[t.x][t.y]==false)
    101                 {
    102                     hash[t.x][t.y]=true;
    103                     Q.push(t);
    104                 }
    105             }
    106         }
    107     }
    108 }
    109 void s_serch()
    110 {
    111     int i;
    112     while(!Q.empty())
    113     {
    114         Q.pop();
    115     }
    116     for(i=1;i<=m;i++)
    117         if(hash[1][i]==false && use[1][i]==false)
    118             dbfs(1,i);
    119     for(i=1;i<=m;i++)
    120         if(hash[n][i]==false && use[n][i]==false)
    121             dbfs(n,i);
    122 
    123     for(i=1;i<=n;i++)
    124         if(hash[i][1]==false && use[i][1]==false)
    125             dbfs(i,1);
    126     for(i=1;i<=n;i++)
    127         if(hash[i][m]==false && use[i][m]==false)
    128             dbfs(i,m);
    129 }
    130 int main()
    131 {
    132     int i,j,x,y,cur,k;
    133     bool flag;
    134     while(scanf("%d%d",&n,&m)>0)
    135     {
    136         memset(hash,false,sizeof(hash));
    137         memset(use,false,sizeof(use));
    138         for(i=1;i<=n;i++)
    139             for(j=1;j<=m;j++)
    140             {
    141                 scanf("%d",&a[i][j]);
    142             }
    143         while(!Q.empty())
    144         {
    145             Q.pop();
    146         }
    147         for(i=1;i<=n;i++)
    148             for(j=1;j<=m;j++)
    149             {
    150                 if(a[i][j]==-1 && use[i][j]==false)
    151                     bfs(i,j);//first serch.
    152             }
    153         if(pd()==true){
    154             printf("Ali Win
    ");
    155             continue;
    156         }
    157         s_serch();
    158         for(i=1,cur=0;i<=n;i++)
    159             for(j=1;j<=m;j++)
    160             {
    161                 if(hash[i][j]==true && a[i][j]>0)
    162                 {
    163                     flag=false;
    164                     for(k=0;k<4;k++)
    165                     {
    166                         x=i+map1[k][0];
    167                         y=j+map1[k][1];
    168                         if(x>=1&&x<=n &&y>=1&&y<=m  && use[x][y]==true)
    169                         {
    170                             flag=true;
    171                             break;
    172                         }
    173                     }
    174                     if(flag==true) cur=cur+a[i][j]-1;
    175                     else cur=cur+a[i][j];
    176                 }
    177             }
    178     //    printf("%d
    ",cur);
    179         cur=cur%2;
    180         if(cur==1) printf("Ali Win
    ");
    181         else printf("Baba Win
    ");
    182     }
    183     return 0;
    184 }
     
  • 相关阅读:
    HCIA_R&S-学习_Day05(PPPoE、DHCP、ACL、NAT)
    HCIA_R&S-学习_Day04(链路状态协议OSPF & PPP)
    HCIA_R&S-学习_Day03(路由协议基础与实现)
    【详谈 Delta Lake 】系列技术专题 之 湖仓一体( Lakehouse )
    工作7年,我的10条经验总结
    Hologres揭秘:优化COPY,批量导入性能提升5倍+
    谈谈JVM内部锁升级过程
    如何帮用户管好云账本?阿里云数据库助力收钱吧 | 甲子光年
    重磅 | 数据库自治服务DAS论文入选全球顶会SIGMOD,领航“数据库自动驾驶”新时代
    同程旅行基于 RocketMQ 高可用架构实践
  • 原文地址:https://www.cnblogs.com/tom987690183/p/3659736.html
Copyright © 2011-2022 走看看