poj3414——Pots(BFS)

Description

You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:

FILL(i) fill the pot i (1 ≤ i ≤ 2) from the tap;

DROP(i) empty the pot i to the drain;

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)

简单的BFS,三个操作总共六种状态,遍历就行

#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <cstdio>
#include <algorithm>
#define INF 0x3f3f3f3f
#define MAXN 1010
using namespace std;
struct Node
{
    int a,b;
    int op[10000],cnt;

};
bool flag;
int vis[110][110];
void bfs(Node start,int a,int b,int c)
{
    queue<Node> q;
    q.push(start);
    while(!q.empty())
    {
        Node tmp=q.front();
        q.pop();
        //cout<<tmp.a<<" "<<tmp.b<<endl;
        if(tmp.a==c||tmp.b==c)
        {
            printf("%d\n",tmp.cnt);
            for(int i=0; i<tmp.cnt; ++i)
            {
                if(tmp.op[i]==11)
                    printf("FILL(1)\n");
                if(tmp.op[i]==12)
                    printf("FILL(2)\n");
                if(tmp.op[i]==21)
                    printf("DROP(1)\n");
                if(tmp.op[i]==22)
                    printf("DROP(2)\n");
                if(tmp.op[i]==312)
                    printf("POUR(1,2)\n");
                if(tmp.op[i]==321)
                    printf("POUR(2,1)\n");
            }
            flag=true;
            return;
        }
        Node tmp1;

        tmp1=tmp;
        if(tmp1.a!=0)
        {
            tmp1.a=0;
            tmp1.op[tmp1.cnt++]=21;
            if(!vis[tmp1.a][tmp1.b])
            {
                vis[tmp1.a][tmp1.b]=1;
                q.push(tmp1);
            }
        }

        tmp1=tmp;
        if(tmp1.b!=0)
        {
            tmp1.b=0;
            tmp1.op[tmp1.cnt++]=22;
            if(!vis[tmp1.a][tmp1.b])
            {
                vis[tmp1.a][tmp1.b]=1;
                q.push(tmp1);
            }
        }

        tmp1=tmp;
        if(tmp1.a!=a)
        {
            tmp1.a=a;
            tmp1.op[tmp1.cnt++]=11;
            if(!vis[tmp1.a][tmp1.b])
            {
                vis[tmp1.a][tmp1.b]=1;
                q.push(tmp1);
            }
        }

        tmp1=tmp;
        if(tmp1.b!=b)
        {
            tmp1.b=b;
            tmp1.op[tmp1.cnt++]=12;
            if(!vis[tmp1.a][tmp1.b])
            {
                vis[tmp1.a][tmp1.b]=1;
                q.push(tmp1);
            }
        }

        tmp1=tmp;
        if(tmp1.a>b-tmp1.b)
        {
            tmp1.a-=(b-tmp1.b);
            tmp1.b=b;
            tmp1.op[tmp1.cnt++]=312;
        }
        else
        {
            tmp1.b+=tmp1.a;
            tmp1.a=0;
            tmp1.op[tmp1.cnt++]=312;
        }
        if(!vis[tmp1.a][tmp1.b])
        {
            vis[tmp1.a][tmp1.b]=1;
            q.push(tmp1);
        }

        tmp1=tmp;
        if(tmp1.b>a-tmp1.a)
        {
            tmp1.b-=(a-tmp1.a);
            tmp1.a=a;
            tmp1.op[tmp1.cnt++]=321;
        }
        else
        {
            tmp1.a+=tmp1.b;
            tmp1.b=0;
            tmp1.op[tmp1.cnt++]=321;
        }
        if(!vis[tmp1.a][tmp1.b])
        {
            vis[tmp1.a][tmp1.b]=1;
            q.push(tmp1);
        }
    }
}
int main()
{
    int a,b,c;
    while(~scanf("%d%d%d",&a,&b,&c))
    {
        flag=false;
        Node start;
        memset(vis,0,sizeof(vis));
        start.a=0;
        start.b=0;
        start.cnt=0;
        bfs(start,a,b,c);
        if(!flag)
            cout<<"impossible"<<endl;
    }
    return 0;
}
时间: 2025-01-08 18:06:51

poj3414——Pots(BFS)的相关文章

poj3414 Pots (BFS)

Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12198   Accepted: 5147   Special Judge Description You are given two pots, having the volume of A and B liters respectively. The following operations can be performed: FILL(i)        f

POJ-3414 Pots (BFS)

Description You are given two pots, having the volume of A and B liters respectively. The following operations can be performed: FILL(i)        fill the pot i (1 ≤ i ≤ 2) from the tap; DROP(i)      empty the pot i to the drain; POUR(i,j)    pour from

poj3414 Pots(BFS)

题目链接 http://poj.org/problem?id=3414 题意 有两个杯子,容量分别为A升,B升,可以向杯子里倒满水,将杯子里的水倒空,将一个杯子里的水倒到另一个杯子里,求怎样倒才能使其中的一个杯子里的水恰为C升,输出最少步数和操作:如果不能倒到C升,输出“impossible”. 思路 这题与poj1606基本相同,在poj1606的基础上添加了输出最少步数,修改了操作的表示,以及不可能达到目标时输出impossible.将poj1606的代码略作修改即可. 代码 1 #incl

poj3414 Pots(BFS+路径输出)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj.org/problem?id=3414 此题和poj1606一样 :http://blog.csdn.net/u012860063/article/details/37772275 Description You are given two pots, having the volume of A and B liters respectively.

pots(BFS)

D - Pots Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Status Description You are given two pots, having the volume of A and B liters respectively. The following operations can be performed: Input On the first and

POJ 3414 Pots(罐子)

p.MsoNormal { margin-bottom: 10.0000pt; font-family: Tahoma; font-size: 11.0000pt } h1 { margin-top: 5.0000pt; margin-bottom: 5.0000pt; text-align: left; font-family: 宋体; font-weight: bold; font-size: 24.0000pt } span.10 { font-family: "Times New Rom

USACO抓牛catchcow (bfs)

这题是黄巨大出的比赛题. http://poj.org/problem?id=3278 Description Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤

hdu 1728 逃离迷宫 (BFS)

逃离迷宫 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 14376    Accepted Submission(s): 3458 Problem Description 给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,gloria可以穿越,有些地方

牛汇(BFS)入金具体流程(图文指导)

牛汇开户流程:bfsforex牛汇入金教程 所谓入金,也就是充值的意思,必须充钱到平台才能进行外汇交易.首先,我们先登录bfsforex牛汇官方网站,在交易办公室功能区域下面,点击账户入金: 为您提供中国各大银行的网银支付解决方案,支持人民币支付,和信用卡入金,入金是实时到账的. 牛汇(BFS)入金具体流程(图文指导),布布扣,bubuko.com