Pots(BFS+记录路径)

https://cn.vjudge.net/problem/POJ-3414

 

#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#include <cstdlib>
using namespace std;
struct node
{
    int x, y;
    int step;
    char st[110][110];
} t, f;
int visit[110][110];
void bfs(int a, int b, int c)
{
    queue <node> q;
    memset(visit, 0, sizeof(visit));
    t.x = a;
    t.y = 0;
    t.step = 1;
    strcpy(t.st[1],"FILL(1)");
    q.push(t);
    t.x = 0;
    t.y = b;
    t.step = 1;
    strcpy(t.st[1],"FILL(2)");
    q.push(t);
    visit[0][0] = 1;
    while(!q.empty())
    {
        f = q.front();
        q.pop();
        if(f.x==c||f.y==c)
        {
            printf("%d\n", f.step);
            for(int i=1; i<=f.step; i++)
            {
                printf("%s\n", f.st[i]);
            }
            return;
        }
        t = f;
        t.x = a;
        t.y = f.y;
        t.step++;
        strcpy(t.st[t.step], "FILL(1)");
        if(visit[t.x][t.y]==0)
        {
            visit[t.x][t.y] = 1;
            q.push(t);
        }
        t = f;
        t.x = f.x;
        t.y = b;
        t.step++;
        strcpy(t.st[t.step], "FILL(2)");
        if(visit[t.x][t.y]==0)
        {
            visit[t.x][t.y] = 1;
            q.push(t);
        }
        t = f;
        t.x = 0;
        t.y = f.y;
        t.step++;
        strcpy(t.st[t.step], "DROP(1)");
        if(visit[t.x][t.y]==0)
        {
            visit[t.x][t.y] = 1;
            q.push(t);
        }
        t = f;
        t.x = f.x;
        t.y = 0;
        t.step++;
        strcpy(t.st[t.step], "DROP(2)");
        if(visit[t.x][t.y]==0)
        {
            visit[t.x][t.y] = 1;
            q.push(t);
        }
        if(f.x+f.y>a)
        {
            t = f;
            t.x = a;
            t.y = f.x+f.y-a;
            t.step++;
            strcpy(t.st[t.step], "POUR(2,1)");
            if(visit[t.x][t.y]==0)
            {
                visit[t.x][t.y] = 1;
                q.push(t);
            }
        }
        else
        {
            t = f;
            t.x = f.x+f.y;
            t.y = 0;
            t.step++;
            strcpy(t.st[t.step], "POUR(2,1)");

        }
        if(visit[t.x][t.y]==0)
        {
            visit[t.x][t.y] = 1;
            q.push(t);
        }
        if(f.x+f.y>b)
        {
            t = f;
            t.x = f.x+f.y-b;
            t.y = b;
            t.step++;
            strcpy(t.st[t.step], "$$POUR(1,2)");
        }
        else
        {
            t = f;
            t.x = 0;
            t.y = f.x+f.y;
            t.step++;
            strcpy(t.st[t.step], "##POUR(1,2)");

        }
        if(visit[t.x][t.y]==0)
        {
            visit[t.x][t.y] = 1;
            q.push(t);
        }
    }
    printf("impossible\n");
    return ;
}
int main()
{
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    bfs(a, b, c);
    return 0;
}
 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值