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;
    }
     
  • 相关阅读:
    使用 CountDownLatch 控制多个线程执行顺序
    define 与 inline
    虚函数 纯虚函数 抽象方法 接口
    [转]Android 超高仿微信图片选择器 图片该这么加载
    Android ImageView src与backgroud
    Android View绘制原理分析
    Android 5.0 Default SMS App以及运营商授权SMS App
    Android 5.0 双卡信息管理分析
    Android 5.1 AOSP 源码获取
    Android 5.0 Uicc框架分析
  • 原文地址:https://www.cnblogs.com/whileskies/p/7232482.html
Copyright © 2011-2022 走看看