POJ - 3414 Pots(BFS 路径回溯)

传送门
分析:根上一题差不多,也是有6种操作方式

#include <set>
#include <map>
#include <cmath>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include<cstring>
#include <stdio.h>
#include <iostream>
#include <algorithm>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 1e2+5;
int n,m,k;
struct node{
    int x,y,step,pots;
};
node pre[maxn][maxn];
string s[6]={"FILL(1)","FILL(2)","DROP(1)","DROP(2)","POUR(1,2)","POUR(2,1)"};
bool vis[maxn][maxn];
void output(int x,int y){
       if(x==0&&y==0)
           return;
       node tem=pre[x][y];
       output(tem.x,tem.y);
       printf("%s\n",s[tem.pots].c_str());// 小技巧,.c_str()将string类转化为char类输出
}
void BFS()
{
    memset(vis,false,sizeof(vis));
    queue<node>q;
    q.push(node{0,0,0});
    while (!q.empty())
    {
        node now=q.front(); q.pop();
        if(now.x==k||now.y==k){ printf("%d\n",now.step);output(now.x,now.y);return; }
        int tx,ty;
        // FILL(1)
        if(n>now.x)
        {
            tx=n,ty=now.y;
            if(!vis[tx][ty])
            {
                vis[tx][ty] = true;
                q.push(node{tx, ty, now.step + 1, 0});
                pre[tx][ty]=now,pre[tx][ty].pots=0;
            }
        }
        // FILL(2)
        if(m>now.y)
        {
            tx=now.x,ty=m;
            if(!vis[tx][ty])
            {
                vis[tx][ty]=true;
                q.push(node{tx,ty,now.step+1,1});
                pre[tx][ty]=now,pre[tx][ty].pots=1;
            }
        }
        // DROP(1)
        if(now.x!=0)
        {
            tx=0,ty=now.y;
            if(!vis[tx][ty])
            {
                vis[tx][ty]=true;
                q.push(node{tx,ty,now.step+1,2});
                pre[tx][ty]=now,pre[tx][ty].pots=2;
            }
        }
        // DROP(2)
        if(now.y!=0)
        {
            tx=now.x,ty=0;
            if(!vis[tx][ty])
            {
                vis[tx][ty]=true;
                q.push(node{tx,ty,now.step+1,3});
                pre[tx][ty]=now;pre[tx][ty].pots=3;
            }
        }
        // POUR(1,2)
        if(now.x!=0)
        {
            if(now.x>m-now.y)
            {
                tx=now.x+now.y-m,ty=m;
                if(!vis[tx][ty])
                {
                    vis[tx][ty]=true;
                    q.push(node{tx,ty,now.step+1,4});
                    pre[tx][ty]=now;pre[tx][ty].pots=4;
                }
            } else{
                tx=0,ty=now.x+now.y;
                if(!vis[tx][ty]){
                    vis[tx][ty]=true;
                    q.push(node{tx,ty,now.step+1,4});
                    pre[tx][ty]=now,pre[tx][ty].pots=4;
                }
            }
        }
        // POUR(2,1)
        if(now.y!=0)
        {
             if(now.y>n-now.x)
             {
                 tx=n,ty=now.y+now.x-n;
                 if(!vis[tx][ty])
                 {
                     vis[tx][ty]=true;
                     q.push(node{tx,ty,now.step+1,5});
                     pre[tx][ty]=now,pre[tx][ty].pots=5;
                 }
             } else{
                 ty=0,tx=now.y+now.x;
                 if(!vis[tx][ty]){
                     vis[tx][ty]=true;
                     q.push(node{tx,ty,now.step+1,5});
                     pre[tx][ty]=now,pre[tx][ty].pots=5;
                 }
             }
        }
    }
    printf("impossible\n");
}
int main()
{
    scanf("%d%d%d",&n,&m,&k);
    BFS();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值