POJ - 3414 Pots (倒水问题 BFS + 打印路径)

22 篇文章 0 订阅
16 篇文章 0 订阅
POJ - 3414 Pots
题目大意

就是给你A, B两个杯子,及他们的最大容量,三种操作方法,让你判断最少用多少此方法可以让任意一个被子里装有 C 升水。并打印路径。

分析

这里很容易想到广搜,设初始状态为(i,j),一共就只有六种变化
A杯倒满,B杯倒满,A杯倒出完,B杯倒出完,A到给B,B到给A。
任意一种状态i 或者 j达到 C 就停止,注意要打印路径,我这里用一个string存的操作顺序。

 #include <cstdio>
#include <vector>
#include <queue>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <iostream>
#include <algorithm>
#include <functional>
#include <stack>
#include <ctime>
#include <cstdlib>
//#pragma comment (linker, "/STACK:256000000")
using namespace std;
#define db(x) cout<<x<<endl

typedef long long ll;
const int N = 100 + 10;
const int INF = 0x3f3f3f;

int flag = 0;
int a, b, c;    //A, B, c, 盒子容量
int vis[N][N];  //保存容量状态
string str[10] = {"", "FILL(1)", "FILL(2)", "DROP(1)", "DROP(2)", "POUR(1,2)", "POUR(2,1)"};

struct node
{
    int x, y;   //A, B盒子容量
    int step;   //操作数
    string s;   //操作路径
    node(int x, int y, int step, string s):x(x),y(y),step(step),s(s){}
};

void bfs()
{
    queue<node> q;
    vis[0][0] = 1;
    q.push(node(0, 0, 0, "0"));
    while(!q.empty()){
        node temp = q.front();
        q.pop();
        if(temp.x == c || temp.y == c){
            flag = 1;
            cout << temp.step << endl;
            for (int i = 1; i < temp.s.length(); i++){
                cout << str[temp.s[i] - '0'] << endl;
            }
            return;
        }
        if(temp.x < a){ //将第一个瓶子装满
            if(!vis[a][temp.y]){
                vis[a][temp.y] = 1;
                q.push(node(a, temp.y, temp.step + 1, temp.s + "1"));
            }
        }
        if(temp.y < b){ //将第二个瓶子装满
            if(!vis[temp.x][b]){
                vis[temp.x][b] = 1;
                q.push(node(temp.x, b, temp.step + 1, temp.s + "2"));
            }
        }
        if(temp.x != 0){ //将第一个瓶子倒完
            if(!vis[0][temp.y]){
                vis[0][temp.y] = 1;
                q.push(node(0, temp.y, temp.step + 1, temp.s + "3"));
            }
        }
        if(temp.y != 0){ //将第二个瓶子装满
            if(!vis[temp.x][0]){
                vis[temp.x][0] = 1;
                q.push(node(temp.x, 0, temp.step + 1, temp.s + "4"));
            }
        }
        if(temp.x != 0 && temp.y < b){  //将第一个瓶子倒进第二个瓶子
            int xx, yy;
            if(temp.x <= b - temp.y){
                xx = 0;
                yy = temp.y + temp.x;
            }else{
                yy = b;
                xx = temp.x - b + temp.y;
            }
            if(!vis[xx][yy]){
                vis[xx][yy] = 1;
                q.push(node(xx, yy, temp.step + 1, temp.s + "5"));
            }
        }
        if(temp.y != 0 && temp.x < a){  //将第二个瓶子倒进第一个瓶子
            int xx, yy;
            if(temp.y <= a - temp.x){
                yy = 0;
                xx = temp.x + temp.y;
            }else{
                xx = a;
                yy = temp.y - a + temp.x;
            }
            if(!vis[xx][yy]){
                vis[xx][yy] = 1;
                q.push(node(xx, yy, temp.step + 1, temp.s + "6"));
            }
        }
    }
}

int main()
{
    cin >> a >> b >> c;
    bfs();
    if(!flag)
        cout << "impossible" << endl;
    return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值