POJ3414,Pots,隐式图搜索

7 篇文章 0 订阅

Pots

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)

分析:
将一个状态看做(a,b)这一坐标,6入口bfs。这里需要打印路径,我的方式是每一个结点增加一个path信息,从上一个直接结点拷贝然后再在末尾加上当前的方向。
要占不少空间貌似,不过这题数据弱爆了,看discuss里很多有问题的程序都能过。时间倒是0ms...还有,尼玛把impossible写成Impossible害得我贡献了一次WA还有n久的时间...给出一组强大的数据。。。

91 100 95
结果是
170
FILL(1)
POUR(1,2)
FILL(1)
POUR(1,2)
DROP(2)
POUR(1,2)
FILL(1)
POUR(1,2)
DROP(2)
POUR(1,2)
FILL(1)
POUR(1,2)
DROP(2)
POUR(1,2)
FILL(1)
POUR(1,2)
DROP(2)
POUR(1,2)
FILL(1)
POUR(1,2)
DROP(2)
POUR(1,2)
FILL(1)
POUR(1,2)
DROP(2)
POUR(1,2)
FILL(1)
POUR(1,2)
DROP(2)
POUR(1,2)
FILL(1)
POUR(1,2)
DROP(2)
POUR(1,2)
FILL(1)
POUR(1,2)
DROP(2)
POUR(1,2)
FILL(1)
POUR(1,2)
DROP(2)
POUR(1,2)
FILL(1)
POUR(1,2)
FILL(1)
POUR(1,2)
DROP(2)
POUR(1,2)
FILL(1)
POUR(1,2)
DROP(2)
POUR(1,2)
FILL(1)
POUR(1,2)
DROP(2)
POUR(1,2)
FILL(1)
POUR(1,2)
DROP(2)
POUR(1,2)
FILL(1)
POUR(1,2)
DROP(2)
POUR(1,2)
FILL(1)
POUR(1,2)
DROP(2)
POUR(1,2)
FILL(1)
POUR(1,2)
DROP(2)
POUR(1,2)
FILL(1)
POUR(1,2)
DROP(2)
POUR(1,2)
FILL(1)
POUR(1,2)
DROP(2)
POUR(1,2)
FILL(1)
POUR(1,2)
DROP(2)
POUR(1,2)
FILL(1)
POUR(1,2)
FILL(1)
POUR(1,2)
DROP(2)
POUR(1,2)
FILL(1)
POUR(1,2)
DROP(2)
POUR(1,2)
FILL(1)
POUR(1,2)
DROP(2)
POUR(1,2)
FILL(1)
POUR(1,2)
DROP(2)
POUR(1,2)
FILL(1)
POUR(1,2)
DROP(2)
POUR(1,2)
FILL(1)
POUR(1,2)
DROP(2)
POUR(1,2)
FILL(1)
POUR(1,2)
DROP(2)
POUR(1,2)
FILL(1)
POUR(1,2)
DROP(2)
POUR(1,2)
FILL(1)
POUR(1,2)
DROP(2)
POUR(1,2)
FILL(1)
POUR(1,2)
DROP(2)
POUR(1,2)
FILL(1)
POUR(1,2)
FILL(1)
POUR(1,2)
DROP(2)
POUR(1,2)
FILL(1)
POUR(1,2)
DROP(2)
POUR(1,2)
FILL(1)
POUR(1,2)
DROP(2)
POUR(1,2)
FILL(1)
POUR(1,2)
DROP(2)
POUR(1,2)
FILL(1)
POUR(1,2)
DROP(2)
POUR(1,2)
FILL(1)
POUR(1,2)
DROP(2)
POUR(1,2)
FILL(1)
POUR(1,2)
DROP(2)
POUR(1,2)
FILL(1)
POUR(1,2)
DROP(2)
POUR(1,2)
FILL(1)
POUR(1,2)
DROP(2)
POUR(1,2)
FILL(1)
POUR(1,2)
DROP(2)
POUR(1,2)
FILL(1)
POUR(1,2)

code:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<cmath>
#include<queue>
using namespace std;
int A,B,C;
typedef struct Node
{
    int a,b;
    int step;
    string path;
}Node;
queue<Node> q;
bool vis[105][105];
void PrintPath(string s)
{
    for(int i=0;i<s.size();i++)
    {
        switch(s[i])
        {
            case '1': printf("FILL(1)\n"); break;
            case '2': printf("FILL(2)\n"); break;
            case '3': printf("DROP(1)\n"); break;
            case '4': printf("DROP(2)\n"); break;
            case '5': printf("POUR(1,2)\n"); break;
            case '6': printf("POUR(2,1)\n"); break;
        }
    }
}
void bfs()
{
    Node first={0,0,0,"\0"};
    q.push(first);
    vis[0][0]=1;
    while(!q.empty())
    {
        Node tmp=q.front();
        if(tmp.a==C||tmp.b==C)
        {
            printf("%d\n",tmp.step);
            PrintPath(tmp.path);
            return;
        }
        for(int i=1;i<=6;i++)
        {
            switch(i)
            {
                case 1:
                    if(!vis[A][tmp.b]||A==C)
                    {
                        Node tmp1={A,tmp.b,tmp.step+1,tmp.path};
                        tmp1.path+='1';
                        vis[tmp1.a][tmp1.b]=true;
                        q.push(tmp1);
                    }
                    break;
                case 2:
                    if(!vis[tmp.a][B]||B==C)
                    {
                        Node tmp2={tmp.a,B,tmp.step+1,tmp.path};
                        tmp2.path+='2';
                        vis[tmp2.a][tmp2.b]=true;
                        q.push(tmp2);
                    }
                    break;
                case 3:
                    if(!vis[0][tmp.b])
                    {
                        Node tmp3={0,tmp.b,tmp.step+1,tmp.path};
                        tmp3.path+='3';
                        vis[tmp3.a][tmp3.b]=true;
                        q.push(tmp3);
                    }
                    break;
                case 4:
                    if(!vis[tmp.a][0])
                    {
                        Node tmp4={tmp.a,0,tmp.step+1,tmp.path};
                        tmp4.path+='4';
                        vis[tmp4.a][tmp4.b]=true;
                        q.push(tmp4);
                    }
                    break;
                case 5:
                {
                    Node tmp5;
                    if(tmp.a+tmp.b<=B)
                    {
                        Node _tmp={0,tmp.a+tmp.b,tmp.step+1,tmp.path};
                        tmp5=_tmp;
                    }
                    else
                    {
                        Node _tmp={tmp.a+tmp.b-B,B,tmp.step+1,tmp.path};
                        tmp5=_tmp;
                    }
                    if(!vis[tmp5.a][tmp5.b])
                    {
                        vis[tmp5.a][tmp5.b]=true;
                        tmp5.path+='5';
                        q.push(tmp5);
                    }
                    break;
                }
                case 6:
                {
                    Node tmp6;
                    if(tmp.a+tmp.b<=A)
                    {
                        Node _tmp={tmp.a+tmp.b,0,tmp.step+1,tmp.path};
                        tmp6=_tmp;
                    }
                    else
                    {
                        Node _tmp={A,tmp.a+tmp.b-A,tmp.step+1,tmp.path};
                        tmp6=_tmp;
                    }
                    if(!vis[tmp6.a][tmp6.b])
                    {
                        vis[tmp6.a][tmp6.b]=true;
                        tmp6.path+='6';
                        q.push(tmp6);
                    }
                    break;
                }
            }
        }
        q.pop();
    }
    printf("impossible\n");
    return;
}
int main()
{
    memset(vis,false,sizeof(vis));
    scanf("%d %d %d",&A,&B,&C);
    bfs();
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值