zoukankan      html  css  js  c++  java
  • bfs + 路径输出

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

            FILL(i)        fill the pot i (1 ≤ i ≤ 2) from the tap;
            DROP(i)      empty the pot i to the drain;
            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 A, B, 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)

    题意 :

    3 种操作,第一种将桶内装满,第二种将桶倒干净,第三种将一个桶内的倒入另一个桶。

    思路 :

    BFS做,每次将当前的状态打入队列,最后要输出路径,在结构体中存上一个点的位置。

    代码 :

    int a, b, c;
    struct node
    {
        int x, y;
        int pt;
        int temp;
        node *pre;
    }arr[400];
     
    int vis[105][105];
    int ans;
    stack<int>s;
    void bfs(){
        queue<node>que; // 每次将当前的状态打入队列,对于每个状态有 6 种处理方法
        memset(vis, 0, sizeof(vis));
        
        node v;
        v.x = 0, v.y = 0;
        v.pt = 0;
        v.temp = 0;
        v.pre = NULL;
        while(!que.empty()) que.pop();
        que.push(v);
        int cnt = -1;
        vis[0][0] = 1;
        
        while(!que.empty()){
            v = que.front();
            que.pop();
            cnt++;
            arr[cnt] = v;
            
            for(int i = 1; i <= 6; i++){
                switch(i){
                    case 1: 
                        v.x = a;
                        v.y = arr[cnt].y;
                        v.pt = 1;
                        break;
                    case 2:
                        v.x = arr[cnt].x;
                        v.y = b;
                        v.pt = 2;
                        break;
                    case 3:
                        v.x = 0;
                        v.y = arr[cnt].y;
                        v.pt = 3;
                        break;
                    case 4:
                        v.x = arr[cnt].x;
                        v.y = 0;
                        v.pt = 4;
                        break;
                    case 5:
                        if (arr[cnt].x > (b - arr[cnt].y)){
                            v.x = arr[cnt].x - (b - arr[cnt].y);
                            v.y = b;
                        }
                        else {
                            v.x = 0;
                            v.y = arr[cnt].x + arr[cnt].y;
                        }
                        v.pt = 5;
                        break;
                    case 6:
                        if (arr[cnt].y > (a - arr[cnt].x)){
                            v.x = a;
                            v.y = arr[cnt].y - (a - arr[cnt].x);
                        }
                        else {
                            v.x = arr[cnt].x + arr[cnt].y;
                            v.y = 0;
                        }
                        v.pt = 6;
                }
                if (vis[v.x][v.y]) continue;
                vis[v.x][v.y] = 1;
                v.temp = arr[cnt].temp + 1; 
                v.pre = &arr[cnt];
                if (v.x == c || v.y == c){
                    
                    while(!s.empty()) s.pop();
                    ans = v.temp;
                    while(v.pre){
                        s.push(v.pt);
                        v = *v.pre;
                    }
                    return;
                }
                que.push(v);
            }
        }
    }
    
    void print(){
        while(!s.empty()){
            int f = s.top();
            s.pop();
            switch(f){
                case 1: printf("FILL(1)
    ");break;
                case 2: printf("FILL(2)
    ");break;
                case 3: printf("DROP(1)
    ");break;
                case 4: printf("DROP(2)
    ");break;
                case 5: printf("POUR(1,2)
    ");break;
                case 6: printf("POUR(2,1)
    ");break;
            }
        }
    }
    
    int main() {
        
        while(~scanf("%d%d%d", &a, &b, &c)){
            ans = 0;
            bfs();
            
            if (ans == 0) printf("impossible
    ");
            else {
                printf("%d
    ", ans);
                print();
            }
        }
        return 0;
    }
    
    东北日出西边雨 道是无情却有情
  • 相关阅读:
    php编程规范整理
    约瑟夫环问题的实现
    MYSQL中SHOW的使用整理收藏
    mysql使用存储过程&函数实现批量插入
    浅谈select for update 和select lock in share mode的区别
    jQuery对象扩展方法(Extend)深度解析
    WCF系列教程之WCF操作协定
    WCF系列教程之WCF实例化
    WCF系列教程之WCF中的会话
    WCF系列教程之WCF服务协定
  • 原文地址:https://www.cnblogs.com/ccut-ry/p/7782917.html
Copyright © 2011-2022 走看看