POJ 3414 POTS BFS

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.

InputOn 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).
OutputThe 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 Input3 5 4
Sample Output6
FILL(2)
POUR(2,1)
DROP(1)
POUR(2,1)
FILL(2)

POUR(2,1)


题意:就是给你2个瓶子要得到给定的升水,有六个操作:

1.充满瓶1;2.充满瓶2;3. 把瓶1 的水倒入瓶2;4把瓶2的水倒入瓶1;5清空瓶1 ;6清空瓶2

思路:刚开始以为每次都有六种操作,就直接让这种状态进入队列,然后提交显示内存超限了,又改了一下以为是有些操作没有成功进入队列,譬如1瓶的水为0 ,那他就没必要在进行清空之类的操作了,加上之后还是内存超限,后来觉得如果能够标记掉之前出现过的状态就好了,所以用2维数组标记之前出现过的状态。。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>

using namespace std;

int a,b,c;
struct point {
    int va;
    int vb;
    int step;
    int op[1005];
}p;
int visit[105][105];

int bfs(){
    queue <point> q;
    p.va = 0;
    p.vb = 0;
    p.step = 0;
    p.op[p.step] = 0;
    visit[p.va][p.vb] = 1;
    q.push (p);
    point k;
    while(!q.empty()){
        p = q.front();
        q.pop();
        if(p.va == c || p.vb == c){
            printf("%d\n",p.step);
            for(int j = 1; j <= p.step ;++j){
                switch(p.op[j]){
                    case 1:printf("FILL(1)\n");break;
                    case 2:printf("FILL(2)\n");break;
                    case 3:printf("POUR(1,2)\n");break;
                    case 4:printf("POUR(2,1)\n");break;
                    case 5:printf("DROP(1)\n");break;
                    case 6:printf("DROP(2)\n");break;
                }
            }
            return 1;
        }
        for(int i = 1; i < 7 ; ++i){//六种操作
            k = p;
            int sum;
            switch(i){
               case 1:
                   k.va = a;
                   k.step += 1 ;
                   k.op[k.step] = 1 ;
                   break;
               case 2:
                   k.vb = b;
                   k.step += 1 ;
                   k.op[k.step] = 2 ;
                   break;
               case 3:
                   sum = k.va + k.vb ;
                   if(sum <= b){
                    k.vb = sum;
                    k.va = 0;
                   }
                   else{
                    k.va = sum - b;
                    k.vb = b;
                   }
                   k.step += 1;
                   k.op[k.step] = 3 ;
                   break;
               case 4:
                   sum = k.va + k.vb ;
                   if(sum <= a){
                    k.va = sum;
                    k.vb = 0;
                   }
                   else{
                    k.vb = sum - a;
                    k.va = a;
                   }
                   k.step += 1;
                   k.op[k.step] = 4 ;
                   break;
               case 5:
                   k.va = 0;
                   k.step += 1 ;
                   k.op[k.step] = 5 ;
                   break;
               case 6:
                   k.vb = 0;
                   k.step += 1 ;
                   k.op[k.step] = 6 ;
                   break;
            }
            if(visit[k.va][k.vb] == 0){
              q.push (k);
              visit[k.va][k.vb] = 1;
            }
        }
    }
    return 0;
}

int main(){
    while(~scanf("%d%d%d",&a,&b,&c)){
        memset(visit,0,sizeof(visit));
        if(!bfs()){
            printf("impossible\n");
        }
    }
    return 0;
}

这题还是值得注意的,菜鸟不太会bfs,继续学好bfs,fighting。。。。。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值