zoukankan      html  css  js  c++  java
  • POJ 3414 dfs广搜直接应用

    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)

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<iostream>
    #include<queue>
    #include<math.h>
    using namespace std;
    #define INF 0x3f3f3f3f
    #define LL long long
    #define N 109
    #define mod 2009
    int tt=0,n,m,d;
    int w[N][N];
    char str[6][10]={"FILL(1)","FILL(2)","POUR(1,2)","POUR(2,1)","DROP(1)","DROP(2)"};///对应的数字对应相应的意思
    struct node
    {
        int x,y,t;
        char s[N];
    };
    void q()
    {
        queue<node>Q;
        memset(w,0,sizeof(w));
        node q,p;
        q.x=0;
        q.y=0;
        q.t=0;
        q.s[0]='0';
        Q.push(q);
        w[q.x][q.y]=1;
        while(Q.size())
        {
            p=Q.front();Q.pop();
            if(p.x==d||p.y==d)
            {
                tt=1;
                printf("%d
    ",p.t);
                for(int i=1;i<=p.t;i++)
                    printf("%s
    ",str[p.s[i]-'0']);///按照字符串字符对应的意思输出
                return ;
            }
            for(int i=0;i<6;i++)
            {
                q.t=p.t+1;///保存着第几步
                q.x=q.y=0;
                strcpy(q.s,p.s);///用字符串保留相应的数字   方便输出
                q.s[q.t]=i+'0';///第q.t步选择的那一种
                if(i==0&&p.x!=n)///便是将1杯加满  所以一杯不等于n
                {
                    q.x=n;
                    q.y=p.y;
                }
                else if(i==1&&p.y!=m)///同上
                {
                    q.y=m;
                    q.x=p.x;
                }
                else if(i==2&&p.x&&p.y!=m)///从1往2加  1中必须有 2中必须不满才能加
                {
                    if(p.x+p.y<=m)///1全部加入2中
                    {
                        q.x=0;
                        q.y=p.x+p.y;
                    }
                    else///1还有剩余
                    {
                        q.x=p.x+p.y-m;
                        q.y=m;
                    }
                }
                else if(i==3&&p.y&&p.x!=n)///同上
                {
                    if(p.x+p.y<=n)
                    {
                        q.y=0;
                        q.x=p.x+p.y;
                    }
                    else
                    {
                        q.y=p.x+p.y-n;
                        q.x=n;
                    }
                }
                else if(i==4&&p.x)///将1清0 所以1之前一定不等0
                {
                    q.x=0;
                    q.y=p.y;
                }
                else if(i==5&&p.y)/// 同上
                {
                    q.y=0;
                    q.x=p.x;
                }
    
                if(!w[q.x][q.y])///没有标记的存入队列
                {
                    w[q.x][q.y]=1;
                    Q.push(q);
                }
            }
        }
        return ;
    }
    int main()
    {
        while(scanf("%d%d%d",&n,&m,&d)!=EOF)
        {
            tt=0;
            q();
            if(tt==0)
                printf("impossible
    ");
        }
        return 0;
    }
  • 相关阅读:
    看完这篇文章,你还会问陈景润证明“1+2”有什么意义吗?
    stm32串口发送数据复位 第一个数据丢失
    无理数的由来
    定义一个大数组时,出现错误,程序进入HardFault_Handler中断
    STM32使用FatFs
    块级元素IE6/IE7下inline-block解决方案
    Building Your First jQuery Plugin
    ub挂载window磁盘
    PE文件结构部分解析以及输入的定位
    私有云建设之超融合技术
  • 原文地址:https://www.cnblogs.com/a719525932/p/5763678.html
Copyright © 2011-2022 走看看