首页 > 其他分享 >kuangbin专题一 简单搜索 罐子(POJ-3414)

kuangbin专题一 简单搜索 罐子(POJ-3414)

时间:2023-04-15 19:13:35浏览次数:69  
标签:罐子 pot typedef POUR 3414 POJ kuangbin include FILL

Pots

Time Limit: 1000MS Memory Limit: 65536K

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 的罐子1和一个容量为 b 的罐子2,有六种操作:
(1)装满罐子 1FILL(1)
(2)装满罐子 2FILL(2)
(3)将罐子 1 倒空,DROP(1)
(4)将罐子 2 倒空,DROP(2)
(5)将罐子 1 倒入罐子 2,直至罐子 1 为空或者罐子 2 已满,POUR(1, 2)
(5)将罐子 2 倒入罐子 1,直至罐子 2 为空或者罐子 1 已满,POUR(2, 1)

求其中一个罐子达到容量 c 的最小步数以及具体步骤。



解题思路

明显的BFS求最短步数模型,不过有些细节需要注意,进行罐子互相倾倒的操作时需要注意边界,而且罐子的操作需要一一对应。其它地方基本上都是套用模板,总的来说还是简单题。

/*   一切都是命运石之门的选择  El Psy Kongroo  */
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<cmath>
#include<functional>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
typedef pair<double, double> pdd;
typedef pair<string, pii> psi;
typedef __int128 int128;
#define PI acos(-1.0)
#define x first
#define y second
const int inf = 0x3f3f3f3f, mod = 1e9 + 7;

const int N = 110;
struct node{
    int x, y;    //记录两个瓶子状态
    string s;    //记录到达状态的所有操作
};
string ops[6] = {"FILL(1)","FILL(2)","DROP(1)","DROP(2)","POUR(1,2)","POUR(2,1)"};
int dist[N][N], a, b, c;

void bfs(){
    memset(dist, 0x3f, sizeof(dist));
    node src = {0, 0, ""};
    queue<node> q; q.push(src);
    dist[0][0] = 0;

    while(!q.empty()){
        node cur = q.front();
        q.pop();

        int d = dist[cur.x][cur.y], x = cur.x, y = cur.y;
        //已经出现有一个瓶子达到c
        if(x == c || y == c){
            cout << d << endl;
            for(int i = 0; i < (int)cur.s.size(); i ++ ) cout << ops[cur.s[i] - '0'] << endl;
            return;
        }

        int t1 = min(x, b - y), t2 = min(a - x, y);  //瓶子1与瓶子2相互倒
        int dx[6] = {a - x, 0, -x, 0, -t1, t2};      //与ops一一对应
        int dy[6] = {0, b - y, 0, -y, t1, -t2};

        for(int k = 0; k < 6; k ++ ){
            int nx = x + dx[k], ny = y + dy[k];
            if(dist[nx][ny] == inf){   //每个状态至多访问一次
                char op = '0' + k;
                string ns = cur.s + op;
                node to = {nx, ny, ns};
                q.push(to);
                dist[nx][ny] = d + 1;
            }
        }
    }

    cout << "impossible" << endl;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    cin >> a >> b >> c;

    bfs();

    return 0;
}

标签:罐子,pot,typedef,POUR,3414,POJ,kuangbin,include,FILL
From: https://www.cnblogs.com/MAKISE004/p/17321663.html

相关文章

  • poj2750(线段树+复杂区间合并)
    PottedFlowerPOJ-2750思路:我们将题目简单化,假设我们要求的是序列的最大连续子段和,且可以包括所有数。我们的线段树需要维护这段区间的最大前缀和pre,最大后缀和suf,区间和sum,区间连续最大和mx。那么难点就在于如何由子节点更新父节点。我们可以知道,tr[p].sum=tr[lc].sum......
  • kuangbin专题一 简单搜索 找倍数(POJ-1426)
    FindTheMultipleTimeLimit:1000MS MemoryLimit:10000KDescriptionGivenapositiveintegern,writeaprogramtofindoutanonzeromultiplemofnwhosedecimalrepresentationcontainsonlythedigits0and1.Youmayassumethatnisnotgreaterth......
  • poj2777(线段树)
    CountColorPOJ-2777思路:暴力能过,线段树维护这个区间的颜色,如果是混色则置为1,如果是单一颜色则设为这个颜色,修改就是正常的区间修改,区间查询就要变一下。还有题解是用二进制做得,可以学一下。#define_CRT_SECURE_NO_WARNINGS1#include<algorithm>#include<fstream>#inc......
  • kuangbin专题一 简单搜索 翻转(POJ-3279)
    FliptileTimeLimit:2000MS MemoryLimit:65536KDescriptionFarmerJohnknowsthatanintellectuallysatisfiedcowisahappycowwhowillgivemoremilk.HehasarrangedabrainyactivityforcowsinwhichtheymanipulateanM×Ngrid(1≤M≤15;1≤......
  • poj 2182
    LostCowsPOJ-2182与这题一样BuyTickets-POJ2828-VirtualJudge(csgrandeur.cn)题意:有1~NN个数字,这N个数字的顺序是打乱的,从第二个数字开始给你它的前面有多少个数字比他小思路:输入的数字都要加一,然后我们从后往前遍历,在线段树中如果左子树的sum‘>sum,则进入左子......
  • kuangbin专题一 简单搜索 抓住那头牛(POJ-3278)
    CatchThatCowTimeLimit:2000MS MemoryLimit:65536KTotalSubmissions:210291 Accepted:63838DescriptionFarmerJohnhasbeeninformedofthelocationofafugitivecowandwantstocatchherimmediately.HestartsatapointN(0≤N≤100,000)on......
  • kuangbin专题一 简单搜索 棋盘问题(POJ-1321)
    棋盘问题TimeLimit:1000MS MemoryLimit:10000KTotalSubmissions:125427 Accepted:56304Description在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别。要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘......
  • kuangbin专题一 简单搜索 地牢大师(POJ-2251)
    DungeonMasterTimeLimit:1000MS MemoryLimit:65536KTotalSubmissions:92499 Accepted:31990DescriptionYouaretrappedina3Ddungeonandneedtofindthequickestwayout!Thedungeoniscomposedofunitcubeswhichmayormaynotbefilledwith......
  • POJ 1780 Code (欧拉回路+非递归版dfs)
    题目地址:POJ1780还是求序列的欧拉回路。只不过这题有两坑。第一坑是用数字来当点的话,会MLE,因为每个数字可以连10条边,100w条边会MLE,即使用vector也会TLE。这题可以用边来记录,对于n为1时直接输出,然后后面的,比如12,23这两个点就用边权值为123来表示这两个点,这样就把点和边的范围......
  • HDU 1116 && POJ 1386 Play on Words(欧拉路径)
    按字母来建边,每个单词的首字母和尾字母加边。先判断是否连通,然后判断每个字母的入度和出度不能出现差的绝对值大于2,然后入度和出度差的绝对值为1的不能超过两个。就可以形成欧拉路径代码如下:#include<iostream>#include<string.h>#include<math.h>#include<queue>#include......