POJ-3414 POTS(BFS打印路径)

8 篇文章 1 订阅

问题描述

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题,在没有做BFS类别中的迷宫问题时,我是不会做这个题的,当那个题目会了,这个题也就没什么问题了,并且还需要计算最少次数,那就正常的BFS就可以了。
打印路径关键是记录父节点状态。
一共6个operations,每个进行BFS就行。

代码如下;

#include<cstdio>
#include<queue>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn = 100+10;
int visited[maxn][maxn];
int A, B, C;
int enda, endb;
int ok;

struct State{
    int litera, literb;
    int step;
}begin;

struct Par{
    int x, y;
    int num;
}par[maxn][maxn];

/*初始化,很关键*/
void Init()
{
    ok = 0;
    memset(visited, 0, sizeof(visited));
    memset(par, 0, sizeof(par));
    visited[0][0] = 1;
    begin.litera = 0; begin.literb = 0; 
    begin.step = 0;
}

void BFS()
{
    queue<State>q;
    q.push(begin);
    while(!q.empty())
    {
        State u = q.front(); q.pop();
        if(u.litera == C || u.literb == C)
        {
            ok = 1;
            printf("%d\n",u.step); 
            enda = u.litera; endb = u.literb;
            return;
        }

        //FILL(A) 
        if(!visited[A][u.literb])
        {
            visited[A][u.literb] = 1;
            State v;
            v.litera = A; v.literb = u.literb;
            v.step = u.step + 1; 
            q.push(v);
            par[v.litera][v.literb].x = u.litera;
            par[v.litera][v.literb].y = u.literb;
            par[v.litera][v.literb].num = 1;
        }
        //FILL(B) 
        if(!visited[u.litera][B])
        {
            visited[u.litera][B] = 1;
            State v;
            v.litera = u.litera; v.literb = B;
            v.step = u.step + 1; 
            q.push(v);
            par[v.litera][v.literb].x = u.litera;
            par[v.litera][v.literb].y = u.literb;
            par[v.litera][v.literb].num = 2;
        }

        //DROP(A)      empty the pot A to the drain;
        if(!visited[0][u.literb])
        {
            visited[0][u.literb] = 1;
            State v;
            v.litera = 0; v.literb = u.literb;
            v.step = u.step + 1; 
            q.push(v);
            par[v.litera][v.literb].x = u.litera;
            par[v.litera][v.literb].y = u.literb;
            par[v.litera][v.literb].num = 3;
        }

        //DROP(B)      empty the pot B to the drain;
        if(!visited[u.litera][0])
        {
            visited[u.litera][0] = 1;
            State v;
            v.litera = u.litera; v.literb = 0;
            v.step = u.step + 1; 
            q.push(v);
            par[v.litera][v.literb].x = u.litera;
            par[v.litera][v.literb].y = u.literb;
            par[v.litera][v.literb].num = 4;
        }

        //POUR(A,B)
        int da, db;
        if(u.litera > B - u.literb)
        {
            da = u.litera - B + u.literb;
            db = B;
        }
        else if(u.litera <= B - u.literb)
        {
            da = 0;
            db = u.literb + u.litera;
        }
        if(!visited[da][db])
        {
//          printf("%d %d\n", da, db);
            visited[da][db] = 1;
            State v;
            v.litera = da;  v.literb = db;
            v.step = u.step + 1;
            q.push(v);

            /*记录父节点*/ 
            par[v.litera][v.literb].x = u.litera;
            par[v.litera][v.literb].y = u.literb;
            par[v.litera][v.literb].num = 5;
        }

        //POUR(B,A)
        if(u.literb > A - u.litera)
        {
            db = u.literb - A + u.litera;
            da = A;
        }
        else if(u.literb <= A - u.litera)
        {
            db = 0;
            da = u.litera + u.literb;
        }
        if(!visited[da][db])
        {
//          printf("%d %d\n", da, db);
            visited[da][db] = 1;
            State v;
            v.litera = da;  v.literb = db;
            v.step = u.step + 1;
            q.push(v);
            par[v.litera][v.literb].x = u.litera;
            par[v.litera][v.literb].y = u.literb;
            par[v.litera][v.literb].num = 6;
        }
    }
    printf("impossible\n");
    return; 
}
/*DFS打印路径*/
void DFS(int x, int y)
{
    if(x == 0 && y == 0) return;
    DFS(par[x][y].x, par[x][y].y);
    if(par[x][y].num == 1) printf("FILL(1)\n");
    else if(par[x][y].num == 2) printf("FILL(2)\n");
    else if(par[x][y].num == 3) printf("DROP(1)\n");
    else if(par[x][y].num == 4) printf("DROP(2)\n");
    else if(par[x][y].num == 5) printf("POUR(1,2)\n");
    else if(par[x][y].num == 6) printf("POUR(2,1)\n");
} 

int main()
{
    scanf("%d%d%d", &A, &B, &C);
    Init();
    BFS();
    if(ok == 1) 
    {
        DFS(enda, endb);//如果有解,则打印 
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值