poj3414--Pots(bfs,记录路径)

Pots

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10149   Accepted: 4275   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 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

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

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的液体,六种操作

装满1。装满2,倒掉1。倒掉2,由1倒到2,有2倒到1.

记录一下路径。也就是有谁到达的当前状态

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct node{
    int a , b , pre ;
    char s[20] ;
}p[1000000] , x ;
int flag[20000] , low , top , k ;
char str[6][20] = { "FILL(1)","FILL(2)","DROP(1)","DROP(2)","POUR(1,2)","POUR(2,1)" };
int pre[1000000] ;
int main()
{
    int a , b , c , sum , i , j ;
    k = 0 ;
    memset(flag,0,sizeof(flag));
    scanf("%d %d %d", &a, &b, &c);
    flag[a*100+b] = 1 ;
    low = top = 0 ;
    p[top].a = 0 ; p[top].b = 0 ;
    p[top++].pre = -1 ;
    while(low < top)
    {
        if( p[low].a == c || p[low].b == c )
            break;
        if( p[low].a < a )
        {
            p[top].a = a ; p[top].b = p[low].b ;
            if( !flag[ p[top].a*100+p[top].b ] )
            {
                flag[ p[top].a*100+p[top].b ] = 1 ;
                strcpy(p[top].s,str[0]);
                p[top++].pre = low ;
            }
        }
        if( p[low].b < b )
        {
            p[top].a = p[low].a ; p[top].b = b ;
            if( !flag[ p[top].a*100+p[top].b ] )
            {
                flag[ p[top].a*100+p[top].b ] = 1 ;
                strcpy(p[top].s,str[1]);
                p[top++].pre = low ;
            }
        }
        if( p[low].a > 0 )
        {
            p[top].a = 0 ; p[top].b = p[low].b ;
            if( !flag[ p[top].a*100+p[top].b ] )
            {
                flag[ p[top].a*100+p[top].b ] = 1 ;
                strcpy(p[top].s,str[2]);
                p[top++].pre = low ;
            }
        }
        if( p[low].b > 0 )
        {
            p[top].a = p[low].a ; p[top].b = 0 ;
            if( !flag[ p[top].a*100+p[top].b ] )
            {
                flag[ p[top].a*100+p[top].b ] = 1 ;
                strcpy(p[top].s,str[3]);
                p[top++].pre = low ;
            }
        }
        if( p[low].a > 0 && p[low].b < b )
        {
            sum = p[low].a + p[low].b ;
            if( sum <= b )
            {
                p[top].a = 0 ; p[top].b = sum ;
            }
            else
            {
                p[top].a = sum - b ; p[top].b = b ;
            }
            if( !flag[ p[top].a*100+p[top].b ] )
            {
                flag[ p[top].a*100+p[top].b ] = 1 ;
                strcpy(p[top].s,str[4]);
                p[top++].pre = low ;
            }
        }
        if( p[low].a < a && p[low].b > 0 )
        {
            sum = p[low].a + p[low].b ;
            if( sum <= a )
            {
                p[top].a = sum ; p[top].b = 0 ;
            }
            else
            {
                p[top].a = a ; p[top].b = sum - a ;
            }
            if( !flag[ p[top].a*100+p[top].b ] )
            {
                flag[ p[top].a*100+p[top].b ] = 1 ;
                strcpy(p[top].s,str[5]);
                p[top++].pre = low ;
            }
        }
        low++ ;
    }
    if( low == top )
        printf("impossible\n");
    else
    {
        j = 0 ;
        for(i = low ; i != 0 ; i = p[i].pre)
        {
            pre[j++] = i ;
        }
        printf("%d\n", j);
        for(j -= 1 ; j >= 0 ; j--)
            printf("%s\n", p[ pre[j] ].s);
    }
    return 0;
}

版权声明:转载请注明出处:http://blog.csdn.net/winddreams

时间: 2024-11-13 03:11:19

poj3414--Pots(bfs,记录路径)的相关文章

POJ 3414 Pots (BFS + 记录路径)

Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10300 Accepted: 4362 Special Judge Description You are given two pots, having the volume of A and B liters respectively. The following operations can be performed: FILL(i)        fill th

HDU1026--Ignatius and the Princess I(BFS记录路径)

Problem Description The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius has to rescue our pretty Princess. Now he gets into feng5166's castle. The castle is a large labyrinth. To make the problem simply, we assume the labyrint

SDUT2465其实玩游戏也得学程序(BFS记录路径问题)

BFS记录路径第一炮 题目连接:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2465 思路:搜索找路径问题,DFS进行,调用     DFS(当前->当前点父节点)->起点,思想,以栈为储存结构,保存路径. 和以往BFS不一样的是地图储存在结构体里...注意 在DFS时候,BLF0是当前点的父节点,BLF1是其子节点,因为初始时cost是无限大,每次进出队列的过程都保证当前的cost是较小

poj 3984 迷宫问题 (BFS+记录路径)

题目连接:http://poj.org/problem?id=3984 题解:简单的BFS+记录路径,具体题解看代码注释. #include <iostream> #include <queue> #include <cstdio> using namespace std; struct point { int x; int y; }; queue<point>q; int map[5][5]; int vis[5][5];//标记走过的路 int g[4]

(简单) POJ 3414 Pots,BFS+记录路径。

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

hdu 1226 BFS + bfs记录路径

http://acm.hdu.edu.cn/showproblem.php?pid=1226 为了省空间,可以用vis数组初始化的时候初始化为-1, 发现一个BFS容易错的地方 开始一直WA在这里:就是我int tp=q.front();之后马上q.pop():了,然后才去判断是不是符合条件以break,这样就不能根据q.empty()==1认为没有找到ans 因为这里WA了 其实也可以vis[0] == -1来判断 比较不理解的是 当n==0的时候 %n==0的时候怎么处理 //#pragma

学霸的迷宫(BFS+记录路径)

1 //求从(sx.sy)到(gx.gy)的最短距离; 2 3 #include<iostream> 4 #include<cstdio> 5 #include<cstdio> 6 #include<queue> 7 #include<cstring> 8 #define INF 99999999 9 10 using namespace std; 11 12 typedef pair<int, int > P; //数对,记录位置

poj1426 - Find The Multiple [bfs 记录路径]

传送门 转:http://blog.csdn.net/wangjian8006/article/details/7460523 (比较好的记录路径方案) 1 #include<iostream> 2 #include<cstring> 3 #include<cstdlib> 4 #include<cstdio> 5 #include<algorithm> 6 #include<cmath> 7 #include<queue>

hdu 1026 Ignatius and the Princess I(优先队列+bfs+记录路径)

以前写的题了,现在想整理一下,就挂出来了. 题意比较明确,给一张n*m的地图,从左上角(0, 0)走到右下角(n-1, m-1). 'X'为墙,'.'为路,数字为怪物.墙不能走,路花1s经过,怪物需要花费1s+数字大小的时间. 比较麻烦的是需要记录路径.还要记录是在走路还是在打怪. 因为求最短路,所以可以使用bfs. 因为进过每一个点花费时间不同,所以可以使用优先队列. 因为需要记录路径,所以需要开一个数组,来记录经过节点的父节点.当然,记录方法不止一种. 上代码—— 1 #include <c

poj_3984_迷宫问题_(bfs+记录路径)

前两天状态一直很不好,不知道为什么,感觉什么都没有掌握,很想回家.还好,今天调整过来啦,acm,try my best. 前两天一道题都没有ac,不能再这样了,每天都必须有ac. 嗯,说说这道题. 这道题明明就很水,结果我做了一个下午,囧. 迷宫问题 Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Description 定义一个二维数组: int maze[5]