zoukankan      html  css  js  c++  java
  • [poj 3414] Pots bfs+路径打印

    Description

    You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:

    1. FILL(i)        fill the pot i (1 ≤ ≤ 2) from the tap;
    2. DROP(i)      empty the pot i to the drain;
    3. POUR(i,j)    pour from pot i to pot j; after this operation either the pot j is full (and there may be some water left in the pot i), or the pot i is empty (and all its contents have been moved to the pot j).

    Write a program to find the shortest possible sequence of these operations that will yield exactly C liters of water in one of the pots.

    Input

    On the first and only line are the numbers AB, and C. These are all integers in the range from 1 to 100 and C≤max(A,B).

    Output

    The first line of the output must contain the length of the sequence of operations K. The following K lines must each describe one operation. If there are several sequences of minimal length, output any one of them. If the desired result can’t be achieved, the first and only line of the file must contain the word ‘impossible’.

    Sample Input

    3 5 4

    Sample Output

    6
    FILL(2)
    POUR(2,1)
    DROP(1)
    POUR(2,1)
    FILL(2)
    POUR(2,1)

    总共有六种状态,用bfs搜索即可,用字符串记录路径。
    #include <iostream>
    #include <cstring>
    #include <stdio.h>
    #include <vector>
    #include <queue>
    #include <string>
    #include <algorithm>
    using namespace std;
    
    int c;
    int va, vb;
    string op;
    char o[7][20] = {"", "FILL(1)", "FILL(2)", "DROP(1)", "DROP(2)",
                    "POUR(1,2)", "POUR(2,1)"};
    struct node
    {
        int a, b;
        int t;
        string op;
    }pre, now;
    
    queue<node> q;
    bool v[110][110];
    void bfs() 
    {
        memset(v, 0, sizeof(v));
        now.a = 0; now.b = 0;
        now.t = 0; now.op = "";
        v[now.a][now.b] = 1;
        q.push(now);
    
        while (!q.empty()) {
            pre = q.front();
            q.pop();
            if (pre.a == c || pre.b == c) {
                printf("%d
    ", pre.t);
                for (int i = 0; i < pre.op.size(); i++) {
                    printf("%s
    ", o[pre.op[i]-'0']);
                }
                return;
            }
    
            now.a = va; now.b = pre.b;  //fill(a)
            now.t = pre.t+1; now.op = pre.op+'1';
            if (!v[now.a][now.b]) {
                v[now.a][now.b] = 1;
                q.push(now);
            }
    
            now.a = pre.a; now.b = vb;  //fill(b)
            now.t = pre.t+1; now.op = pre.op+'2';
            if (!v[now.a][now.b]) {
                v[now.a][now.b] = 1;
                q.push(now);
            }
    
            now.a = 0; now.b = pre.b;  //drop(a)
            now.t = pre.t+1; now.op = pre.op+'3';
            if (!v[now.a][now.b]) {
                v[now.a][now.b] = 1;
                q.push(now);
            }
    
            now.a = pre.a; now.b = 0;  //drop(b)
            now.t = pre.t+1; now.op = pre.op+'4';
            if (!v[now.a][now.b]) {
                v[now.a][now.b] = 1;
                q.push(now);
            }
    
            if (pre.a > (vb-pre.b)) {  //pour(a,b)
                now.a =pre.a - (vb-pre.b);
                now.b = vb;
            }
            else {
                now.a = 0;
                now.b = pre.a+pre.b;
            }
            now.t = pre.t+1; now.op = pre.op+'5';
            if (!v[now.a][now.b] ) {
                v[now.a][now.b] = 1;
                q.push(now);
            }
    
            if (pre.b > (va-pre.a)) {  //pour(b,a)
                now.b =pre.b - (va-pre.a);
                now.a = va;
            }
            else {
                now.b = 0;
                now.a = pre.a+pre.b;
            }
            now.t = pre.t+1; now.op = pre.op+'6';
            if (!v[now.a][now.b]) {
                v[now.a][now.b] = 1;
                q.push(now);
            }
        }
        printf("impossible
    ");
    }
    
    
    int main()
    {
        //freopen("1.txt", "r", stdin);
        scanf("%d%d%d", &va, &vb, &c);
        bfs();
    
        return 0;
    }
     
  • 相关阅读:
    myeclipse中代码不显示SVN版本号
    java HttpURLConnection 登录网站 完整代码
    新浪微博自动(模拟)登陆详解及实现
    java模拟Cookies登陆
    paper 53 :深度学习(转载)
    paper 52 :windows7环境下theano安装
    paper 51:图像复原
    paper 50 :人脸识别简史与近期进展
    paper 49:论文退稿?审稿人帮你总结了22个能避免的常见问题
    paper 48: Latex中如何制作参考文献
  • 原文地址:https://www.cnblogs.com/whileskies/p/7232482.html
Copyright © 2011-2022 走看看