• HDOJ 1495 非常可乐 【bfs】


    HDOJ 1495 非常可乐 【bfs】

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


    一共只有6种状态
    a → b
    a → c
    b → a
    b → c
    c → a
    c → b
    bfs搜一遍即可
    注意队列清空 标记数组清零
    输入S为奇数时一定不符合要求,删除该情况剪枝

      1 #include<iostream>
      2 #include<algorithm>
      3 #include<cstdio>
      4 #include<cstring>
      5 #include<queue>
      6 using namespace std;
      7 int S, N, M;
      8 typedef struct state{
      9     int s, n, m; // 三个杯子的容量
     10     int step; // 次数
     11 }state;
     12 queue<state> q;
     13 bool vis[105][105][105];
     14 bool End(int a, int b, int c){
     15     if((a == b && c == 0) || (a == c && b == 0) || (b == c && a == 0)){
     16         return true;
     17     }
     18     else
     19         return false;
     20 }
     21 
     22 int bfs(){
     23     memset(vis, 0, sizeof(vis));
     24     while(!q.empty()) q.pop();
     25     state start;
     26     start.s = S, start.n = 0, start.m = 0, start.step = 0;
     27     vis[S][0][0] = true;
     28     //printf("start
    ");
     29     q.push(start);
     30     while(!q.empty()){
     31         state current, next;
     32         int s, n, m, step; // current.
     33         current = q.front();
     34         s = current.s, n = current.n, m = current.m, step = current.step;
     35         //printf("%d	%d	%d
    ", s, n, m);
     36 
     37         if(End(s, n, m)){ // 判断结束
     38             //printf("%d
    ", step);
     39             return step;
     40         }
     41 
     42         next.step = step+1;
     43         if(s > 0){ // a → b / c
     44             if(s > N-n){ // a → b a有剩余
     45                 next.s = s-(N-n);
     46                 next.n = N;
     47                 next.m = m;
     48                 if(vis[next.s][next.n][next.m] == false){
     49                     vis[next.s][next.n][next.m] = true;
     50                     //printf("a → b a有剩余
    ");
     51                     q.push(next);
     52                 }
     53             }
     54             else{ // a → b a无剩余
     55                 next.s = 0;
     56                 next.n = n+s;
     57                 next.m = m;
     58                 if(vis[next.s][next.n][next.m] == false){
     59                     vis[next.s][next.n][next.m] = true;
     60                     //printf("a → b a无剩余
    ");
     61                     q.push(next);
     62                 }
     63             }
     64             if(s > M-m){ // a → c a有剩余
     65                 next.s = s-(M-m);
     66                 next.n = n;
     67                 next.m = M;
     68                 if(vis[next.s][next.n][next.m] == false){
     69                     vis[next.s][next.n][next.m] = true;
     70                     //printf("a → c a有剩余
    ");
     71                     q.push(next);
     72                 }
     73             }
     74             else{ // a → c a无剩余
     75                 next.s = 0;
     76                 next.n = n;
     77                 next.m = m+s;
     78                 if(vis[next.s][next.n][next.m] == false){
     79                     vis[next.s][next.n][next.m] = true;
     80                     //printf("a → c a无剩余
    ");
     81                     q.push(next);
     82                 }
     83             }
     84         }
     85         if(n > 0){ // b → a / c
     86             if(n > S-s){ // b → a b有剩余
     87                 next.s = S;
     88                 next.n = n - (S-s);
     89                 next.m = m;
     90                 if(vis[next.s][next.n][next.m] == false){
     91                     vis[next.s][next.n][next.m] = true;
     92                     //printf("b → a b有剩余
    ");
     93                     q.push(next);
     94                 }
     95             }
     96             else{ // b → a b无剩余
     97                 next.s = s+n;
     98                 next.n = 0;
     99                 next.m = m;
    100                 if(vis[next.s][next.n][next.m] == false){
    101                     vis[next.s][next.n][next.m] = true;
    102                     //printf("b → a b无剩余
    ");
    103                     q.push(next);
    104                 }
    105             }
    106             if(n > M-m){ // b → c b有剩余
    107                 next.s = s;
    108                 next.n = n - (M-m);
    109                 next.m = M;
    110                 if(vis[next.s][next.n][next.m] == false){
    111                     vis[next.s][next.n][next.m] = true;
    112                     //printf("b → c b有剩余
    ");
    113                     q.push(next);
    114                 }
    115             }
    116             else{ // b → c b无剩余
    117                 next.s = s;
    118                 next.n = 0;
    119                 next.m = m+n;
    120                 if(vis[next.s][next.n][next.m] == false){
    121                     vis[next.s][next.n][next.m] = true;
    122                     //printf("b → c b无剩余
    ");
    123                     q.push(next);
    124                 }
    125             }
    126         }
    127         if(m > 0){ // c → a / b
    128             if(m > S-s){ // c → a c有剩余
    129                 next.s = S;
    130                 next.n = n;
    131                 next.m = m - (S-s);
    132                 if(vis[next.s][next.n][next.m] == false){
    133                     vis[next.s][next.n][next.m] = true;
    134                     //printf("c → a c有剩余
    ");
    135                     q.push(next);
    136                 }
    137             }
    138             else{ // c → a c无剩余
    139                 next.s = s+m;
    140                 next.n = n;
    141                 next.m = 0;
    142                 if(vis[next.s][next.n][next.m] == false){
    143                     vis[next.s][next.n][next.m] = true;
    144                     //printf("c → a c无剩余
    ");
    145                     q.push(next);
    146                 }
    147             }
    148             if(m > N-n){ // c → b c有剩余
    149                 next.s = s;
    150                 next.n = N;
    151                 next.m = m - (N-n);
    152                 if(vis[next.s][next.n][next.m] == false){
    153                     vis[next.s][next.n][next.m] = true;
    154                     //printf("c → b c有剩余
    ");
    155                     q.push(next);
    156                 }
    157             }
    158             else{ // c → b c无剩余
    159                 next.s = s;
    160                 next.n = n+m;
    161                 next.m = 0;
    162                 if(vis[next.s][next.n][next.m] == false){
    163                     vis[next.s][next.n][next.m] = true;
    164                     //printf("c → b c无剩余
    ");
    165                     q.push(next);
    166                 }
    167             }
    168         }
    169         q.pop();
    170     }
    171     return 0;
    172 }
    173 
    174 int main(){
    175     while(~scanf("%d%d%d", &S, &N, &M)){
    176         if(!S && !N && !M) break;
    177         if(S & 1) printf("NO
    ");
    178         else{
    179             int ans = bfs();
    180             if(!ans) printf("NO
    ");
    181             else printf("%d
    ", ans);
    182         }
    183     }
    184 
    185     return 0;
    186 }
  • 相关阅读:
    importToMbtiles
    http-server服务跨域设置
    Leaflet 测试加载高德地图
    矢量切片
    zigzag方式编码
    CentOS7安装docker
    CentOS7安装NodeJS
    CentOS7上Python3.5安装
    JDK8下maven使用maven-javadoc-plugin插件报错
    Ubuntu16.10上安装NodeJS6.9.2
  • 原文地址:https://www.cnblogs.com/miaowTracy/p/4836767.html
Copyright © 2020-2023  润新知