zoukankan      html  css  js  c++  java
  • poj3414--Pots(bfs,记录路径)

    Pots
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 10149   Accepted: 4275   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)
    POUR(2,1)
    DROP(1)
    POUR(2,1)
    FILL(2)
    POUR(2,1)

    有a,b两个瓶,得到体积为c的液体,六种操作

    装满1。装满2,倒掉1。倒掉2,由1倒到2,有2倒到1.

    记录一下路径。也就是有谁到达的当前状态

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    struct node{
        int a , b , pre ;
        char s[20] ;
    }p[1000000] , x ;
    int flag[20000] , low , top , k ;
    char str[6][20] = { "FILL(1)","FILL(2)","DROP(1)","DROP(2)","POUR(1,2)","POUR(2,1)" };
    int pre[1000000] ;
    int main()
    {
        int a , b , c , sum , i , j ;
        k = 0 ;
        memset(flag,0,sizeof(flag));
        scanf("%d %d %d", &a, &b, &c);
        flag[a*100+b] = 1 ;
        low = top = 0 ;
        p[top].a = 0 ; p[top].b = 0 ;
        p[top++].pre = -1 ;
        while(low < top)
        {
            if( p[low].a == c || p[low].b == c )
                break;
            if( p[low].a < a )
            {
                p[top].a = a ; p[top].b = p[low].b ;
                if( !flag[ p[top].a*100+p[top].b ] )
                {
                    flag[ p[top].a*100+p[top].b ] = 1 ;
                    strcpy(p[top].s,str[0]);
                    p[top++].pre = low ;
                }
            }
            if( p[low].b < b )
            {
                p[top].a = p[low].a ; p[top].b = b ;
                if( !flag[ p[top].a*100+p[top].b ] )
                {
                    flag[ p[top].a*100+p[top].b ] = 1 ;
                    strcpy(p[top].s,str[1]);
                    p[top++].pre = low ;
                }
            }
            if( p[low].a > 0 )
            {
                p[top].a = 0 ; p[top].b = p[low].b ;
                if( !flag[ p[top].a*100+p[top].b ] )
                {
                    flag[ p[top].a*100+p[top].b ] = 1 ;
                    strcpy(p[top].s,str[2]);
                    p[top++].pre = low ;
                }
            }
            if( p[low].b > 0 )
            {
                p[top].a = p[low].a ; p[top].b = 0 ;
                if( !flag[ p[top].a*100+p[top].b ] )
                {
                    flag[ p[top].a*100+p[top].b ] = 1 ;
                    strcpy(p[top].s,str[3]);
                    p[top++].pre = low ;
                }
            }
            if( p[low].a > 0 && p[low].b < b )
            {
                sum = p[low].a + p[low].b ;
                if( sum <= b )
                {
                    p[top].a = 0 ; p[top].b = sum ;
                }
                else
                {
                    p[top].a = sum - b ; p[top].b = b ;
                }
                if( !flag[ p[top].a*100+p[top].b ] )
                {
                    flag[ p[top].a*100+p[top].b ] = 1 ;
                    strcpy(p[top].s,str[4]);
                    p[top++].pre = low ;
                }
            }
            if( p[low].a < a && p[low].b > 0 )
            {
                sum = p[low].a + p[low].b ;
                if( sum <= a )
                {
                    p[top].a = sum ; p[top].b = 0 ;
                }
                else
                {
                    p[top].a = a ; p[top].b = sum - a ;
                }
                if( !flag[ p[top].a*100+p[top].b ] )
                {
                    flag[ p[top].a*100+p[top].b ] = 1 ;
                    strcpy(p[top].s,str[5]);
                    p[top++].pre = low ;
                }
            }
            low++ ;
        }
        if( low == top )
            printf("impossible
    ");
        else
        {
            j = 0 ;
            for(i = low ; i != 0 ; i = p[i].pre)
            {
                pre[j++] = i ;
            }
            printf("%d
    ", j);
            for(j -= 1 ; j >= 0 ; j--)
                printf("%s
    ", p[ pre[j] ].s);
        }
        return 0;
    }
    


     

    版权声明:转载请注明出处:http://blog.csdn.net/winddreams

  • 相关阅读:
    微信小程序-开发入门(一)
    Vue入门
    美化input type=range标签滑动样式(带渐变效果)
    全新的css网站布局--Grid布局
    移动端布局与样式上的坑
    swiper 下拉刷新混乱
    子元素scroll父元素容器不跟随滚动JS实现
    解决前端页面闪烁问题(转载)
    VUE 与其他常见前端框架对比
    移动端表层div滑动,导致底层body滑动(touchmove的阻止)
  • 原文地址:https://www.cnblogs.com/bhlsheji/p/4887472.html
Copyright © 2011-2022 走看看