码迷,mamicode.com
首页 > 其他好文 > 详细

poj3414——Pots(BFS)

时间:2016-07-30 16:51:22      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:

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;
}

poj3414——Pots(BFS)

标签:

原文地址:http://blog.csdn.net/blue_skyrim/article/details/52073021

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!