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;
    }
  • 相关阅读:
    shell脚本查找tcp过多ip地址封掉
    tomcat日志传参乱码问题
    nginx部署vue跨域proxy方式
    nginx部署VUE跨域访问api
    springboot2.1.3 + redisTemplate + Lock 操作 redis 3.0.5
    java8 lamb表达式对List排序
    Mysql5.7降级到5.6遇到的坑
    mac中git使用
    mac中git flow使用
    mac安装openjdk8-maven-mysql-git-docker
  • 原文地址:https://www.cnblogs.com/Annetree/p/5830500.html
Copyright © 2011-2022 走看看