zoukankan      html  css  js  c++  java
  • POJ 3414 Pots(BFS+回溯)

    Pots
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 11705   Accepted: 4956   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 ≤ ≤ 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)

    Source

    Northeastern Europe 2002, Western Subregion




        题意:输入3个整数n,m,k,前两个整数代表两个杯子的容量。要求用两个杯子通过倒满。倒空。倒入还有一个杯子等方法使得两个杯子中的当中一个杯子的水量等于k,并输出步骤。
        思路:题目非常easy,就是麻烦。特别是步骤那一部分,须要用到BFS的回溯,通过BFS进行搜索,记录两个杯子的水量,使得后面不得反复。

    假设搜不到就输出impossible




    #include<iostream>
    #include<algorithm>
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    #include<queue>
    
    using namespace std;
    
    int n,m,k;
    int ans;
    int v[110][110];
    
    struct node
    {
        int x;
        int y;
        int z;
        int cnt;
    } a[1000010];
    
    void DFS(int kk)
    {
        int pt = a[kk].cnt;
        if(pt<=0)
        {
            return ;
        }
        DFS(pt);
        if(a[pt].x == 1)
        {
            if(a[pt].y == 1)
            {
                printf("FILL(1)
    ");
            }
            else
            {
                printf("FILL(2)
    ");
            }
        }
        else if(a[pt].x == 2)
        {
            if(a[pt].y == 1)
            {
                printf("DROP(1)
    ");
            }
            else
            {
                printf("DROP(2)
    ");
            }
        }
        else if(a[pt].x == 3)
        {
            if(a[pt].y == 1)
            {
                printf("POUR(1,2)
    ");
            }
            else
            {
                printf("POUR(2,1)
    ");
            }
        }
    }
    
    void BFS()
    {
        ans = 1;
        queue<node>q;
        memset(v,0,sizeof(v));
        struct node t,f;
        t.x = 0;
        t.y = 0;
        t.z = 0;
        t.cnt = 0;
        a[0].x = 0;
        a[0].y = 0;
        a[0].cnt = 0;
        q.push(t);
        v[t.x][t.y] = 1;
        while(!q.empty())
        {
            t = q.front();
            q.pop();
            for(int i=1; i<=3; i++)
            {
                for(int j=1; j<=2; j++)
                {
                    f.x = t.x;
                    f.y = t.y;
                    if(i == 1)
                    {
                        if(j == 1 && f.x!=n)
                        {
                            f.x = n;
                        }
                        else if(j == 2 && f.y!=m)
                        {
                            f.y = m;
                        }
                    }
                    else if(i == 2)
                    {
                        if(j == 1 && f.x!=0)
                        {
                            f.x = 0;
                        }
                        else if(j == 2 && f.y!=0)
                        {
                            f.y = 0;
                        }
                    }
                    else if(i == 3)
                    {
                        if(j == 1 && (f.x!=0 && f.y!=m))
                        {
                            if(f.x>=m-f.y)
                            {
                                f.x = f.x - m + f.y;
                                f.y = m;
                            }
                            else
                            {
                                f.y = f.y + f.x;
                                f.x = 0;
                            }
                        }
                        else if(j == 2 && (f.y!=0 && f.x!=n))
                        {
                            if(f.y>=n-f.x)
                            {
                                f.y =  f.y - n + f.x;
                                f.x = n;
                            }
                            else
                            {
                                f.x = f.x + f.y;
                                f.y = 0;
                            }
                        }
                    }
                    if(v[f.x][f.y] == 0)
                    {
                        f.cnt = ans;
                        f.z = t.z + 1;
                        a[ans].x = i;
                        a[ans].y = j;
                        a[ans].cnt = t.cnt;
                        q.push(f);
                        v[f.x][f.y] = 1;
                        if(f.x == k || f.y == k)
                        {
                            printf("%d
    ",f.z);
                            DFS(ans);
                            if(i == 1)
                            {
                                if(j == 1)
                                {
                                    printf("FILL(1)
    ");
                                }
                                else
                                {
                                    printf("FILL(2)
    ");
                                }
                            }
                            else if(i == 2)
                            {
                                if(j == 1)
                                {
                                    printf("DROP(1)
    ");
                                }
                                else
                                {
                                    printf("DROP(2)
    ");
                                }
                            }
                            else if(i == 3)
                            {
                                if(j == 1)
                                {
                                    printf("POUR(1,2)
    ");
                                }
                                else
                                {
                                    printf("POUR(2,1)
    ");
                                }
                            }
                            return ;
                        }
                        ans++;
                    }
                }
            }
        }
        printf("impossible
    ");
    }
    
    int main()
    {
        while(scanf("%d%d%d",&n,&m,&k)!=EOF)
        {
            BFS();
        }
        return 0;
    }
    


  • 相关阅读:
    美女程序员是如何将QQ转换成题目中那串数字的--读博文《找女神要QQ号码》
    C#微信开发-微信JS-SDK(1)之通过config接口注入权限验证配置
    AjaxUpload.3.5.js之ASP.NET 文件上传
    web开发常用的js验证,利用正则表达式验证邮箱、手机、身份证等输入
    HNU_结对项目_小初高数学学习软件_功能说明
    转载:alpha测试和beta测试的区别;黑盒测试和白盒测试的区别;
    HNU_个人项目_中小学数学卷子自动生成程序_简要分析HnuLyx代码
    文件与磁盘空间管理---外存分配方式、存储空间管理
    Java程序设计——不一样的开始 IP地址判定
    Java编程思想—八皇后问题(数组法、堆栈法)
  • 原文地址:https://www.cnblogs.com/yfceshi/p/6742774.html
Copyright © 2011-2022 走看看