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;
    }
     
  • 相关阅读:
    一文快速入门分库分表(必修课)
    MySql分库分表与分区的区别和思考
    常用分库分表方案汇总
    分区分表分库
    MySQL分区和分表
    MySQL的聚集索引和非聚集索引
    PHP大文件上传支持断点上传组件
    PHP大文件上传支持断点上传工具
    Nginx大文件上传支持断点上传
    百度WebUploader大文件上传支持断点上传
  • 原文地址:https://www.cnblogs.com/whileskies/p/7232482.html
Copyright © 2011-2022 走看看