Pots-POJ 3414

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)

题目链接:

2.题意
给出三个数啊a,b,c。
两个杯子,a,b表示两个杯子的容量,有6种操作,a清空,b清空,a装满水,b装满水,a往b倒水,b往a倒水,c表示需要的容量,问最少经过多少次操作后两个被子中有一杯容量是c。

3.思路
有6种操作方式
按这6种方式bfs搜就完了

4.AC代码

#include"iostream"
#include"queue"
#include"cstring"
#include"cstdio"
#include"string"
using namespace std;
const int maxn = 105;
int a, b, c, f = 0;
int vis[maxn][maxn];//两个杯子的容量状态是否存在过
struct node{
    int x, y, step;//x:1号杯子的当前容量,y:2号杯子当前容量,累计操作数
    string s;//操作方式
};
string path[10] = {//6种操作方式
    ""
   , "FILL(1)"
   , "FILL(2)"
   , "DROP(1)"
   , "DROP(2)"
   , "POUR(1,2)"
   , "POUR(2,1)"
};
void bfs(){//bfs遍历
    memset(vis, 0, sizeof(vis));//将所有情况都赋0
    vis[0][0] = 1;
    queue<node>Q;
    node p;
    p.x = 0;
    p.y = 0;
    p.step = 0;
    p.s = "0";
    Q.push(p);
    while(!Q.empty()){
        node q = Q.front();
        Q.pop();
        if(q.x == c || q.y == c){//有一个杯子当前容量是c
            f = 1;
            printf("%d\n",q.step);//输出操作次数
            for(int i = 1 ; i < q.s.length(); i++)//输出操作内容
                printf("%s\n",path[q.s[i]-'0'].c_str());
            return ;
        }
        if(a > q.x){//将1号杯子装满(1号杯当前没满)
            if(!vis[a][q.y]){
                vis[a][q.y] = 1;
                p.x = a;
                p.y = q.y;
                p.step = q.step + 1;
                p.s = q.s + "1";
                Q.push(p);
            }
        }
        if(b > q.y){//将2号杯子装满(2号杯当前没满)
            if(!vis[q.x][b]){
                vis[q.x][b] = 1;
                p.x = q.x;
                p.y = b;
                p.step = q.step + 1;
                p.s = q.s + "2";
                Q.push(p);
            }
        }
        if(q.x){//将1号杯子清空(当前不为空)
            p.x = 0;
            p.y = q.y;
            p.step = q.step + 1;
            p.s = q.s + "3";
            if(!vis[0][p.y]){
                vis[0][p.y] = 1;
                Q.push(p);
            }
        }
        if(q.y){//将2号杯子清空(当前不为空)
            p.x = q.x;
            p.y = 0;
            p.step = q.step + 1;
            p.s = q.s + "4";
            if(!vis[p.x][0]){
                vis[p.x][0] = 1;
                Q.push(p);
            }
        }
        if(q.x && q.y < b){//1号杯往2号杯倒水
            if(q.x + q.y > b){
                p.x = q.x-(b-q.y);
                p.y = b;
            }
            else{
                p.x = 0;
                p.y = q.x + q.y;
            }
            p.step = q.step + 1;
            p.s = q.s + "5";
            if(!vis[p.x][p.y]){
                vis[p.x][p.y] = 1;
                Q.push(p);
            }
        }
        if(q.y && q.x < a){//2号杯往1号杯倒水
            if(q.x + q.y >a){
                p.y = q.y-(a-q.x);
                p.x = a;
            }
            else{
                p.y = 0;
                p.x = q.x + q.y;
            }
            p.step = q.step + 1;
            p.s = q.s + "6";
            if(!vis[p.x][p.y]){
            vis[p.x][p.y] = 1;
            Q.push(p);
        }
        }
    }
}
int main(){
    scanf("%d%d%d",&a,&b,&c);
    bfs();
    if(f==0)//如果没有出现满足条件的情况,输出------
        puts("impossible");
    return 0;
}

5.总结
这题是典型的bfs打印路径问题。
开始用cin,cout,RE了,改成了scanf,不知道为啥数据这么小还是爆了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值