POJ 3414 Pots(BFS)

Posted Annetree的博客

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ 3414 Pots(BFS)相关的知识,希望对你有一定的参考价值。

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

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)

怎么说呢,BFS,有六种状态
写起来还是有点烦的,要细心
#include<cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include<queue>;
using namespace std;
struct node
{
    int av,aw,bv,bw;
    int o[105],pos;
}s;
bool vis[105][105];
int f;
node cur,next;
void fill(int i)
{
    if(i==1)
        next.aw=next.av;
    else if(i==2)
        next.bw=next.bv;
}
void drop(int i)
{
    if(i==1)
        next.aw=0;
    else
        next.bw=0;
}
void pour(int a,int b)
{
    if(a==1&&b==2){
    if(next.aw>next.bv-next.bw)
    {
        next.aw-=(next.bv-next.bw);
        next.bw=next.bv;
    }
    else
    {
        next.bw+=next.aw;
        next.aw=0;
    }}
    else
    {
        if(next.bw>next.av-next.aw)
    {
        next.bw-=(next.av-next.aw);
        next.aw=next.av;
    }
    else
    {
        next.aw+=next.bw;
        next.bw=0;
    }
    }
}
void print(int i)
{
    if(i==1)
        printf("POUR(1,2)\n");
    else if(i==2)
        printf("POUR(2,1)\n");
    else if(i==3)
        printf("FILL(1)\n");
    else if(i==4)
        printf("FILL(2)\n");
    else if(i==5)
        printf("DROP(1)\n");
    else if(i==6)
        printf("DROP(2)\n");
}
bool bfs()
{
    memset(vis,false,sizeof(vis));
    s.aw=0;s.bw=0;
    s.pos=0;
    vis[0][0]=true;
    queue<node>Q;
    Q.push(s);
    while(!Q.empty())
    {
        cur=Q.front();
        Q.pop();
        if(f==cur.aw||f==cur.bw)
        {
            printf("%d\n",cur.pos);
            for(int i=0;i<cur.pos;i++)
            {
                print(cur.o[i]);
            }
            return true;
            break;
        }
        //1
        if(cur.aw!=0&&cur.bw!=cur.bv){
        next=cur;
        pour(1,2);
        next.o[next.pos]=1;
        next.pos++;
        if(!vis[next.aw][next.bw])
        {
            vis[next.aw][next.bw]=true;
            Q.push(next);
        }}
        //2
        if(cur.bw!=0&&cur.aw!=cur.av){
        next=cur;
        pour(2,1);
        next.o[next.pos]=2;
        next.pos++;
        if(!vis[next.aw][next.bw])
        {
            vis[next.aw][next.bw]=true;
            Q.push(next);
        }}
        //3
        if(cur.aw!=cur.av){
        next=cur;
        fill(1);
        next.o[next.pos]=3;
        next.pos++;
        if(!vis[next.aw][next.bw])
        {
            vis[next.aw][next.bw]=true;
            Q.push(next);
        }}
        //4
        if(cur.bw!=cur.bv){
        next=cur;
        fill(2);
        next.o[next.pos]=4;
        next.pos++;
        if(!vis[next.aw][next.bw])
        {
            vis[next.aw][next.bw]=true;
            Q.push(next);
        }}
        //5
        if(cur.aw!=0){
        next=cur;
        drop(1);
        next.o[next.pos]=5;
        next.pos++;
        if(!vis[next.aw][next.bw])
        {
            vis[next.aw][next.bw]=true;
            Q.push(next);
        }}
        //6
        if(cur.bw!=0){
        next=cur;
        drop(2);
        next.o[next.pos]=6;
        next.pos++;
        if(!vis[next.aw][next.bw])
        {
            vis[next.aw][next.bw]=true;
            Q.push(next);
        }}
    }
    return false;
}
int main()
{
    while(scanf("%d%d%d",&s.av,&s.bv,&f)!=EOF)
    {
        if(!bfs())printf("impossible\n");
    }
    return 0;
}

 

以上是关于POJ 3414 Pots(BFS)的主要内容,如果未能解决你的问题,请参考以下文章

poj3414Pots(bfs)

POJ 3414 Pots(BFS)

poj3414 Pots(BFS)

POJ 3414 Pots (BFS/DFS)

POJ_3414 Pots 复杂BFS

POJ - 3414 Pots (BFS+路径记录)