POJ 3414 Pots 均分水(bfs)

来源:互联网 发布:剑三女神捏脸数据 编辑:程序博客网 时间:2024/05/22 03:41

题目链接:

POJ 3414 Pots

题目大意:

有两个水杯,一开始都没有装水,给出两个水杯的容量,和要达到的目标容量
每个水被有三种操作:1.装满水,2.倒掉所有水,3,将水倒入另一个杯子中.

思路:

两个杯子,六种操作,,求最短路,并打印路径,6入口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 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)

代码:

/*************************************************************************        > File Name: poj_3414.cpp      > Author: dulun      > Mail: dulun@xiyoulinux.org      > Created Time: 2016年04月13日 星期三 12时59分54秒 ************************************************************************/#include<iostream>#include<stdio.h>#include<cstring>#include<cstdlib>#include<queue>#include<algorithm>#define LL long longusing namespace std;const int N = 186;struct Node{    int a, b, step;    char str[N][N];};bool v[N][N];void bfs(int a, int b, int c){    memset(v, 0, sizeof(v));    Node t, s;    queue<Node> q;    t = (Node){0, 0, 0};    q.push(t);    v[t.a][t.b] = true;    while(!q.empty())    {        t = q.front();        q.pop();        v[t.a][t.b] = true;        if(t.a == c || t.b == c)        {            printf("%d\n", t.step);            for(int i = 1; i <= t.step; i++)            {                printf("%s\n", t.str[i]);            }            return ;        }        if(t.a == 0)        {            s = t;            s.a = a;            s.step++;            strcpy(s.str[s.step], "FILL(1)");            if(!v[s.a][s.b])            {                v[s.a][s.b] = true;                q.push(s);            }        }        else if(t.a <= a)        {            s = t;            s.a = 0;            s.step++;            strcpy(s.str[s.step], "DROP(1)");            if(!v[s.a][s.b])            {                v[s.a][s.b] = true;                q.push(s);            }            if(t.b < b)            {                s = t;                if(t.a+t.b <= b)                {                    s.a = 0;                    s.b = t.a+t.b;                }                else{                    s.a = t.a - (b-t.b);                    s.b = b;                }                s.step++;                strcpy(s.str[s.step], "POUR(1,2)");                if(!v[s.a][s.b])                {                    v[s.a][s.b] = true;                    q.push(s);                }            }        }        if(t.b == 0)        {            s = t;            s.b = b;            s.step++;            strcpy(s.str[s.step], "FILL(2)");            if(!v[s.a][s.b])            {                v[s.a][s.b] = true;                q.push(s);            }        }        else if(t.b <= b)        {            s = t;            s.b = 0;            s.step++;            strcpy(s.str[s.step], "DROP(2)");            if(!v[s.a][s.b])            {                v[s.a][s.b] = true;                q.push(s);            }            if(t.a < a)            {                if(t.a+t.b < a)                {                    s = t;                    s.a = t.a+t.b;                    s.b = 0;                }                else{                    s = t;                    s.a = a;                    s.b = t.b - (a-t.a);                }                s.step++;                strcpy(s.str[s.step], "POUR(2,1)");                if(!v[s.a][s.b])                {                    v[s.a][s.b] = true;                    q.push(s);                }            }        }    }    printf("impossible\n");    return;}int main(){    int a, b, c;    scanf("%d%d%d", &a, &b, &c);    bfs(a, b, c);    return 0;}
0 0
原创粉丝点击