POJ-3414(Pots)——BFS

题目大意:给你两个容器的容量AB,经过以下操作后是否可以得到C量的水,如果可以输出倒水的步骤

FILL(i)        fill the pot i (1 ≤ ≤ 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).


解题思路:很显然题目要求最小倒水步骤,那么BFS为首选(其实DFS也可以,不过需要剪枝)。

    与普通BFS类似,开vis表示走过没(0还是非0)与第几步走过(>0)。

   x和y表示两个容器当前的水量。

   这里,我用数字来表示每一种操作,0表示FILL(1)1表示FILL(2)2表示DROP(1)3表示DROP(2)4表示POUR(1,2)5表示POUR(2,1)

    但是这里要输出步骤,就比较烦了。这里我推荐两种写法:

1、用C语言中数组模拟队列的格式写,有点啰嗦,而且没接触过的人可能一开始不会写

2、再开一个数组meh,表示对于(x,y)点,此点进入队列的编号,最后返回的时候就可以在队列记录(p数组)中查到之前的点。

      p数组是队列进入情况的数组,用pair结构,first表示当前操作是那种操作(0,1,2,3,4,5)second表示前一操作在p数组中的编号。

         我就是采用第二种方法,看起来写得很麻烦,但其实大部分只需复制粘贴然后略加调整。


代码:

#include <set>
#include <map>
#include <list>
#include <queue>
#include <stack>
#include <cmath>
#include <string>
#include <cstdio>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
//#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
#define PB push_back
#define FOR(i,n,m) for(int i=n;i<=m;i++)
#define ROF(i,n,m) for(int i=n;i>=m;i--)
#define clr(i,j) memset(i,j,sizeof(i))
#define maxn 105
typedef long long ll;
int a,b,c,ans;
int vis[maxn][maxn];
int meh[maxn][maxn];
pair<int,int>p[maxn*maxn];
char str[6][10]={"FILL(1)","FILL(2)","DROP(1)","DROP(2)","POUR(1,2)","POUR(2,1)"};
void output(int tem)
{
    if(tem!=0)
    {
        output(p[tem].second);
        printf("%s\n",str[p[tem].first]);
    }
}
void bfs()
{
    queue<int>q;
    vis[0][0]=1;
    p[ans].second=-1;
    meh[0][0]=ans++;
    q.push(0);
    while(!q.empty())
    {
        int w=q.front();
        q.pop();
        int x=w/1000,y=w%1000;
        if(x==c||y==c)
        {
            printf("%d\n",vis[x][y]-1);
            output(meh[x][y]);
            return;
        }
        if(!vis[a][y])
        {
            q.push(a*1000+y);
            vis[a][y]=vis[x][y]+1;
            p[ans].first=0;
            p[ans].second=meh[x][y];
            meh[a][y]=ans++;
        }
        if(!vis[x][b])
        {
            q.push(x*1000+b);
            vis[x][b]=vis[x][y]+1;
            p[ans].first=1;
            p[ans].second=meh[x][y];
            meh[x][b]=ans++;
        }
        if(!vis[0][y])
        {
            q.push(y);
            vis[0][y]=vis[x][y]+1;
            p[ans].first=2;
            p[ans].second=meh[x][y];
            meh[0][y]=ans++;
        }
        if(!vis[x][0])
        {
            q.push(x*1000);
            vis[x][0]=vis[x][y]+1;
            p[ans].first=3;
            p[ans].second=meh[x][y];
            meh[x][0]=ans++;
        }
        int f=min(x,b-y);
        if(!vis[x-f][y+f])
        {
            q.push((x-f)*1000+y+f);
            vis[x-f][y+f]=vis[x][y]+1;
            p[ans].first=4;
            p[ans].second=meh[x][y];
            meh[x-f][y+f]=ans++;
        }
        f=min(a-x,y);
        if(!vis[x+f][y-f])
        {
            q.push((x+f)*1000+y-f);
            vis[x+f][y-f]=vis[x][y]+1;
            p[ans].first=5;
            p[ans].second=meh[x][y];
            meh[x+f][y-f]=ans++;
        }
    }
    puts("impossible");
}
int main()
{
    while(~scanf("%d %d %d",&a,&b,&c))
    {
        clr(vis,0);
        clr(meh,0);
        ans=0;
        bfs();
    }
    return 0;
}

  • FILL(i)        fill the pot i (1 ≤ ≤ 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).
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值