zoukankan      html  css  js  c++  java
  • poj 3414 Pots【bfs+回溯路径 正向输出】

    题目地址:http://poj.org/problem?id=3414

    Pots
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 12080   Accepted: 5104   Special Judge

    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 ≤ i ≤ 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 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)
    #include<stdio.h>
    #include<string.h>
    #include <queue>
    #include <stack>
    #include <algorithm>
    
    using namespace std;
    
    int a, b, c;//a b容器的大小 c目标体积
    
    bool vis[101][101];
    
    struct node
    {
        char ch;
        int x,y;
        int pre;
        int self;
    }op[100000];
    int e=0;
    
    struct path
    {
        int u, v;
        int cnt;
        int pre;
        int self;
    };
    
    //int road[10000], step;
    
    stack<int>p;
    bool ok;
    int ans;
    
    void bfs()
    {
        queue<path>q;//创建队列
        memset(vis, false, sizeof(vis));//初始化未访问
    
        path s; s.u=0; s.v=0; s.cnt=0; s.pre=-1; s.self=-1;
        q.push(s);  int num=-1;
        vis[0][0]=true; ok=false; ans=0;
    
        while(!q.empty())
        {
            path cur=q.front(); q.pop();
    
            if(cur.u==c || cur.v==c){
                ans=cur.cnt;
                ok=true;
                int fa=cur.self;
                while(fa!=-1){
                    p.push(fa);
                    fa=op[fa].pre;
                }
                break;
            }//如果找到了目标状态
    
            //进行6种操作
            path temp;
            //Fill A
            temp=cur; temp.u=a; temp.cnt=cur.cnt+1;
            if(!vis[temp.u][temp.v]){
                temp.pre=cur.self; num++; temp.self=num;
                q.push(temp);
                vis[temp.u][temp.v]=true;//标记状态访问
                op[e].pre=cur.self;
                op[e].ch='F'; op[e++].x=1;
            }
    
            //File B
            temp=cur; temp.v=b; temp.cnt=cur.cnt+1;
            if(!vis[temp.u][temp.v]){
                temp.pre=cur.self; num++; temp.self=num;
                q.push(temp);
                vis[temp.u][temp.v]=true;
                op[e].pre=cur.self;
                op[e].ch='F'; op[e++].x=2;
            }
    
            //Drop A
            temp=cur; temp.u=0; temp.cnt=cur.cnt+1;
            if(!vis[temp.u][temp.v]){
                temp.pre=cur.self; num++; temp.self=num;
                q.push(temp);
                vis[temp.u][temp.v]=true;
                op[e].pre=cur.self;
                op[e].ch='D'; op[e++].x=1;
            }
            //Drop B
            temp=cur; temp.v=0; temp.cnt=cur.cnt+1;
            if(!vis[temp.u][temp.v]){
                temp.pre=cur.self; num++; temp.self=num;
                q.push(temp);
                vis[temp.u][temp.v]=true;
                op[e].pre=cur.self;
                op[e].ch='D'; op[e++].x=2;
            }
    
            //pour(A, B)
            temp.v=cur.u+cur.v; temp.u=0; temp.cnt=cur.cnt+1;
            if(temp.v>b){
                temp.v=b;//装满B
                temp.u=cur.u+cur.v-b;
            }
            if(!vis[temp.u][temp.v]){
                temp.pre=cur.self; num++; temp.self=num;
                q.push(temp);
                vis[temp.u][temp.v]=true;
                op[e].pre=cur.self;
                op[e].ch='P'; op[e].x=1; op[e++].y=2;
            }
    
            //pour(B, A)
            temp.u=cur.u+cur.v; temp.v=0; temp.cnt=cur.cnt+1;
            if(temp.u>a){
                temp.u=a;//装满A
                temp.v=cur.u+cur.v-a;
            }
            if(!vis[temp.u][temp.v]){
                temp.pre=cur.self; num++; temp.self=num;
                q.push(temp);
                vis[temp.u][temp.v]=true;
                op[e].pre=cur.self;
                op[e].ch='P'; op[e].x=2; op[e++].y=1;
            }
    
        }
    }
    
    int main()
    {
        scanf("%d %d %d", &a, &b, &c);
        e=0;
        bfs();
    
        if(!ok){
            printf("impossible
    ");
        }
        else{
            printf("%d
    ", ans);
            while(!p.empty())
            {
                int dd=p.top(); p.pop();
    
                if(op[dd].ch=='F'){
                    printf("FILL(%d)
    ", op[dd].x );
                }
                else if(op[dd].ch=='D'){
                    printf("DROP(%d)
    ", op[dd].x );
                }
                else{
                    printf("POUR(%d,%d)
    ", op[dd].x, op[dd].y );
                }
            }
        }
    
    	return 0;
    }
    
    POUR(2,1) DROP(1) POUR(2,1) FILL(2) POUR(2,1)

    代码:
  • 相关阅读:
    向Oracle 数据表中插入一条带有日期类型的数据
    JDBC 连接Oracle 数据库,JDBC 连接Mysql 数据库
    球球大作战四亿人都在玩?玩家回归没有优越感,新玩家游戏被虐,游戏体验感极差!
    struts2中的错误--java.lang.NoClassDefFoundError: org/apache/commons/lang3/StringUtils
    如何在idea中设置 jsp 内容修改以后,立即生效而不用重新启动服务?
    idea中 在接口中如何直接跳转到该接口的是实现类中?
    使用IDEA 创建Servlet 的时候,找不到javax.servlet
    如何高效的遍历HashMap 以及对key 进行排序
    springboot 自动装配
    git 多账户添加ssh秘钥
  • 原文地址:https://www.cnblogs.com/yspworld/p/4748876.html
Copyright © 2011-2022 走看看