倒水问题BFS

传送门.

题意

两个杯子容量为A,B,有6个操作
FILL(1) 装满A
FILL(2) 装满B
DROP(1) 倒掉A
DROP(2) 倒掉B
POUR(1,2) A倒给B,到B满为止
POUR(2,1) B倒给A,到A满为止
问最少多少次能有其中一个杯子里面有C升水。
输出相应操作。
bfs搜索6种情况,烦是真的烦。
直接看代码

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <vector>
#include <queue>
#define T int t;scanf("%d", &t);while(t--)
#define ll long long
using namespace std;
string path[] = {"FILL(1)","FILL(2)","DROP(1)","DROP(2)","POUR(1,2)","POUR(2,1)"};
struct node{
    int x1;
    int x2;
    int step;
    string sss;
}s[110][110];
int vis[500][500];
int main(){
    int a,b,c;
    scanf("%d %d %d", &a, &b, &c);
    queue<node> q;
    q.push({0,0,0});
    int flag = 0;
    while(!q.empty()){
        node old = q.front();
        q.pop();
        if(old.x1 == c || old.x2 == c){
            cout<<old.step<<endl;
            for(int i = 0 ; i < old.sss.length(); i ++){
                cout<<path[old.sss[i] - '0']<<endl;
            }
            flag = 1;
            break;
        }
        if(old.x1 != a){    //0
            node x;
            x.x1 = a;
            x.x2 = old.x2;
            x.step = old.step + 1;
            x.sss = old.sss + '0';
            if(vis[x.x1][x.x2] == 0){
                vis[x.x1][x.x2] = 1;
                q.push(x);
                s[x.x1][x.x2].sss = x.sss;
            } 
            // cout<<0<<endl;
        }
        if(old.x2 != b){    //1
            node x;
            x.x1 = old.x1;
            x.x2 = b;
            x.step = old.step + 1;
            x.sss = old.sss + '1';
            if(vis[x.x1][x.x2] == 0){
                vis[x.x1][x.x2] = 1;
                q.push(x);
                s[x.x1][x.x2].sss = x.sss;
            } 
            // cout<<1<<endl;
        } 
        if(old.x1 != 0){    //2
            node x;
            x.x1 = 0;
            x.x2 = old.x2;
            x.step = old.step + 1;
            x.sss = old.sss + '2';
            if(vis[x.x1][x.x2] == 0){
                vis[x.x1][x.x2] = 1;
                q.push(x);
                s[x.x1][x.x2].sss = x.sss;
            } 
            // cout<<2<<endl;
        }
        if(old.x2 != 0){    //3
            node x;
            x.x1 = old.x1;
            x.x2 = 0;
            x.step = old.step + 1;
            x.sss = old.sss + '3';
            if(vis[x.x1][x.x2] == 0){
                vis[x.x1][x.x2] = 1;
                q.push(x);
                s[x.x1][x.x2].sss = x.sss;
            } 
            // cout<<3<<endl;
        }
        if(old.x1 != 0 && old.x2 != b){         //4
            node x;
            x.x1 = max(0,old.x1 - (b - old.x2));
            x.x2 = min(b,old.x1 + old.x2);
            x.step = old.step + 1;
            x.sss = old.sss + '4';
            if(vis[x.x1][x.x2] == 0){
                vis[x.x1][x.x2] = 1;
                q.push(x);
                s[x.x1][x.x2].sss = x.sss;
            } 
            // cout<<4<<endl;
        }
        if(old.x1 != a && old.x2 != 0){         //5
            node x;
            x.x1 = min(a,old.x1 + old.x2);
            x.x2 = max(0,old.x2 - (a - old.x1));
            x.step = old.step + 1;
            x.sss = old.sss + '5';
            if(vis[x.x1][x.x2] == 0){
                vis[x.x1][x.x2] = 1;
                q.push(x);
                s[x.x1][x.x2].sss = x.sss;
            } 
            // cout<<5<<endl;
        }
    }
    if(!flag) cout<<"impossible"<<endl;

    return 0;
}

猜你喜欢

转载自blog.csdn.net/HHeyanjie/article/details/107596389
今日推荐