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

    题意:略。

    析:由于只有三只杯子,那么我们可以用两个杯子的状态,那么第三只的状态也可以确定下来,每次倒水,要么全倒过去,要么把那个杯子倒满。

    注意题意是要保证最后是两个一样多。

    代码如下:

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include <cstdio>
    #include <string>
    #include <cstdlib>
    #include <cmath>
    #include <iostream>
    #include <cstring>
    #include <set>
    #include <queue>
    #include <algorithm>
    #include <vector>
    #include <map>
    #include <cctype>
    #include <cmath>
    #include <stack>
    #include <sstream>
    #define debug() puts("++++");
    #define gcd(a, b) __gcd(a, b)
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define freopenr freopen("in.txt", "r", stdin)
    #define freopenw freopen("out.txt", "w", stdout)
    using namespace std;
    
    typedef long long LL;
    typedef unsigned long long ULL;
    typedef pair<int, int> P;
    const int INF = 0x3f3f3f3f;
    const double inf = 0x3f3f3f3f3f3f;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int maxn = 200 + 10;
    const int mod = 1e9 + 7;
    const int dr[] = {-1, 0, 1, 0};
    const int dc[] = {0, 1, 0, -1};
    const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
    int n, m;
    const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    inline bool is_in(int r, int c){
      return r >= 0 && r < n && c >= 0 && c < m;
    }
    int d[maxn][maxn];
    int all;
    
    struct Node{
      int a, b;
      Node(int aa, int bb) : a(aa), b(bb) { }
    };
    
    bool judge(Node &u){
      if(u.a * 2 == all)  return u.b == 0 || (all - u.a - u.b) == 0;
      if(u.b * 2 == all)  return u.a == 0 || (all - u.a - u.b) == 0;
      if((all - u.a - u.b) * 2 == all)  return u.a == 0 || u.b == 0;
      return false;
    }
    
    void update(int &x, int &y, Node &u, queue<Node> &q){
      d[x][y] = d[u.a][u.b] + 1;
      q.push(Node(x, y));
    }
    
    int bfs(){
      queue<Node> q;
      q.push(Node(all, 0));
      memset(d, -1, sizeof d);
      d[all][0] = 0;
    
      while(!q.empty()){
        Node u = q.front();  q.pop();
        if(judge(u))  return d[u.a][u.b];
        // pour the first to the second or not
        int t = min(u.a, n - u.b);
        int x = u.a - t, y = u.b + t;
        if(d[x][y] == -1)  update(x, y, u, q);
        t = min(all-u.a, u.b);
        x = u.a + t, y = u.b - t;
        if(d[x][y] == -1)  update(x, y, u, q);
        // pour the first to the third or not
        t = min(u.a, m - (all-u.a-u.b));
        x = u.a - t, y = u.b;
        if(d[x][y] == -1)  update(x, y, u, q);
        t = min(all-u.a, all-u.a-u.b);
        x = u.a + t;
        if(d[x][y] == -1)  update(x, y, u, q);
        //pour the second to the third or not
        t = min(u.b, m - (all-u.a-u.b));
        x = u.a, y = u.b - t;
        if(d[x][y] == -1)  update(x, y, u, q);
        t = min(n-u.b, all-u.a-u.b);
        y = u.b + t;
        if(d[x][y] == -1)  update(x, y, u, q);
      }
      return -1;
    }
    
    int main(){
      while(scanf("%d %d %d", &all, &n, &m) == 3 && all + n + m){
        all <<= 1, n <<= 1, m <<= 1;
        int ans = bfs();
        if(ans == -1)  printf("NO
    ");
        else printf("%d
    ", ans);
      }
      return 0;
    }
    
  • 相关阅读:
    java EL表达式中${param.name}详细
    javascript中常用坐标属性offset、scroll、client
    Oralce if ..elsif结构
    VS2013 字符串函数与VC不兼容问题解决方法
    Directory Instruction
    Timestamp
    GetCurrentTime() and CFile option
    MessageBox 参数说明
    单片机红外线接收算法
    IAR for MPS430 V7.12.1 编程规范
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/6599925.html
Copyright © 2011-2022 走看看