Pots(SDUT 2780 bfs)

Problem 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
There are multiple test cases.
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. 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

Hint
FILL(2)
POUR(2,1)
DROP(1)
POUR(2,1)
FILL(2)
POUR(2,1)

题目大意:
用两个瓶子,通过6种操作,通过最少的操作数得出需要的容量或者不能得出。

说明:
这道题输出操作的步数就可以啦,但是想起了《大话西游2经典版》中有一个游戏任务,就是让你进行操作,我就输出的操作的步骤。
但是感觉就是用的变量多的话,依然用简单的a,b,c等来进行标记容易弄混。

AC代码

#include <bits/stdc++.h>
using namespace std;
struct node
{
    int x,y,num;
}q[10001],f,t;

int vir[101][101];

int bfs(int a,int b,int c)
{
    t.x=0;
    t.y=0;
    t.num=0;
    int head=0,tail=0;
    q[tail++]=t;
    vir[t.x][t.y]=1;
    while(head<tail){
        t=q[head++];
        if(t.x==c||t.y==c){
            printf("%d\n",t.num);
            return 0;
        }
        for(int i=0;i<6;i++){
            if(i==0){
                f.x=t.x;
                f.y=b;
            }
            if(i==1){
                f.x=a;
                f.y=t.y;
            }
            if(i==2){
                f.x=0;
                f.y=t.y;
            }
            if(i==3){
                f.x=t.x;
                f.y=0;
            }
            if(i==4){
                f.x=t.x+t.y;
                if(f.x>a){
                    f.y=f.x-a;
                    f.x=a;
                }
                else{
                    f.y=0;
                }
            }
            if(i==5){
                f.y=t.x+t.y;
                if(f.y>b){
                    f.x=f.y-b;
                    f.y=b;
                }
                else{
                    f.x=0;
                }
            }
            if(vir[f.x][f.y]==0){
                vir[f.x][f.y]=1;
                f.num=t.num+1;
                q[tail++]=f;
            }
        }
    }
    printf("impossible\n");
    return 0;
}

int main()
{
    int a,b,c;
    while(scanf("%d %d %d",&a,&b,&c)!=EOF){
        memset(vir,0,sizeof(vir));
        bfs(a,b,c);
    }
    return 0;
}

游戏操作代码:

#include <bits/stdc++.h>
using namespace std;

struct node
{
    int x,y,num;
    int father,top;
} q[10001],f,t;
/*
     在结构题中,x,y,num分别表示某状态下A瓶的存量,
     B瓶的存量,和到达该存量经过的操作步数。
     而fahter表示,他的父亲节点在数组q中的编号,
     top为它是经过哪一种操作得到的(在父亲节点哪里)。
  */
int vir[101][101];
int mp[101];

int bfs(int a,int b,int c)
{
    t.x=0;
    t.y=0;
    t.num=0;
    t.father=-1;
    t.top=-1;
    int head=0,tail=0;
    q[tail++]=t;
    vir[t.x][t.y]=1;
    while(head<tail)
    {
        t=q[head++];
        if(t.x==c||t.y==c)
        {
            printf("%d\n",t.num);
            int top=0;
            while(t.father!=-1)
            {
                mp[++top]=t.top;
                t=q[t.father];
            }
            for(int i=top; i>=1; i--)
            {
                if(mp[i]==0)
                    printf("将B瓶装满\n");
                if(mp[i]==1)
                    printf("将A瓶装满\n");
                if(mp[i]==2)
                    printf("将A瓶倒出\n");
                if(mp[i]==3)
                    printf("将B瓶倒出\n");
                if(mp[i]==4)
                    printf("将B到给A\n");
                if(mp[i]==5)
                    printf("将A到给B\n");
            }
            return 0;
        }
        for(int i=0; i<6; i++)
        {
            if(i==0)
            {
                /*给B瓶装满*/
                f.x=t.x;
                f.y=b;
            }
            if(i==1)
            {
                /*给A瓶装满*/
                f.x=a;
                f.y=t.y;
            }
            if(i==2)
            {
                /*将A瓶倒出*/
                f.x=0;
                f.y=t.y;
            }
            if(i==3)
            {
                /*将B瓶倒出*/
                f.x=t.x;
                f.y=0;
            }
            if(i==4)
            {
                /*将B瓶倒给A瓶*/
                f.x=t.x+t.y;
                if(f.x>a)
                {
                    f.y=f.x-a;
                    f.x=a;
                }
                else
                {
                    f.y=0;
                }
            }
            if(i==5)
            {
                /*将A瓶倒给B瓶*/
                f.y=t.x+t.y;
                if(f.y>b)
                {
                    f.x=f.y-b;
                    f.y=b;
                }
                else
                {
                    f.x=0;
                }
            }
            /*
                 检查该状态是否存在过,
                 若没有存在过,入栈
            */
            if(vir[f.x][f.y]==0)
            {
                vir[f.x][f.y]=1;
                f.num=t.num+1;
                f.father=head-1;
                f.top=i;
                q[tail++]=f;
            }
        }
    }
    printf("impossible\n");
    return 0;
}

int main()
{
    int a,b,c;
    while(scanf("%d %d %d",&a,&b,&c)!=EOF)
    {
        memset(vir,0,sizeof(vir));
        bfs(a,b,c);
    }
    return 0;
}
#include <bits/stdc++.h>
using namespace std;

struct node
{
    int x,y,num;
    int father,top;
} q[10001],f,t;
/*
     在结构题中,x,y,num分别表示某状态下A瓶的存量,
     B瓶的存量,和到达该存量经过的操作步数。
     而fahter表示,他的父亲节点在数组q中的编号,
     top为它是经过哪一种操作得到的(在父亲节点哪里)。
  */
int vir[101][101];
int mp[101];

int bfs(int a,int b,int c)
{
    t.x=0;
    t.y=0;
    t.num=0;
    t.father=-1;
    t.top=-1;
    int head=0,tail=0;
    q[tail++]=t;
    vir[t.x][t.y]=1;
    while(head<tail)
    {
        t=q[head++];
        if(t.x==c||t.y==c)
        {
            printf("%d\n",t.num);
            int top=0;
            while(t.father!=-1)
            {
                mp[++top]=t.top;
                t=q[t.father];
            }
            for(int i=top; i>=1; i--)
            {
                if(mp[i]==0)
                    printf("步骤4\n");
                if(mp[i]==1)
                    printf("步骤1\n");
                if(mp[i]==2)
                    printf("步骤2\n");
                if(mp[i]==3)
                    printf("步骤5\n");
                if(mp[i]==4)
                    printf("步骤6\n");
                if(mp[i]==5)
                    printf("步骤3\n");
            }
            printf("我倒完了!\n");
            return 0;
        }
        for(int i=0; i<6; i++)
        {
            if(i==0)
            {
                /*给B瓶装满*/
                f.x=t.x;
                f.y=b;
            }
            if(i==1)
            {
                /*给A瓶装满*/
                f.x=a;
                f.y=t.y;
            }
            if(i==2)
            {
                /*将A瓶倒出*/
                f.x=0;
                f.y=t.y;
            }
            if(i==3)
            {
                /*将B瓶倒出*/
                f.x=t.x;
                f.y=0;
            }
            if(i==4)
            {
                /*将B瓶倒给A瓶*/
                f.x=t.x+t.y;
                if(f.x>a)
                {
                    f.y=f.x-a;
                    f.x=a;
                }
                else
                {
                    f.y=0;
                }
            }
            if(i==5)
            {
                /*将A瓶倒给B瓶*/
                f.y=t.x+t.y;
                if(f.y>b)
                {
                    f.x=f.y-b;
                    f.y=b;
                }
                else
                {
                    f.x=0;
                }
            }
            /*
                 检查该状态是否存在过,
                 若没有存在过,入栈
            */
            if(vir[f.x][f.y]==0)
            {
                vir[f.x][f.y]=1;
                f.num=t.num+1;
                f.father=head-1;
                f.top=i;
                q[tail++]=f;
            }
        }
    }
    printf("impossible\n");
    return 0;
}

int main()
{
    int a,b,c;
    while(scanf("%d %d %d",&a,&b,&c)!=EOF)
    {
        memset(vir,0,sizeof(vir));
        bfs(a,b,c);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值