POJ_3414

问题描述

题目大意 给定2个容器的最大容量和一个目标量问能否通过特定操作使2容器中任意一个容器的水达到目标容量,输出过程
特定操作:
FILL(i) :将第i个容器装满水
DROP(i) :将第i个容器的水倒掉
POUR(i,j):将第i个容器的水倒入第j个容器中,若j满了,则停止;(没满倒满)

显然是一道bfs+记录路径的题
记录路径方法:
将方法转化为string数组储存

string op[] = {"FILL(1)", "FILL(2)", "POUR(2,1)", "POUR(1,2)", "DROP(1)", "DROP(2)"};

用ops数组记录op编号

struct node
{
    int step;
    int x;
    int y;
    int ops[1000];
}

细节注意!
找到一个状态时记得要将路径进行传承!

 for (int j = 0; j < now.step; j++)
 {
    nxt.ops[j] = now.ops[j];
 }

完整代码:

//#include<cstdio>
#include <iostream>
#include <queue>
#include <cstring>
#include <string>
#include <algorithm>
//#include<windows.h>
#include <vector>

using namespace std;
int is[400][400] = {0};
struct node
{
    int step;
    int x;
    int y;
    int ops[1000];
} now, pre, nxt, gg;

string op[] = {"FILL(1)", "FILL(2)", "POUR(2,1)", "POUR(1,2)", "DROP(1)", "DROP(2)"};

int x_m, y_m, c;

queue<node> q;

node bfs()
{
    pre.x = 0;
    pre.y = 0;
    pre.step = 0;
    q.push(pre);
    is[0][0] = 1;
    while (!q.empty())
    {
        now = q.front();
        q.pop();
        if (now.x == c || now.y == c)
        {
            return now;
        }
        for (int i = 0; i < 6; i++)
        {
            for (int j = 0; j < now.step; j++)
            {
                nxt.ops[j] = now.ops[j];
            }
            if (i == 0)
            {
                nxt.x = x_m;
                nxt.y = now.y;
            }
            if (i == 1)
            {
                nxt.y = y_m;
                nxt.x = now.x;
            }
            if (i == 2)
            {
                if (now.y + now.x >= x_m)
                {
                    nxt.x = x_m;
                    nxt.y = now.x + now.y - nxt.x;
                }
                else
                {
                    nxt.y = 0;
                    nxt.x = now.y + now.x;
                }
            }
            if (i == 3)
            {
                if (now.y + now.x >= y_m)
                {
                    nxt.y = y_m;
                    nxt.x = now.x + now.y - nxt.y;
                }
                else
                {
                    nxt.x = 0;
                    nxt.y = now.x + now.y;
                }
            }
            if (i == 4)
            {
                nxt.x = 0;
                nxt.y = now.y;
            }
            if (i == 5)
            {
                nxt.y = 0;
                nxt.x = now.x;
            }
            if (is[nxt.x][nxt.y] == 0 && nxt.x <= x_m && nxt.y <= y_m)
            {
                //cout<<op[i]<<":\n";
                //cout<<"x: "<<nxt.x<<"  y: "<<nxt.y<<endl;
                nxt.step = now.step + 1;
                nxt.ops[now.step] = i;
                q.push(nxt);
                is[nxt.x][nxt.y] = 1;
            }
        }
        //system("pause");
        // system("cls");
    }
    return gg;
}

int main()
{
    gg.step = 0;
    memset(is, 0, sizeof(is));
    cin >> x_m >> y_m >> c;
    node anc = bfs();
    if (anc.step == 0)
    {
        cout << "impossible";
    }
    else
    {
        cout << anc.step << endl;
        cout << op[anc.ops[0]];
        for (int i = 1; i < anc.step; i++)
        {
            cout << endl;
            cout << op[anc.ops[i]];
        }
        //system("cls");
    }
    return 0;
}
发布了7 篇原创文章 · 获赞 0 · 访问量 52

猜你喜欢

转载自blog.csdn.net/Doneoll/article/details/104429921