ZOJ 1005 Jugs

题意 给定两个两个容量分别为 a,b 的水壶,现在给定六种操作

  • fill A
  • fill B
  • empty A
  • empty B
  • pour A B
  • pour B A

现在问需要如何操作使得任意杯子里面的水有 n 加仑
思路:简单的bfs,用一个vis[1000][1000]数组来记录某种状态是不是已经经历过了,另外 node 节点中一个数组用来记录操作的方法。

#include <cstdio>
#include <string>
#include<iostream>
#include<vector>
#include <stack>
#include <queue>
#include <map>
#include <cstdlib>
#include<string.h>
#include <cstring>
#include <ctime>
#include <algorithm>
#include <set>

using namespace std;

typedef long long ll;
typedef pair<int, int>pii;
typedef pair<ll, ll> pll;
typedef pair<int, ll> pil;
typedef vector<vector<ll> >vvi;
typedef vector<ll> vi;

struct node
{
    int x, y;
    vector<int>op;
};
const int MAXN = 1010;
bool vis[MAXN][MAXN];

int main()
{
    int a, b, n;
    while (scanf("%d%d%d", &a, &b, &n) != EOF)
    {
        memset(vis, false, sizeof vis);
        queue<node>q;
        node start;
        start.x = 0, start.y = 0;
        q.push(start);
        node ans;
        ans.x = -1;
        while (!q.empty())
        {
            node tmp = q.front();
            q.pop();
            if (vis[tmp.x][tmp.y])continue;
            vis[tmp.x][tmp.y] = true;
            if (tmp.x == n || tmp.y == n)
            {
                ans = tmp;
                break;
            }
            node in = tmp;
            in.x = a;
            in.op.push_back(0);
            q.push(in);
            in = tmp;
            in.y = b;
            in.op.push_back(1);
            q.push(in);
            in = tmp;
            in.x = 0;
            in.op.push_back(2);
            q.push(in);
            in = tmp;
            in.y = 0;
            in.op.push_back(3);
            q.push(in);
            in = tmp;
            int x = a - in.x;
            if (x > in.y)
                in.x += in.y, in.y = 0;
            else in.x = a, in.y -= x;
            in.op.push_back(5);
            q.push(in);
            in = tmp;
            x = b - in.y;
            if (x > in.x)
                in.y += in.x, in.x = 0;
            else in.y = b, in.x -= x;
            in.op.push_back(4);
            q.push(in);
        }
        vector<int> &v = ans.op;
        for (int i = 0; i < v.size(); i++)
        {

            if (v[i] == 0)puts("fill A");
            else if (v[i] == 1)puts("fill B");
            else if (v[i] == 2)puts("empty A");
            else if (v[i] == 3)puts("empty B");
            else if (v[i] == 4)puts("pour A B");
            else if (v[i] == 5)puts("pour B A");
        }
        puts("success");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值