(BFS+打印路径)poj3414 Pots

本文详细介绍了如何使用广度优先搜索解决POJ3414 Pots问题,通过记录不同状态下容器的水量变化,寻找达到指定水量的最短操作序列。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

poj3414 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)

题解:代码2是将代码1中的结构体用pair来替代,使得结构更加清晰。由题干可知,有6种扩展方式。需要记录两个容器的剩余量,操作类型及操作次数,直接用两个容器的液体剩余量来表示状态。每拓展一次,判断两个容器中是否有液体剩余量为C的,有的话,输出扩展路径。如果所有路径都拓展完了,也没有满足条件的,直接输出‘impossible’。打印路径用回溯

代码1:

//感觉队列中的结构体可以用pair替代,明天有时间再详写
#include<iostream>
#include<cstdio>
#include<string.h>
#include<string>
#include<queue>
using namespace std;
const int maxn=105;
int vis[maxn][maxn];
int A,B,C;
char dir[6][10]={"FILL(1)","FILL(2)","DROP(1)","DROP(2)","POUR(1,2)","POUR(2,1)"};
typedef struct Node{
    int sum,x,y;
    int op;
}Node;
Node pre[maxn][maxn];  //记录路径,pre[i][j]表示第一个容器的现存水量为i,第二个容器的现存水量为j;(pre[i][j].x,pre[i][j].y)表示到达这一状态之前的状态

void Input(int a,int b){
    if(a==0&&b==0)
        return ;
    Input(pre[a][b].x,pre[a][b].y);
    printf("%s\n",dir[pre[a][b].op]);
}

void bfs(int a,int b){
    memset(vis,0,sizeof(vis));
    queue<Node> q;
    Node tmp,nxt;
    tmp.x=0;
    tmp.y=0;
    tmp.sum=0;
    vis[0][0]=1;
    q.push(tmp);
    while(!q.empty()){
        tmp=q.front();
        q.pop();
        if(tmp.x==C||tmp.y==C){
            printf("%d\n",tmp.sum);
            Input(tmp.x,tmp.y);
            return ;
        }
        //FILL(1)
        if(!vis[A][tmp.y]){
            vis[A][tmp.y]=1;
            nxt.x=A;
            nxt.y=tmp.y;
            nxt.sum=tmp.sum+1;
            q.push(nxt);
            pre[nxt.x][nxt.y].x=tmp.x;
            pre[nxt.x][nxt.y].y=tmp.y;
            pre[nxt.x][nxt.y].op=0;
        }
        //FILL(2)
        if(!vis[tmp.x][B]){
            vis[tmp.x][B]=1;
            nxt.x=tmp.x;
            nxt.y=B;
            nxt.sum=tmp.sum+1;
            q.push(nxt);
            pre[nxt.x][nxt.y].x=tmp.x;
            pre[nxt.x][nxt.y].y=tmp.y;
            pre[nxt.x][nxt.y].op=1;
        }
        //DROP(1)
        if(!vis[0][tmp.y]){
            vis[0][tmp.y]=1;
            nxt.x=0;
            nxt.y=tmp.y;
            nxt.sum=tmp.sum+1;
            q.push(nxt);
            pre[nxt.x][nxt.y].x=tmp.x;
            pre[nxt.x][nxt.y].y=tmp.y;
            pre[nxt.x][nxt.y].op=2;
        }
        //DROP(2)
        if(!vis[tmp.x][0]){
            vis[tmp.x][0]=1;
            nxt.x=tmp.x;
            nxt.y=0;
            nxt.sum=tmp.sum+1;
            q.push(nxt);
            pre[nxt.x][nxt.y].x=tmp.x;
            pre[nxt.x][nxt.y].y=tmp.y;
            pre[nxt.x][nxt.y].op=3;
        }
        //POUR(1,2)
        nxt.y=min(B,tmp.x+tmp.y);
        nxt.x=tmp.x-(nxt.y-tmp.y);
        if(!vis[nxt.x][nxt.y]){
            vis[nxt.x][nxt.y]=1;
            nxt.sum=tmp.sum+1;
            q.push(nxt);
            pre[nxt.x][nxt.y].x=tmp.x;
            pre[nxt.x][nxt.y].y=tmp.y;
            pre[nxt.x][nxt.y].op=4;
        }
        //POUR(2,1)
        nxt.x=min(A,tmp.x+tmp.y);
        nxt.y=tmp.y-(nxt.x-tmp.x);
        if(!vis[nxt.x][nxt.y]){
            vis[nxt.x][nxt.y]=1;
            nxt.sum=tmp.sum+1;
            q.push(nxt);
            pre[nxt.x][nxt.y].x=tmp.x;
            pre[nxt.x][nxt.y].y=tmp.y;
            pre[nxt.x][nxt.y].op=5;
        }
    }
    printf("impossible\n");
}


int main(){
    scanf("%d%d%d",&A,&B,&C);
    bfs(0,0);
    return 0;
}

代码2:

#include<iostream>
#include<cstdio>
#include<string.h>
#include<string>
#include<queue>
using namespace std;
const int maxn=105;
int vis[maxn][maxn];
int A,B,C;
char dir[6][10]={"FILL(1)","FILL(2)","DROP(1)","DROP(2)","POUR(1,2)","POUR(2,1)"};
typedef struct Node{
    int sum,x,y;
    int op;
}Node;
Node pre[maxn][maxn];  //记录路径,pre[i][j]表示第一个容器的现存水量为i,第二个容器的现存水量为j;(pre[i][j].x,pre[i][j].y)表示到达这一状态之前的状态

void Input(int a,int b){
    if(a==0&&b==0)
        return ;
    Input(pre[a][b].x,pre[a][b].y);
    printf("%s\n",dir[pre[a][b].op]);
}

void bfs(int a,int b){
    memset(vis,0,sizeof(vis));
    queue<pair<int,int> >q;
    pair<int,int> tmp,nxt;
    pre[0][0].sum=0;
    vis[0][0]=1;
    q.push(make_pair(0,0));
    while(!q.empty()){
        tmp=q.front();
        q.pop();
        if(tmp.first==C||tmp.second==C){
            printf("%d\n",pre[tmp.first][tmp.second].sum);
            Input(tmp.first,tmp.second);
            return ;
        }
        //FILL(1)
        if(!vis[A][tmp.second]){
            vis[A][tmp.second]=1;
            nxt.first=A;
            nxt.second=tmp.second;
            pre[nxt.first][nxt.second].sum=pre[tmp.first][tmp.second].sum+1;
            q.push(nxt);
            pre[nxt.first][nxt.second].x=tmp.first;
            pre[nxt.first][nxt.second].y=tmp.second;
            pre[nxt.first][nxt.second].op=0;
        }
        //FILL(2)
        if(!vis[tmp.first][B]){
            vis[tmp.first][B]=1;
            nxt.first=tmp.first;
            nxt.second=B;
            pre[nxt.first][nxt.second].sum=pre[tmp.first][tmp.second].sum+1;
            q.push(nxt);
            pre[nxt.first][nxt.second].x=tmp.first;
            pre[nxt.first][nxt.second].y=tmp.second;
            pre[nxt.first][nxt.second].op=1;
        }
        //DROP(1)
        if(!vis[0][tmp.second]){
            vis[0][tmp.second]=1;
            nxt.first=0;
            nxt.second=tmp.second;
            pre[nxt.first][nxt.second].sum=pre[tmp.first][tmp.second].sum+1;
            q.push(nxt);
            pre[nxt.first][nxt.second].x=tmp.first;
            pre[nxt.first][nxt.second].y=tmp.second;
            pre[nxt.first][nxt.second].op=2;
        }
        //DROP(2)
        if(!vis[tmp.first][0]){
            vis[tmp.first][0]=1;
            nxt.first=tmp.first;
            nxt.second=0;
            pre[nxt.first][nxt.second].sum=pre[tmp.first][tmp.second].sum+1;
            q.push(nxt);
            pre[nxt.first][nxt.second].x=tmp.first;
            pre[nxt.first][nxt.second].y=tmp.second;
            pre[nxt.first][nxt.second].op=3;
        }
        //POUR(1,2)
        nxt.second=min(B,tmp.first+tmp.second);
        nxt.first=tmp.first-(nxt.second-tmp.second);
        if(!vis[nxt.first][nxt.second]){
            vis[nxt.first][nxt.second]=1;
            pre[nxt.first][nxt.second].sum=pre[tmp.first][tmp.second].sum+1;
            q.push(nxt);
            pre[nxt.first][nxt.second].x=tmp.first;
            pre[nxt.first][nxt.second].y=tmp.second;
            pre[nxt.first][nxt.second].op=4;
        }
        //POUR(2,1)
        nxt.first=min(A,tmp.first+tmp.second);
        nxt.second=tmp.second-(nxt.first-tmp.first);
        if(!vis[nxt.first][nxt.second]){
            vis[nxt.first][nxt.second]=1;
            pre[nxt.first][nxt.second].sum=pre[tmp.first][tmp.second].sum+1;
            q.push(nxt);
            pre[nxt.first][nxt.second].x=tmp.first;
            pre[nxt.first][nxt.second].y=tmp.second;
            pre[nxt.first][nxt.second].op=5;
        }
    }
    printf("impossible\n");
}


int main(){
    scanf("%d%d%d",&A,&B,&C);
    bfs(0,0);
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值