• HDU 1495


    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1495

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

    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
     
     
    把每次三个瓶子的状态作为一个node,进行BFS。
    BFS的过程不难,但是可以说在没有经验的情况下,要想到这道题用BFS还是有难度的。
      1 #include<cstdio>
      2 #include<cstring>
      3 #include<queue>
      4 using namespace std;
      5 struct Node{
      6     int a,b,c,step;//a,b,c三个瓶子对应S,N,M 
      7 }S,now,nex;
      8 int s,n,m;
      9 bool vis[101][101][101];
     10 int bfs()
     11 {
     12     queue<Node> q;
     13     q.push(S);
     14     vis[S.a][S.b][S.c]=1;
     15     while(!q.empty())
     16     {
     17         now=q.front(); q.pop();
     18         if(2*now.a==s && (now.a==now.b || now.a==now.c)) return now.step;
     19         if(2*now.b==s && (now.b==now.a || now.b==now.c)) return now.step;
     20         if(2*now.c==s && (now.c==now.b || now.c==now.a)) return now.step;
     21         nex.step=now.step+1;
     22         //a->b:
     23         if(now.b<n)
     24         {
     25             nex.c=now.c;
     26             if(now.a >= n-now.b) nex.a=now.a-(n-now.b), nex.b=n;
     27             else nex.a=0, nex.b=now.b+now.a;
     28             if(!vis[nex.a][nex.b][nex.c])
     29             {
     30                 q.push(nex);
     31                 vis[nex.a][nex.b][nex.c]=1;
     32             }
     33         }
     34         
     35         //a->c:
     36         if(now.c<m)
     37         {
     38              nex.b=now.b;
     39             if(now.a >= m-now.c) nex.a=now.a-(m-now.c), nex.c=m;
     40             else nex.a=0, nex.c=now.c+now.a;
     41             if(!vis[nex.a][nex.b][nex.c])
     42             {
     43                 q.push(nex);
     44                 vis[nex.a][nex.b][nex.c]=1;
     45             }
     46         }
     47         
     48         //b->c:
     49         if(now.c<m)
     50         {
     51             nex.a=now.a;
     52             if(now.b >= m-now.c) nex.b=now.b-(m-now.c), nex.c=m;
     53             else nex.b=0, nex.c=now.c+now.b;
     54             if(!vis[nex.a][nex.b][nex.c])
     55             {
     56                 q.push(nex);
     57                 vis[nex.a][nex.b][nex.c]=1;
     58             }
     59         }
     60         
     61         //b->a:
     62         if(now.a<s)
     63         {
     64              nex.c=now.c;
     65             if(now.b >= s-now.a) nex.a=s, nex.b=now.b-(s-now.a);
     66             else nex.a=now.a+now.b, nex.b=0;
     67             if(!vis[nex.a][nex.b][nex.c])
     68             {
     69                 q.push(nex);
     70                 vis[nex.a][nex.b][nex.c]=1;
     71             }
     72         }
     73         
     74         //c->a:
     75         if(now.a<s)
     76         {
     77              nex.b=now.b;
     78             if(now.c >= s-now.a) nex.a=s, nex.c=now.c-(s-now.a);
     79             else nex.a=now.a+now.c, nex.c=0;
     80             if(!vis[nex.a][nex.b][nex.c])
     81             {
     82                 q.push(nex);
     83                 vis[nex.a][nex.b][nex.c]=1;
     84             }
     85         }
     86         
     87         //c->b:
     88         if(now.b<n)
     89         {
     90              nex.a=now.a;
     91             if(now.c >= n-now.b) nex.b=n, nex.c=now.c-(n-now.b);
     92             else nex.b=now.b+now.c, nex.c=0;
     93             if(!vis[nex.a][nex.b][nex.c])
     94             {
     95                 q.push(nex);
     96                 vis[nex.a][nex.b][nex.c]=1;
     97             }
     98         }
     99     }
    100     return -1;
    101 }
    102 int main()
    103 {
    104     while(scanf("%d%d%d",&s,&n,&m) && s!=0)
    105     {
    106         S.a=s, S.b=0, S.c=0, S.step=0;
    107         memset(vis,0,sizeof(vis));
    108         int ans=bfs();
    109         if(ans==-1) printf("NO
    ");
    110         else printf("%d
    ",ans);
    111     }
    112 }

    杭电坑爹的编译器用next不给过,只好全改成了nex……真是embarrassing……

  • 相关阅读:
    centos6升级内核
    centos7启动盘制作
    sed匹配字符串并将匹配行的后几行注释
    cmd下调用xshell登录linux服务器
    centos清除历史命令
    yum错误Cannot retrieve metalink for repository: epel/x86_6
    ansible-playbook
    jdk升级到1.8
    shell瞎记录
    shell 循环
  • 原文地址:https://www.cnblogs.com/dilthey/p/7352582.html
Copyright © 2020-2023  润新知