zoukankan      html  css  js  c++  java
  • HDU-1495 非常可乐(BFS)

       广搜的灵活应用题:

                           非常可乐

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

    Problem Description
    大家一定觉的运动以后喝可乐是一件很惬意的事情,但是seeyou却不这么认为。因为每次当seeyou买了可乐以后,阿牛就要求和seeyou一起分享这一瓶可乐,而且一定要喝的和seeyou一样多。但seeyou的手中只有两个杯子,它们的容量分别是N 毫升和M 毫升 可乐的体积为S (S<101)毫升 (正好装满一瓶) ,它们三个之间可以相互倒可乐 (都是没有刻度的,且 S==N+M,101>S>0,N>0,M>0) 。聪明的ACMER你们说他们能平分吗?如果能请输出倒可乐的最少的次数,如果不能输出"NO"。
     
    Input
    三个整数 : S 可乐的体积 , N 和 M是两个杯子的容量,以"0 0 0"结束。
     
    Output
    如果能平分的话请输出最少要倒的次数,否则输出"NO"。
     
    Sample Input
    7 4 3
    4 1 3
    0 0 0
     
    Sample Output
    NO 3
      1 //一共6种情况;all->big,all->small,big->small,big->all,small->all,small->big进行bfs搜索,如果未被搜到过,
      2 //就入队列,如果出现两个瓶子里水相同就返回输出。(总水量为奇数时无解)
      3 #include<stdio.h>  
      4 #include<string.h>  
      5 #include<queue>  
      6 using namespace std;  
      7 struct node  
      8 {  
      9     int s,n,m;  
     10     int step;  
     11 };  
     12 int hash[101][101][101];  
     13 int s,n,m;  
     14   
     15 int BFS()  
     16 {  
     17     queue<node>q;  
     18     node cur,next;  
     19      cur.s=s;    
     20     cur.n=0;  
     21     cur.m=0;  
     22     cur.step=0;  
     23     hash[cur.s][cur.n][cur.m]=1;  
     24     q.push(cur);  
     25     while(!q.empty())  
     26     {  
     27         cur=q.front();  
     28         q.pop();  
     29         if((cur.n==0 && cur.m==cur.s)||(cur.m==0 && cur.n==cur.s)||( cur.s==0 && cur.n==cur.m)) return cur.step;  
     30         if(cur.n!=0)  
     31         {  
     32             if(cur.n>s-cur.s)  
     33             {  
     34                 next.n=cur.n-(s-cur.s);  
     35                 next.s=s;  
     36                 next.m=cur.m;  
     37                 if(hash[next.s][next.n][next.m]==0)  
     38                 {  
     39                     next.step=cur.step+1;  
     40                     q.push(next);  
     41                     hash[next.s][next.n][next.m]=1;  
     42                 }  
     43             }  
     44             else  
     45             {  
     46                 next.n=0;  
     47                 next.s=cur.s+cur.n;  
     48                 next.m=cur.m;  
     49                 if(hash[next.s][next.n][next.m]==0)  
     50                 {  
     51                     next.step=cur.step+1;  
     52                     q.push(next);  
     53                     hash[next.s][next.n][next.m]=1;  
     54                 }  
     55             }  
     56             if(cur.n>m-cur.m)  
     57             {  
     58                 next.n=cur.n-(m-cur.m);  
     59                 next.m=m;  
     60                 next.s=cur.s;  
     61                 if(hash[next.s][next.n][next.m]==0)  
     62                 {  
     63                     next.step=cur.step+1;  
     64                     q.push(next);  
     65                     hash[next.s][next.n][next.m]=1;  
     66                 }  
     67             }  
     68             else  
     69             {  
     70                 next.n=0;  
     71                 next.m=cur.m+cur.n;  
     72                 next.s=cur.s;  
     73                 if(hash[next.s][next.n][next.m]==0)  
     74                 {  
     75                     next.step=cur.step+1;  
     76                     q.push(next);  
     77                     hash[next.s][next.n][next.m]=1;  
     78                 }  
     79             }  
     80         }  
     81   
     82         if(cur.m)  
     83         {  
     84             if(cur.m>s-cur.s)  
     85             {  
     86                 next.m=cur.n-(s-cur.s);  
     87                 next.s=s;  
     88                 next.n=cur.n;  
     89                 if(hash[next.s][next.n][next.m]==0)  
     90                 {  
     91                     next.step=cur.step+1;  
     92                     q.push(next);  
     93                     hash[next.s][next.n][next.m]=1;  
     94                 }  
     95             }  
     96             else  
     97             {  
     98                 next.m=0;  
     99                 next.s=cur.s+cur.m;  
    100                 next.n=cur.n;  
    101                 if(hash[next.s][next.n][next.m]==0)  
    102                 {  
    103                     next.step=cur.step+1;  
    104                     q.push(next);  
    105                     hash[next.s][next.n][next.m]=1;  
    106                 }  
    107             }  
    108             if(cur.m>n-cur.n)  
    109             {  
    110                 next.m=cur.m-(n-cur.n);  
    111                 next.n=n;  
    112                 next.s=cur.s;  
    113                 if(hash[next.s][next.n][next.m]==0)  
    114                 {  
    115                     next.step=cur.step+1;  
    116                     q.push(next);  
    117                     hash[next.s][next.n][next.m]=1;  
    118                 }  
    119             }  
    120             else  
    121             {  
    122                 next.m=0;  
    123                 next.n=cur.m+cur.n;  
    124                 next.s=cur.s;  
    125                 if(hash[next.s][next.n][next.m]==0)  
    126                 {  
    127                     next.step=cur.step+1;  
    128                     q.push(next);  
    129                     hash[next.s][next.n][next.m]=1;  
    130                 }  
    131             }  
    132         }  
    133   
    134         if(cur.s)  
    135         {  
    136             if(cur.s>n-cur.n)  
    137             {  
    138                 next.s=cur.s-(n-cur.n);  
    139                 next.n=n;  
    140                 next.m=cur.m;  
    141                 if(hash[next.s][next.n][next.m]==0)  
    142                 {  
    143                     next.step=cur.step+1;  
    144                     q.push(next);  
    145                     hash[next.s][next.n][next.m]=1;  
    146                 }  
    147             }  
    148             else  
    149             {  
    150                 next.s=0;  
    151                 next.n=cur.s+cur.n;  
    152                 next.m=cur.m;  
    153                 if(hash[next.s][next.n][next.m]==0)  
    154                 {  
    155                     next.step=cur.step+1;  
    156                     q.push(next);  
    157                     hash[next.s][next.n][next.m]=1;  
    158                 }  
    159             }  
    160             if(cur.s>m-cur.m)  
    161             {  
    162                 next.s=cur.s-(m-cur.m);  
    163                 next.m=m;  
    164                 next.n=cur.n;  
    165                 if(hash[next.s][next.n][next.m]==0)  
    166                 {  
    167                     next.step=cur.step+1;  
    168                     q.push(next);  
    169                     hash[next.s][next.n][next.m]=1;  
    170                 }  
    171             }  
    172             else  
    173             {  
    174                 next.s=0;  
    175                 next.m=cur.m+cur.s;  
    176                 next.n=cur.n;  
    177                 if(hash[next.s][next.n][next.m]==0)  
    178                 {  
    179                     next.step=cur.step+1;  
    180                     q.push(next);  
    181                     hash[next.s][next.n][next.m]=1;  
    182                 }  
    183             }  
    184         }  
    185     }  
    186   
    187     return 0;  
    188 }  
    189 int main()  
    190 {  
    191     int ans;  
    192     while(scanf("%d%d%d",&s,&n,&m),s||n||m)  
    193     {  
    194         memset(hash,0,sizeof(hash));  
    195   
    196       if(s%2) {printf("NO
    ");continue;}             
    197   
    198         ans=BFS();  
    199         if(ans) printf("%d
    ",ans);  
    200         else    printf("NO
    ");  
    201     }  
    202     return 0;  
    203 }  
  • 相关阅读:
    VBS进程判断
    [转]Myeclipse的使用方法格式化源代码
    Stay hungry,stay foolish
    【转】tomcat 设置 session 过期时间
    【转】Java编码转换问题,new String(str.getByts("ISO88591"), "GBK")的作用
    【转】VBS CHR码值对应列表
    【转】 从MS SQL Server 2000升级到MS SQL Server 2005方法
    【转】SqlServer中decimal(numeric )、float 和 real 数据类型的区别
    html radio
    a标签点击状态
  • 原文地址:https://www.cnblogs.com/cancangood/p/3265726.html
Copyright © 2011-2022 走看看