POJ 3414 Pots(BFS)||SDUT(Pots)

                                                                                                                               Pots
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 12022 Accepted: 5078 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 potj is full (and there may be some water left in the poti), or the poti is empty (and all its contents have been moved to the potj).

Write a program to find the shortest possible sequence of these operations that will yield exactlyC liters of water in one of the pots.

Input

On the first and only line are the numbers A, B, andC. These are all integers in the range from 1 to 100 andC≤max(A,B).

Output

The first line of the output must contain the length of the sequence of operationsK. The followingK 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)


题目大意:有两个杯子的容量分别是A,B,问经过多少步,可以让C升的水装到一个杯子中。

思路:问最短的步数当然是BFS,但是怎么将其路径标记下来是难点,并且怎么个搜索法。

可以设一个串在node 来专门装当前的情况,然后分别映射到str中,对于怎么搜索肯定分为fill,drop,pour三大类



#include<iostream>
#include<cstdio>
#include<string.h>
#include<string>
#include<cmath>
#include<queue>
#define LL long long
using namespace std;
int A,B,C;
struct node
{
    int n1,n2,tep;
    string a;//保存当前的操作
};
bool vis[100][100];
char str[6][10]= {"FILL(1)","FILL(2)","POUR(2,1)","POUR(1,2)","DROP(1)","DROP(2)"};//具体有六种实现
void bfs()
{
    queue<node>q;
    while(!q.empty()) q.pop();
    node f1,f2;
    int t;
    f1.n1=0;
    f1.n2=0;
    f1.tep=0;
    f1.a="";开始时为空串
    q.push(f1);
    while(!q.empty())
    {
        f1=q.front();
        q.pop();
        if(vis[f1.n1][f1.n2])//剪纸
            continue;
       else
            vis[f1.n1][f1.n2]=true;
        if(f1.n1==C||f1.n2==C)
        {
            printf("%d\n",f1.tep);
            for(int j=0;j<f1.tep;j++)
            {
                printf("%s\n",str[ f1.a[j]-'0' ]);//分别对应到str中
            }
            return ;
        }
        for(int i=0;i<4;i++)
        {
            f2=f1;
            f2.tep++;
            if(i==0)
            {
                if(f2.n1==0)
                {
                    f2.n1=A;
                    f2.n2=f1.n2;//注意这里必须是f2.x=f1.x因为不是的话可能会对下一个if()造成影响,尽管开头的部分有f2=f1;在下边并列的部分就无所谓了
                    f2.a=f1.a+'0';
                    q.push(f2);
                }
                if(f2.n2==0)
                {
                    f2.n2=B;
                    f2.n1=f1.n1;//保持相对不变
                    f2.a=f1.a+'1';
                    q.push(f2);
                }
            }
            if(i==1)
            {
                if(f2.n1!=0)
                {
                    f2.n1=0;
                    f2.n2=f1.n2;
                    f2.a=f1.a+'4';
                    q.push(f2);
                }
                if(f2.n2!=0)
                {
                    f2.n2=0;
                    f2.n1=f1.n1;
                    f2.a=f1.a+'5';
                    q.push(f2);
                }
            }
            if(i==3)
            {
                if(f2.n2!=0&&f2.n1!=A)
                {
                    t=A-f2.n1;
                    if(f2.n2>=t)
                    {
                        f2.n2-=t;
                        f2.n1=A;
                    }
                    else
                    {
                        f2.n1+=f2.n2;
                        f2.n2=0;
                    }
                    f2.a+='2';
                    q.push(f2);
                }
            }
            if(i==2)
            {
                if(f2.n1!=0&&f2.n2!=B)
                {
                    t=B-f2.n2;
                    if(f2.n1>=t)
                    {
                        f2.n1-=t;
                        f2.n2=B;
                    }
                    else
                    {
                        f2.n2+=f2.n1;
                        f2.n1=0;
                    }
                    f2.a+='3';
                    q.push(f2);
                }
            }
        }
    }
    printf("impossible\n");
}
int main()
{
    int n,m,i,j,k,s;
    while(~scanf("%d%d%d",&A,&B,&C))
    {
        memset(vis,false,sizeof(vis));
        bfs();
    }
    return 0;
}



SDUT Pots

   简化的Pots,过一段时间再做这题,居然怎么搜都没思路了- -,(自己的提升空间还有很大啊!)

   如果是找得话,肯定是讲所有的可能情况都枚举一遍,即细分的话可以有6种情况。既可以分别枚举这些情况还需要注意,运算的先后顺序问题。

 剪枝也是必要的手段,这道题剪枝的话,顶多也就是从某个状态有没有被搜到过 的问题,所以以每次每个个瓶子的状态标记即可。搜过的直接跳过。


#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<queue>
#include<map>
#define L1 long long
#define L2 __int64
#define inf 0x3f3f3f3f
using namespace std;

const int m1=1001000;
const int m2=1010;
int head[m1],vex[m1],arr[m1];
bool vis[m2][m2];
char st2[500000],st1[500000];

struct node
{
    int x,y,tmp;
};

void bfs(int a,int b,int c)
{
    queue<node>q;
    while(!q.empty()) q.pop();
    node f1,f2;
    f1.x=0;
    f1.y=0;
    f1.tmp=0;
    q.push(f1);

    while(!q.empty())
    {
        f1=q.front();
        q.pop();
        if(f1.x==c||f1.y==c)
        {
            printf("%d\n",f1.tmp);
            return ;
        }

        if(vis[f1.x][f1.y] )
            continue;
        else
            vis[f1.x][f1.y]=true;

        for(int i=0;i<6;i++)
        {
            f2=f1;
            f2.tmp++;
            if(i==0)
            {
                if(f2.x==0)
                {
                    f2.x=a;
                    q.push(f2);
                }
            }
            if(i==1)
            {
                if(f2.y==0)
                {
                    f2.y=b;
                    q.push(f2);
                }
            }
            if(i==2)
            {
                if(f2.x!=0)
                {
                    f2.x=0;
                    q.push(f2);
                }
            }
            if(i==3)
            {
                if(f2.y!=0)
                {
                    f2.y=0;
                    q.push(f2);
                }
            }
            if(i==4)
            {
                if(f2.y!=0&&f2.x!=a)
                {
                    int t=a-f2.x;
                    if(f2.y>=t)
                    {
                        f2.x=a;
                        f2.y-=t;
                        q.push(f2);
                    }
                    else
                    {
                        f2.x+=f2.y;
                        f2.y=0;
                        q.push(f2);
                    }
                }
            }
            if(i==5)
            {
                if(f1.y!=b&&f1.x!=0)
                {
                    int t=b-f2.y;
                    if(f2.x>=t )
                    {
                        f2.x-=t;
                        f2.y=b;
                        q.push(f2);
                    }
                    else
                    {
                        f2.y+=f2.x;
                        f2.x=0;
                        q.push(f2);
                    }
                }
            }
        }
    }

}
int gcd(int a,int b)
{
    return b==0?a:gcd(b,a%b);
}
int main()
{
    int n,m,i,j,k;
    int cla;
    while(~scanf("%d%d%d",&n,&m,&k))
    {
        j=gcd(n,m);
        if(k%j)
        {
            printf("impossible\n");continue;
        }
        memset(vis,false,sizeof(vis));
        bfs(n,m,k);
    }
    return 0;
}



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值