Pots

Description
You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:

FILL(i) fill the pot i (1 ≤ i ≤ 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).
Write a program to find the shortest possible sequence of these operations that will yield exactly C liters of water in one of the pots.

Input
On the first and only line are the numbers A, B, and C. These are all integers in the range from 1 to 100 and C≤max(A,B).

Output
The first line of the output must contain the length of the sequence of operations K. The following K lines must each describe one operation. If there are several sequences of minimal length, output any one of them. If the desired result can’t be achieved, the first and only line of the file must contain the word ‘impossible’.

Sample Input
3 5 4
Sample Output
6
FILL(2)
POUR(2,1)
DROP(1)
POUR(2,1)
FILL(2)
POUR(2,1)
容量为a,b的杯子,可以装满,倒掉,互相倒水的操作。问最少有c容量的次数以及步骤。这个根据之前的思路,就是bfs最短路,但是这个路径很蛋疼,之前做的迷宫问题用dfs,但这个裸用显然是很麻烦的,要讨论很多。看网上有人是加了一个步骤代号,就是1,2,3,4分别代表什么操作的,但是我想了一下,还是要讨论,比如a倒向b,根据a容量不一样还原也是不一样的。
如果写操作数的话,为什么不直接写上一个的状态?就是类似于链表的方法,用结构体来实现,契合的很好。
小提普斯:一定要加IMPOSSIBLE!!!

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<map>
#include<queue>
using namespace std;
int leap[105][105], templeap, a, b, c;
struct Node{
    int x;
    int y;
    int ans;
    int prex;
    int prey;
}node[105][105];
queue<Node>p;
void dfs(int x, int y)
{
    int tempx, tempy;
    if (x == 0 && y == 0)
        templeap = 1;
    else{
        dfs(node[x][y].prex, node[x][y].prey);
        if (templeap)
        {
            tempx = node[x][y].prex; tempy = node[x][y].prey;
            if (tempx == x&&y == b)
                cout << "FILL(2)" << endl;
            else if (tempy == y&&x == a)
                cout << "FILL(1)" << endl;
            else if (tempx == x&&y == 0)
                cout << "DROP(2)" << endl;
            else if (x == 0 && tempy == y)
                cout << "DROP(1)" << endl;
            else if ((tempx + tempy) == (x + y) && tempx < x)
                cout << "POUR(2,1)" << endl;
            else if ((tempx + tempy) == (x + y) && tempy < y)
                cout << "POUR(1,2)" << endl;
        }
    }
}
int main()
{
    int i, j, m, n, ans,  tempx, tempy, flag;
    Node temp, now;
    cin >> a >> b >> c;
    for (i = 0; i <= a; i++)
    for (j = 0; j <= b; j++)
    {
        node[i][j].x = i; node[i][j].y = j;
        node[i][j].prex = node[i][j].prey = -1;
        node[i][j].ans = 10005;
        leap[i][j] = 0;
    }
    node[0][0].ans = 0;
    leap[0][0] = 1; flag = 0;
    p.push(node[0][0]);
    while (p.size())
    {
        temp = p.front();
        if (temp.x == c || temp.y == c)
        {
            tempx = temp.x; tempy = temp.y;
            flag = 1; break;
        }
        else{
            if (temp.x != a)
            {
                now.x = a; now.y = temp.y;
                if (!leap[now.x][now.y])
                {
                    node[now.x][now.y].ans = temp.ans + 1;
                    node[now.x][now.y].prex = temp.x;
                    node[now.x][now.y].prey = temp.y;
                    p.push(node[now.x][now.y]);
                    leap[now.x][now.y] = 1;
                }
            }
            if (temp.y != b)
            {
                now.x = temp.x; now.y = b;
                if (!leap[now.x][now.y])
                {
                    leap[now.x][now.y] = 1;
                    node[now.x][now.y].ans = temp.ans + 1;
                    node[now.x][now.y].prex = temp.x;
                    node[now.x][now.y].prey = temp.y;
                    p.push(node[now.x][now.y]);
                }
            }
            if (temp.x != 0)
            {
                now.x = 0; now.y = temp.y;
                if (!leap[now.x][now.y])
                {
                    leap[now.x][now.y] = 1;
                    node[now.x][now.y].ans = temp.ans + 1;
                    node[now.x][now.y].prex = temp.x;
                    node[now.x][now.y].prey = temp.y;
                    p.push(node[now.x][now.y]);
                }
            }
            if (temp.y != 0)
            {
                now.x = temp.x; now.y = 0;
                if (!leap[now.x][now.y])
                {
                    leap[now.x][now.y] = 1;
                    node[now.x][now.y].ans = temp.ans + 1;
                    node[now.x][now.y].prex = temp.x;
                    node[now.x][now.y].prey = temp.y;
                    p.push(node[now.x][now.y]);
                }
            }
            if (temp.x + temp.y >= a)
            {
                now.x = a; now.y = temp.y - (a - temp.x);
                if (!leap[now.x][now.y])
                {
                    leap[now.x][now.y] = 1;
                    node[now.x][now.y].ans = temp.ans + 1;
                    node[now.x][now.y].prex = temp.x;
                    node[now.x][now.y].prey = temp.y;
                    p.push(node[now.x][now.y]);
                }
            }
            if (temp.x + temp.y < a)
            {
                now.x = temp.x + temp.y; now.y = 0;
                if (!leap[now.x][now.y])
                {
                    leap[now.x][now.y] = 1;
                    node[now.x][now.y].ans = temp.ans + 1;
                    node[now.x][now.y].prex = temp.x;
                    node[now.x][now.y].prey = temp.y;
                    p.push(node[now.x][now.y]);
                }
            }
            if (temp.x + temp.y >= b)
            {
                now.x = temp.x - (b - temp.y); now.y = b;
                if (!leap[now.x][now.y])
                {
                    leap[now.x][now.y] = 1;
                    node[now.x][now.y].ans = temp.ans + 1;
                    node[now.x][now.y].prex = temp.x;
                    node[now.x][now.y].prey = temp.y;
                    p.push(node[now.x][now.y]);
                }
            }
            if (temp.x + temp.y < b)
            {
                now.x = 0; now.y = temp.x + temp.y;
                if (!leap[now.x][now.y])
                {
                    leap[now.x][now.y] = 1;
                    node[now.x][now.y].ans = temp.ans + 1;
                    node[now.x][now.y].prex = temp.x;
                    node[now.x][now.y].prey = temp.y;
                    p.push(node[now.x][now.y]);
                }
            }
            p.pop();
        }
    }
    if (flag)
    {
        cout << temp.ans << endl;
        templeap = 0;
        dfs(tempx, tempy);
    }
    else
    {
        cout << "impossible" << endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值