• 【HDOJ】1495 非常可乐


    bfs.

     1 #include <iostream>
     2 #include <queue>
     3 #include <cstdio>
     4 #include <cstring>
     5 using namespace std;
     6 
     7 #define MAXN 105
     8 
     9 bool visit[MAXN][MAXN][MAXN];
    10 
    11 typedef struct node_t {
    12     int x[3];
    13     int t;
    14     node_t() {}
    15     node_t(int xx, int yy, int ss, int tt) {
    16         x[0] = xx;
    17         x[1] = yy;
    18         x[2] = ss;
    19         t = tt;
    20     }
    21 } node_t;
    22 
    23 int s, m, n;
    24 
    25 bool check(node_t d) {
    26     int half = s>>1;
    27 
    28     if ((d.x[0]==half) && (d.x[1]==0 || d.x[2]==0)) return true;
    29     if ((d.x[0]==0) && (d.x[1]==half)) return true;
    30 
    31     return false;
    32 }
    33 
    34 int bfs() {
    35     if (s & 1)
    36         return -1;
    37     queue<node_t> Q;
    38     int x[3] = {m, n, s};
    39     int i, j, k, tmp;
    40 
    41     memset(visit, false, sizeof(visit));
    42     visit[0][0][s] = true;
    43     Q.push(node_t(0,0,s,0));
    44 
    45     while (!Q.empty()) {
    46         node_t d = Q.front();
    47         Q.pop();
    48         for (i=0; i<3; ++i) {
    49             for (j=0; j<3; ++j) {
    50                 if (i == j)
    51                     continue;
    52                 node_t dd = d;
    53                 // from i to j
    54                 tmp = x[j] - dd.x[j];
    55                 if (tmp > 0) {
    56                     if (dd.x[i] > tmp) {
    57                         dd.x[j] = x[j];
    58                         dd.x[i] -= tmp;
    59                     } else {
    60                         dd.x[j] += dd.x[i];
    61                         dd.x[i] = 0;
    62                     }
    63                 }
    64                 if (visit[dd.x[0]][dd.x[1]][dd.x[2]]) {
    65                     continue;
    66                 }
    67                 ++dd.t;
    68                 visit[dd.x[0]][dd.x[1]][dd.x[2]] = true;
    69                 if ( check(dd) )
    70                     return dd.t;
    71                 Q.push(dd);
    72             }
    73         }
    74     }
    75 
    76     return -1;
    77 }
    78 
    79 int main() {
    80 
    81 #ifndef ONLINE_JUDGE
    82     freopen("data.in", "r", stdin);
    83 #endif
    84 
    85     while (scanf("%d%d%d",&s,&m,&n)!=EOF && (s||m||n)) {
    86         int ans = bfs();
    87         if (ans > 0)
    88             printf("%d
    ", ans);
    89         else
    90             printf("NO
    ");
    91     }
    92 
    93     return 0;
    94 }
  • 相关阅读:
    MySQL-keepalived做高可用
    Linux-服务管理
    MySQL-CentOS7上安装Mysql5.7
    MySQL-查看DB文件位置
    游戏编程与游戏种类
    计算机
    python
    python中的构造函数
    IndentationError:expected an indented block错误解决
    python程序的pdb调试方法
  • 原文地址:https://www.cnblogs.com/bombe1013/p/4095358.html
Copyright © 2020-2023  润新知