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;
}
 

猜你喜欢

转载自blog.csdn.net/weixin_42137874/article/details/82710423
今日推荐