POJ-3414-Pots(BFS 模拟)

来源:互联网 发布:cnc加工中心手动编程 编辑:程序博客网 时间:2024/05/16 00:33

H - Pots
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Submit

Status

Practice

POJ 3414
Appoint description:
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)

代码

#include<stdio.h>#include<algorithm>#include<iostream>#include<string.h>#include<queue>#include<math.h>using namespace std;struct node{    int A;    int B;    int steap;    char map[10000][10];//记录操作过程};int is_vis[105][105];//已出现过的组合标记为1int A;int B;//已有杯子的容量int C;//目标水量void BFS(){    memset(is_vis,0,sizeof(is_vis));    is_vis[0][0]=1;//初始化标记为1    queue<node>q;    node star;    star.A=0;    star.B=0;    star.steap=0;    q.push(star);    while(!q.empty())    {        star=q.front();        q.pop();        if(star.A==C||star.B==C)        {            //printf("%d %d\n",star.A,star.B);            printf("%d\n",star.steap);            for(int i=1; i<=star.steap; i++)                printf("%s\n",star.map[i]);            return;        }        node end;        if(star.A==0)//如果A是空的,那只能把它填满了        {            end=star;            end.A=A;            end.steap++;            strcpy(end.map[end.steap],"FILL(1)");//注意从1开始输出字符串            if(is_vis[end.A][end.B]==0)//如果这种状态没出现过,那就可以入队            {                is_vis[end.A][end.B]=1;                q.push(end);//创建一个子节点            }        }        if(star.A>0)//如果A有剩余,或者压根就是满的        {            end=star;            end.A=0;//清空A作为一个结点            end.steap++;            strcpy(end.map[end.steap],"DROP(1)");            if(is_vis[end.A][end.B]==0)//如果这种状态没出现过,那就可以入队            {                // printf("***\n");                is_vis[end.A][end.B]=1;                q.push(end);//创建一个子节点            }            if(star.B<B)//B不满,那就可以把A倒给B            {                end=star;//下面A和B的计算需要注意                if(star.A<=B-star.B)//如果A的水在B中倒不满                {                    end.A=0;                    end.B=star.A+star.B;                }                else//A的水无法全部倒入B                {                    end.A=star.A-(B-star.B);                    end.B=B;                }                end.steap++;                strcpy(end.map[end.steap],"POUR(1,2)");                if(is_vis[end.A][end.B]==0)//如果这种状态没出现过,那就可以入队                {                    is_vis[end.A][end.B]=1;                    q.push(end);//创建一个子节点                }            }        }        if(star.B==0)//如果B是空的,那也只能把它填满并创建一个子节点        {            end=star;            end.B=B;//充满B            end.steap++;            strcpy(end.map[end.steap],"FILL(2)");            if(is_vis[end.A][end.B]==0)//如果这种状态没出现过,那就可以入队            {                is_vis[end.A][end.B]=1;                q.push(end);//创建一个子节点            }        }        if(star.B>0)        {            end=star;            end.B=0;//清空B作为一个结点            end.steap++;            strcpy(end.map[end.steap],"DROP(2)");            if(is_vis[end.A][end.B]==0)//如果这种状态没出现过,那就可以入队            {                is_vis[end.A][end.B]=1;                q.push(end);//创建一个子节点            }            if(star.A<A)//A不满,那就可以把B倒给A            {                end=star;//下面A和B的计算需要注意                if(star.B<=A-star.A)//如果B的水在A中倒不满                {                    end.A=star.B+star.A;                    end.B=0;                }                else//B的水无法全部倒入A                {                    end.A=A;                    end.B=star.B-(A-star.A);                }                end.steap=star.steap+1;                strcpy(end.map[end.steap],"POUR(2,1)");                if(is_vis[end.A][end.B]==0)//如果这种状态没出现过,那就可以入队                {                    is_vis[end.A][end.B]=1;                    q.push(end);//创建一个子节点                }            }        }    }    printf("impossible\n");}int main(){    while(~scanf("%d%d%d",&A,&B,&C))    {        BFS();    }    return 0;}/*                             简单点                   出题的方式简单点                   递进的大题请省略                     你又不是教研员                     别设计那些情节                              没意见                   我只想看看你怎么编                       你出的题太表面                       像有套路的考卷                       考生一眼能看见             该配合你得分的我演视而不见           在逼一个最爱你的学生撕掉试卷             什么时候我们开始收起及格线          顺应时代的谎言做那些没用的试卷            可你曾经那么爱我干嘛加难试卷            我该考成什么样子才能快点毕业            原来当我交出考卷后的这些那些                           才是考验   */
0 0
原创粉丝点击