zoukankan      html  css  js  c++  java
  • POJ 3414 Pots(BFS)

    Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

    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 ≤ ≤ 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 AB, 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)

    怎么说呢,BFS,有六种状态
    写起来还是有点烦的,要细心
    #include<cstdio>
    #include <cstring>
    #include <cstdlib>
    #include <cmath>
    #include <algorithm>
    #include<queue>;
    using namespace std;
    struct node
    {
        int av,aw,bv,bw;
        int o[105],pos;
    }s;
    bool vis[105][105];
    int f;
    node cur,next;
    void fill(int i)
    {
        if(i==1)
            next.aw=next.av;
        else if(i==2)
            next.bw=next.bv;
    }
    void drop(int i)
    {
        if(i==1)
            next.aw=0;
        else
            next.bw=0;
    }
    void pour(int a,int b)
    {
        if(a==1&&b==2){
        if(next.aw>next.bv-next.bw)
        {
            next.aw-=(next.bv-next.bw);
            next.bw=next.bv;
        }
        else
        {
            next.bw+=next.aw;
            next.aw=0;
        }}
        else
        {
            if(next.bw>next.av-next.aw)
        {
            next.bw-=(next.av-next.aw);
            next.aw=next.av;
        }
        else
        {
            next.aw+=next.bw;
            next.bw=0;
        }
        }
    }
    void print(int i)
    {
        if(i==1)
            printf("POUR(1,2)
    ");
        else if(i==2)
            printf("POUR(2,1)
    ");
        else if(i==3)
            printf("FILL(1)
    ");
        else if(i==4)
            printf("FILL(2)
    ");
        else if(i==5)
            printf("DROP(1)
    ");
        else if(i==6)
            printf("DROP(2)
    ");
    }
    bool bfs()
    {
        memset(vis,false,sizeof(vis));
        s.aw=0;s.bw=0;
        s.pos=0;
        vis[0][0]=true;
        queue<node>Q;
        Q.push(s);
        while(!Q.empty())
        {
            cur=Q.front();
            Q.pop();
            if(f==cur.aw||f==cur.bw)
            {
                printf("%d
    ",cur.pos);
                for(int i=0;i<cur.pos;i++)
                {
                    print(cur.o[i]);
                }
                return true;
                break;
            }
            //1
            if(cur.aw!=0&&cur.bw!=cur.bv){
            next=cur;
            pour(1,2);
            next.o[next.pos]=1;
            next.pos++;
            if(!vis[next.aw][next.bw])
            {
                vis[next.aw][next.bw]=true;
                Q.push(next);
            }}
            //2
            if(cur.bw!=0&&cur.aw!=cur.av){
            next=cur;
            pour(2,1);
            next.o[next.pos]=2;
            next.pos++;
            if(!vis[next.aw][next.bw])
            {
                vis[next.aw][next.bw]=true;
                Q.push(next);
            }}
            //3
            if(cur.aw!=cur.av){
            next=cur;
            fill(1);
            next.o[next.pos]=3;
            next.pos++;
            if(!vis[next.aw][next.bw])
            {
                vis[next.aw][next.bw]=true;
                Q.push(next);
            }}
            //4
            if(cur.bw!=cur.bv){
            next=cur;
            fill(2);
            next.o[next.pos]=4;
            next.pos++;
            if(!vis[next.aw][next.bw])
            {
                vis[next.aw][next.bw]=true;
                Q.push(next);
            }}
            //5
            if(cur.aw!=0){
            next=cur;
            drop(1);
            next.o[next.pos]=5;
            next.pos++;
            if(!vis[next.aw][next.bw])
            {
                vis[next.aw][next.bw]=true;
                Q.push(next);
            }}
            //6
            if(cur.bw!=0){
            next=cur;
            drop(2);
            next.o[next.pos]=6;
            next.pos++;
            if(!vis[next.aw][next.bw])
            {
                vis[next.aw][next.bw]=true;
                Q.push(next);
            }}
        }
        return false;
    }
    int main()
    {
        while(scanf("%d%d%d",&s.av,&s.bv,&f)!=EOF)
        {
            if(!bfs())printf("impossible
    ");
        }
        return 0;
    }
  • 相关阅读:
    客户端用不用bind的区别
    软件项目开发流程
    很强大的几个网站
    MIS系统的 5个基本模块
    asp.net 生成导出word表单 ,导出excel; dataTable生成xls文件,返回前台下载;asp.net启动excel错误 80070005;excelxls columnName 不能改变; 读写excel的开源利器NPOI; 设置excel Cell的数据类型;
    正则快速入门
    asp.net 服务端调用客户端脚本; asp.net 服务端将文件传给客户端; reponse.ContentType的取值;用OutputStream.Write返回文件,效率是WriteFile的10倍;download link click和OutputStream的比较;
    常用编辑
    数据库流程图设计
    scroll位置控制 window的和div的
  • 原文地址:https://www.cnblogs.com/Annetree/p/5830500.html
Copyright © 2011-2022 走看看