• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
jacklee404
Never Stop!
博客园    首页    新随笔    联系   管理    订阅  订阅
POJ-3414-Pots BFS+回溯

题目

Pots

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 37309 Accepted: 15083 Special Judge

Description

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

  1. FILL(i) fill the pot i (1 ≤ i ≤ 2) from the tap;
  2. DROP(i) empty the pot i to the drain;
  3. 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)

思路 BFS + 回溯

​ 一开始看到这道题很难想到是BFS,这里总结一下,假设我们不考虑抽象为图的情况下,BFS可以理解为遍历每一种状态, 然后找到到达某个状态的最短路,其实01迷宫也好,八数码也罢其实他们都可以归到这个大类里面,即通过状态转移条件,然后从源状态找到目的状态的最短路。

​ 这道题的难点在于思考和条件判断,BFS的时候有诸多条件判断。

​ 这里回溯在写写,BFS的回溯其实就是记录每个点和其父亲结点,然后通过字节点递归到原始结点。

Code

#include <iostream>
#include <queue>
#include <utility>

using namespace std;

typedef pair<int, int> PII;


int a, b, c;

bool st[200][200];

struct road{
        int x, y;
        string act;
}path[200][200];


int ax, ay, ans = 0;


void bfs() {
        queue<PII> q1;
        pair<int, int> p1;
        q1.push(make_pair(0, 0));
        while (q1.size()) {
                pair<int, int> t = q1.front(); q1.pop();
                //cout << t.first << " " << t.second << endl;
                if (t.first == c || t.second == c) {
                        ax = t.first, ay = t.second;
                        //cout << ax << " " << ay << endl;
                        return;
                }

                st[t.first][t.second] = true;

                if (t.first < a || t.second < b) {
                        if (t.first < a) {
                                if (!st[a][t.second]) {
                                        q1.push(make_pair(a, t.second));
                                        path[a][t.second].x = t.first, path[a][t.second].y = t.second;
                                        path[a][t.second].act = "FILL(1)";
                                }
                        }

                        if (t.second < b) {
                                if (!st[t.first][b]) {
                                        q1.push(make_pair(t.first, b));
                                        path[t.first][b].x = t.first, path[t.first][b].y = t.second;
                                        path[t.first][b].act = "FILL(2)";
                                }
                        }
                }

                if (t.first || t.second) {
                        if (t.first) {
                                if (!st[0][t.second]) {
                                        q1.push(make_pair(0, t.second));
                                        path[0][t.second].x = t.first, path[0][t.second].y = t.second;
                                        path[0][t.second].act = "DROP(1)";
                                }
                        }
                        if (t.second) {
                                if (!st[t.first][0]) {
                                        q1.push(make_pair(t.first, 0));
                                        path[t.first][0].x = t.first, path[t.first][0].y = t.second;
                                        path[t.first][0].act = "DROP(2)";
                                }
                        }
                }

                if (t.second < b) {
                        int x, y;
                        if (t.second + t.first > b)
                                x = t.first - b + t.second, y = b;
                        else
                                x = 0, y = t.first + t.second;
                        if (!st[x][y]) {
                                q1.push(make_pair(x, y));
                                path[x][y].x = t.first, path[x][y].y = t.second;
                                path[x][y].act = "POUR(1,2)";
                        }
                }

                if (t.first < a) {
                        int x, y;
                        if (t.second + t.first > a)
                                y = t.second - a + t.first, x = a;
                        else
                                y = 0, x = t.first + t.second;
                        if (!st[x][y]) {
                                q1.push(make_pair(x, y));
                                path[x][y].x = t.first, path[x][y].y = t.second;
                                path[x][y].act = "POUR(2,1)";
                        }
                }
        }
}


void print(int x, int y) {
        if (x == 0 && y == 0) {
                cout << ans << endl;
                return;
                //cout << path[x][y].act << endl;
        } else {
                ans ++;
                print(path[x][y].x, path[x][y].y);
        }
        cout << path[x][y].act << endl;
}

int main() {
        ax = -1, ay = -1;
        cin >> a >> b >> c;
        bfs();
        if (ax == -1 && ay == -1)
                cout << "impossible" << endl;
        else {
                print(ax, ay);
        }
}
posted on 2023-01-13 13:40  Jack404  阅读(10)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3