Pots--(dfs)

Problem 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
<p>On the first and only line are the numbers <b>A</b>, <b>B</b>, and <b>C</b>. These are all integers in the range from 1 to 100 and <b>C</b>≤max(<b>A</b>,<b>B</b>).</p>
 

Output
<p>The first line of the output must contain the length of the sequence of operations <b>K</b>. The following <b>K</b> 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 ‘<b>impossible</b>’.</p>
 

Sample Input
  
  
3 5 4
 

Sample Output
  
  
6 FILL(2) POUR(2,1) DROP(1) POUR(2,1) FILL(2) POUR(2,1)

题意:给出两个容量为A和B的桶,问是否能够通过一系列操作使得某个桶内水的体积能达到C,

解题思路:感觉好像dfsbfs都行,不过dfs代码好敲,一直还在害怕dfs会超时,这个题目错了几次错在数组的大小上

代码:

#include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<queue>
#include<cstring>
using namespace std;
int aa,bb,cc,num;
bool flag,ok[201][201]; //判断是否出现过当前体积状态
string s[1000],ss[1000];  //s用来dfs,ss存放最终结果
void dfs(int a,int b,int step)  //a,b是当前体积,step是步数,因为没有一个固定的结束,用step来控制回溯
{
    if(a==cc||b==cc)
    {
        flag=true;
        if((step-1)<num)  //组成c后还要判断是不是最短路径
        {
            num=step-1;
            for(int i=1;i<step;i++)
                ss[i]=s[i];
        }
        return;
    }
    if(step>4000) { return; }
    if(ok[a][bb]==false)
    {
        ok[a][bb]=true;
        s[step]="FILL(2)";
        dfs(a,bb,step+1);
        ok[a][bb]=false;
    }
    if(ok[aa][b]==false)
    {
        ok[aa][b]=true;;
        s[step]="FILL(1)";
        dfs(aa,b,step+1);
        ok[aa][b]=false;
    }
    if(!ok[a][0])
    {
        ok[a][0]=true;
        s[step]="DROP(2)";
        dfs(a,0,step+1);
        ok[a][0]=false;
    }
    if(!ok[0][b])
    {
        ok[0][b]=true;
        s[step]="DROP(1)";
        dfs(0,b,step+1);
        ok[0][b]=false;
    }
    int a1=a,b1=b;
    if((b1+a1)<=bb) {b1=b1+a1; a1=0;}  //a桶倒光了
    else {a1=a1-(bb-b1); b1=bb;} //b桶倒满了
    if(!ok[a1][b1])
    {
        ok[a1][b1]=true;
        s[step]="POUR(1,2)";
        dfs(a1,b1,step+1);
        ok[a1][b1]=false;;
    }
    a1=a,b1=b;
    if((b1+a1)<=aa) {a1=b1+a1; b1=0;}
    else {b1=b1-(aa-a1); a1=aa;}
    if(!ok[a1][b1])
    {
        ok[a1][b1]=true;
        s[step]="POUR(2,1)";
        dfs(a1,b1,step+1);
        ok[a1][b1]=false;;
    }
}
int main()
{
    scanf("%d%d%d",&aa,&bb,&cc);
    memset(ok,false,sizeof(ok));
    num=10000000; flag=false;
    dfs(0,0,1);
    if(flag)
    {
        printf("%d\n",num);
        for(int i=1;i<=num;i++)
            cout<<ss[i]<<endl;;
    }
    else
    {
        printf("impossible\n");
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值