Pots-简单的BFS问题

描述
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.

输入
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).

输出
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’.

样例输入
3 5 4
样例输出
6
FILL(2)
POUR(2,1)
DROP(1)
POUR(2,1)
FILL(2)

POUR(2,1)


题目大意:有2个pots,A,B分别表示2个pots的最大容积,要求通过几种操作方式,得到输入的C所需的容积。

代码部分:

#include <iostream>
#include <cstring>
#include <stdio.h>
using namespace std;
int visited[101][101];
int A,B,C;
struct Node{
    int a,b,opeat,parent,step;
    Node(int x,int y,int o,int p,int s):a(x),b(y),opeat(o),parent(p),step(s){}
    Node(){}
}que[10001];
int head=0,tail=0,step=0;
int opeator;


void print(int x){ //输出
    if(que[x].parent>=0)
        print(que[x].parent);
    switch(que[x].opeat){
        case 0:{
            printf("FILL(1)\n");
            break;
        }case 1:{
            printf("FILL(2)\n");
            break;
        }case 2:{
            printf("DROP(1)\n");
            break;
        }case 3:{
            printf("DROP(2)\n");
            break;
        }case 4:{
            printf("POUR(1,2)\n");
            break;
        }case 5:{
            printf("POUR(2,1)\n");
            break;
        }default:
            break;
    }
}
void Bfs(int a,int b,int c){
    if(a==0&&b==0&&step==0){
        //visited[A][B]=1;
        que[head]=Node(0,0,-1,-1,step);
        visited[a][b]=1;
        step++;
    }
    if(a==c||b==c){
        //cout<<"head:";
        cout<<que[head].step<<endl;
        print(head);
        return;
    }
    for(int i=0;i<6;i++){ //遍历6种方法
        tail++;
        switch(i){
        case 0:{//FILL(1)
            if(a<A&&!visited[A][b]){
                visited[A][b]=1;
                opeator=0;
                que[tail]=Node(A,b,opeator,head,que[head].step+1);
            }else
                tail--;
            break;
        }case 1:{//FILL(2)
            if(b<B&&!visited[a][B]){
                visited[a][B]=1;
                opeator=1;
                que[tail]=Node(a,B,opeator,head,que[head].step+1);
            }else
                tail--;
            break;
        }case 2:{//DROP(1)
            if(a!=0&&!visited[0][b]){
                visited[0][b]=1;
                opeator=2;
                que[tail]=Node(0,b,opeator,head,que[head].step+1);
            }else
                tail--;
            break;
        }case 3:{//DROP(2)
            if(b!=0&&!visited[a][0]){
                visited[a][0]=1;
                opeator=3;
                que[tail]=Node(a,0,opeator,head,que[head].step+1);
            }else
                tail--;
            break;
        }case 4:{//POUR(1,2)
            opeator=4;
            if(a>=(B-b)&&!visited[a-B+b][B]&&a!=0){
                visited[a-B+b][B]=1;
                que[tail]=Node(a-B+b,B,opeator,head,que[head].step+1);
            }else if(a<(B-b)&&!visited[0][b+a]&&a!=0){
                visited[0][b+a]=1;
                que[tail]=Node(0,b+a,opeator,head,que[head].step+1);
            }else
                tail--;
            break;
        }case 5:{//POUR(2,1)
            opeator=5;
            if(b>=(A-a)&&!visited[A][b-A+a]&&b!=0){
                visited[A][b-A+a]=1;
                que[tail]=Node(A,b-A+a,opeator,head,que[head].step+1);
            }else if(b<(A-a)&&!visited[b+a][0]&&b!=0){
                visited[b+a][0]=1;
                que[tail]=Node(b+a,0,opeator,head,que[head].step+1);
            }else
                tail--;
            break;
        }default:
            break;
    }
        if(que[tail].a==c||que[tail].b==c){
            cout<<que[tail].step<<endl;
            print(tail);
            return;
        }
    }
    head++;
    if(head!=tail){
        Bfs(que[head].a,que[head].b,c);
        head++;
    }
}
int main()
{
    cin>>A>>B>>C;
    memset(visited,0,sizeof(visited));
    Bfs(0,0,C);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/lrf2454224026/article/details/80557387
今日推荐