【BFS】Pots

整理自http://www.cnblogs.com/yzm10/p/7242590.html


总时间限制:
1000ms
内存限制:
65536kB
描述

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 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)
分析
让你求最少操作步数,每一步都有多种选择,所以用BFS,记录操作步骤。对于我来说实现起来比较难
1、要想到用01二维数组表示两个杯子的容量
2、想到有六种操作,1倒满,2倒满,倒空1……
3、操作状态Node的成员。
参考代码
#include<stdio.h>
#include<queue>
using namespace std;
int b[105][105],re[10005];///标记数组和结果数组

struct Node{
    int x,y,s,c,f,d;
}node[10005];

int main()
{
    int x,y,z,c,f,i;///f:操作成功与否的标志;c,i是数组遍历下标
    queue<Node> q;
    scanf("%d%d%d",&x,&y,&z);///A,B,C
    if(z==0) printf("0\n");   ///一定注意
    else{
    b[0][0]=1;
    node[1].x=0;///A
    node[1].y=0;///B
    node[1].s=0;///step步骤,统计需要几步
    node[1].c=1;
    node[1].f=1;///记下上一步的C,以便找出当前操作的上一步操作
    node[1].d=0;///6类操作的哪一类
    q.push(node[1]);
    c=1;f=0;
    while(q.size()){
        for(i=1;i<=6;i++){
            if(i==1&&q.front().x<x&&b[x][q.front().y]==0){
                b[x][q.front().y]=1;///倒满1
                node[++c].x=x;
                node[c].y=q.front().y;
                node[c].s=q.front().s+1;
                node[c].c=c;
                node[c].f=q.front().c;
                node[c].d=1;
                if(node[c].x==z||node[c].y==z){
                    f=node[c].s;
                    break;
                }
                q.push(node[c]);
            }
            else if(i==2&&q.front().y<y&&b[q.front().x][y]==0){
                b[q.front().x][y]=1;///倒满2
                node[++c].x=q.front().x;
                node[c].y=y;
                node[c].s=q.front().s+1;
                node[c].c=c;
                node[c].f=q.front().c;
                node[c].d=2;
                if(node[c].x==z||node[c].y==z){
                    f=node[c].s;
                    break;
                }
                q.push(node[c]);
            }
            else if(i==3&&q.front().x>0&&b[0][q.front().y]==0){
                b[0][q.front().y]=1;///倒空1
                node[++c].x=0;
                node[c].y=q.front().y;
                node[c].s=q.front().s+1;
                node[c].c=c;
                node[c].f=q.front().c;
                node[c].d=3;
                if(node[c].x==z||node[c].y==z){
                    f=node[c].s;
                    break;
                }
                q.push(node[c]);
            }
            else if(i==4&&q.front().y>0&&b[q.front().x][0]==0){
                b[q.front().x][0]=1;///倒空2
                node[++c].x=q.front().x;
                node[c].y=0;
                node[c].s=q.front().s+1;
                node[c].c=c;
                node[c].f=q.front().c;
                node[c].d=4;
                if(node[c].x==z||node[c].y==z){
                    f=node[c].s;
                    break;
                }
                q.push(node[c]);
            }
            else if(i==5&&q.front().x>0&&q.front().y<y){
                ///2满或不满的情况
                int tx=q.front().x<y-q.front().y?0:q.front().x+q.front().y-y;
                int ty=q.front().x<y-q.front().y?q.front().x+q.front().y:y;

                if(b[tx][ty]==0){
                    b[tx][ty]=1;
                    node[++c].x=tx;
                    node[c].y=ty;
                    node[c].s=q.front().s+1;
                    node[c].c=c;
                    node[c].f=q.front().c;
                    node[c].d=5;
                    if(node[c].x==z||node[c].y==z){
                        f=node[c].s;
                        break;
                    }
                    q.push(node[c]);
                }
            }
            else if(i==6&&q.front().x<x&&q.front().y>0){
                int tx=x-q.front().x<q.front().y?x:q.front().x+q.front().y;
                int ty=x-q.front().x<q.front().y?q.front().x+q.front().y-x:0;
                if(b[tx][ty]==0){
                    b[tx][ty]=1;
                    node[++c].x=tx;
                    node[c].y=ty;
                    node[c].s=q.front().s+1;
                    node[c].c=c;
                    node[c].f=q.front().c;
                    node[c].d=6;
                    if(node[c].x==z||node[c].y==z){
                        f=node[c].s;
                        break;
                    }
                    q.push(node[c]);
                }
            }
        }
        if(f!=0) break;
        q.pop();
    }
    if(f==0) printf("impossible\n");
    else{
        printf("%d\n",f);
        //根据node中f的标记找到最少的操作
        for(i=1;i<=f;i++){
            re[i]=node[c].d;
            c=node[c].f;
        }

        for(i=f;i>=1;i--){
            if(re[i]==1) printf("FILL(1)\n");
            else if(re[i]==2) printf("FILL(2)\n");
            else if(re[i]==3) printf("DROP(1)\n");
            else if(re[i]==4) printf("DROP(2)\n");
            else if(re[i]==5) printf("POUR(1,2)\n");
            else if(re[i]==6) printf("POUR(2,1)\n");
        }
    }
    }
    return 0;
}
时间: 2024-08-30 03:54:14

【BFS】Pots的相关文章

【bfs】【中等难度】tyvj P1234 - bench与奔驰

P1234 - bench与奔驰 From zhangbh001    Normal (OI) 总时限:10s    内存限制:128MB    代码长度 限制:64KB P1234 - bench与奔驰 背景 Background 公园里有个人在练开奔驰 - -!,但是总是撞在bench上 (众人曰:狼来了,快跑啊!) 描述 Description 公园里的bench与奔驰都是无敌的,不会被撞坏.由于开奔驰的人比较"有特点",总是向上下左右四个方向开,而且只会在撞到椅子之后改变方向(

hdoj 1312 Red and Black 【BFS】

题意:一共有四个方向,从'@'出发,找能到达'.'的个数, #是不能通过的. 策略:广搜. 这道题属于最简单的bfs了. 代码: #include<stdio.h> #include<string.h> #include<queue> using std::queue; bool vis[25][25]; char s[25][25]; int n, m; int ans = 0; struct node{ int x, y; }; node st; const int

HDU1242 Rescue 【BFS】

Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 16314    Accepted Submission(s): 5926 Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is

HDU 1253 胜利大逃亡 NYOJ 523【BFS】

胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 24608    Accepted Submission(s): 9427 Problem Description Ignatius被魔王抓走了,有一天魔王出差去了,这可是Ignatius逃亡的好机会. 魔王住在一个城堡里,城堡是一个A*B*C的立方体,可以被表示成A个B*C的

NYOJ 284 坦克大战 【BFS】+【优先队列】

坦克大战 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 Many of us had played the game "Battle city" in our childhood, and some people (like me) even often play it on computer now. What we are discussing is a simple edition of this game. Given a map that co

【BFS】uva10047The Monocycle

/* 本题的特殊之处,到达一个格子时,因为朝向不同,以及接触地面的颜色不同, 会处于不同的状态::::::::: 把(x, y, d, c)作为一个结点,表示所在位置(x, y),方向为d,颜色为c;;;;; ------------------------------------------------------------------------ 在方向上我们把前,左,右编号为0,1,2:::: 颜色,从蓝色开始编号为0,1,2,3:::::::::: ------------------

【BFS】uva11624Fire!

/* bfs宽度遍历 -------------------------------------------------------------------------- 对人和火同时进行bfs,,注意应该先火后人,即如果在人到达该格子前,格子已经着火 则不应该走,最后人走到边界无路可走,则IMPOSSIBLE!!!!!!!!!!!! --------------------------------------------------------------------------- 两次bfs

【bfs】hdu 1104 Remainder

[bfs]hdu 1104 Remainder 题目链接:hdu 1104 Remainder 很不错的一道搜索题目,但是有几个关键问题要注意. 最短路径,果断bfs+Queue 路径的存储问题,之前只想把每一步的计算结果存储到queue(int)Q中,后来发现路径无法记录,就选择存储节点的方式并用string保存路径,queue(node)Q,开一个临时的节点node p,每进行一次运算就更新它的路径string+'op',最终输出的一定是完整路径!! 但最关键的是取模!!!!! discus

Poj 3087 Shuffle&#39;m Up 【BFS】

Shuffle'm Up Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6556 Accepted: 3077 Description A common pastime for poker players at a poker table is to shuffle stacks of chips. Shuffling chips is performed by starting with two stacks of pok