PKU3414 Pots

题目链接:http://poj.org/problem?id=3414

这题以前碰到,没做,感觉好麻烦的样子。

今天突然想起这题,反应过来,这不就是一个BFS吗。

用一个二维数组vis[a][b]来记录a,b是否出现过,然后6个操作搜索一遍,能入队的入队。

我一开始没发现内存限制这么大,所以写的时候比较考虑内存。。

用了一个char数组来存6个操作的名字

然后在一个路径数组中存了所有操作,每一个节点都记录了这个操作在数组里的下标

然后再用一个结构体来读取路径,最后用了一个栈也是因为写起来比较方便。。。

a,b的范围都是<=100,所以vis数组要开101*101。

写的时候没有多想,一路想一路写下来,有些地方写得不太好,代码显得很长,4000多B,大概写了40分钟,还是需要进步啊!

不过能1次AC也是比较惊讶的,毕竟只测了样例就交了。。。

题目:Pots
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 11017 Accepted: 4674 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)
代码如下:

#include<functional>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<vector>
#include<cctype>
#include<string>
#include<stack>
#include<queue>
#include<cmath>
#include<set>
#include<map>
using namespace std;
int sa,sb,target;
bool vis[101][101];
char output[6][10]={"FILL(1)","FILL(2)","DROP(1)","DROP(2)","POUR(2,1)","POUR(1,2)"};
//                     0         1         2         3         4           5
typedef struct node{
    int index;
    int a,b;
    int step;
};
typedef struct pp{
    int now,pre;
};
int px;
pp path[10001];
int bfs(node &sourse)
{
    queue<node> Q;
    node start,use;
    start.step=0;
    start.index=0;
    start.a=0;
    start.b=0;
    vis[0][0]=1;
    path[px].pre=-1;
    path[px++].now=-1;
    Q.push(start);
    while(!Q.empty())
    {
        start=Q.front();
        Q.pop();
        if(start.a==target||start.b==target)
        {
            sourse=start;
            return start.step;
        }
        if(vis[sa][start.b]==false)
        {//fill 1
            vis[sa][start.b]=true;
            use.a=sa;
            use.b=start.b;
            use.step=start.step+1;
            use.index=px;
            path[px].pre=start.index;
            path[px++].now=0;
            Q.push(use);
        }
        if(vis[start.a][sb]==false)
        {//fill 2
            vis[start.a][sb]=true;
            use.a=start.a;
            use.b=sb;
            use.step=start.step+1;
            use.index=px;
            path[px].pre=start.index;
            path[px++].now=1;
            Q.push(use);
        }
        if(vis[0][start.b]==false)
        {//drop 1
            vis[0][start.b]=true;
            use.a=0;
            use.b=start.b;
            use.step=start.step+1;
            use.index=px;
            path[px].pre=start.index;
            path[px++].now=2;
            Q.push(use);
        }
        if(vis[start.a][0]==false)
        {//drop 2
            vis[start.a][0]=true;
            use.a=start.a;
            use.b=0;
            use.step=start.step+1;
            use.index=px;
            path[px].pre=start.index;
            path[px++].now=3;
            Q.push(use);
        }
        if(start.a<sa&&start.b>0)
        {//pour 2 1
            int tempa,tempb;
            tempa=(start.a+start.b>=sa)?sa:start.a+start.b;
            tempb=(sa-start.a>=start.b)?0:start.b-sa+start.a;
            if(vis[tempa][tempb]==false)
            {
                vis[tempa][tempb]=true;
                use.a=tempa;
                use.b=tempb;
                use.step=start.step+1;
                path[px].pre=start.index;
                use.index=px;
                path[px++].now=4;
                Q.push(use);
            }
        }
        if(start.a>0&&start.b<sb)
        {
            int tempa,tempb;
            tempa=(sb-start.b>=start.a)?0:start.a-sb+start.b;
            tempb=(start.a+start.b>=sb)?sb:start.a+start.b;
            if(vis[tempa][tempb]==false)
            {
                vis[tempa][tempb]=true;
                use.a=tempa;
                use.b=tempb;
                use.step=start.step+1;
                path[px].pre=start.index;
                use.index=px;
                path[px++].now=5;
                Q.push(use);
            }
        }
    }
    return -1;
}
int main()
{
    while(scanf("%d%d%d",&sa,&sb,&target)!=EOF)
    {
        getchar();
        memset(vis,false,sizeof(vis));
        memset(path,0,sizeof(path));
        px=0;
        node sourse;
        sourse.a=-1;
        sourse.b=-1;
        int ans=bfs(sourse);
        if(ans<0){
            printf("impossible\n");
            continue;
        }
        printf("%d",ans);
        stack<int> st;
        while(sourse.index>=0)
        {
            st.push(path[sourse.index].now);
            sourse.index=path[sourse.index].pre;
        }
        while(!st.empty())
        {
            printf("%s\n",output[st.top()]);
            st.pop();
        }
    }
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值