zoukankan      html  css  js  c++  java
  • poj 3414 pots (bfs+路径记录)

    Pots
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 11703   Accepted: 4955   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 ≤ ≤ 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)

    好爽,一遍ac

    /*************************************************************************
        > File Name: code/2015summer/searching/H.cpp
        > Author: 111qqz
        > Email: rkz2013@126.com 
        > Created Time: 2015年07月27日 星期一 09时11分28秒
     ************************************************************************/
    
    #include<iostream>
    #include<iomanip>
    #include<cstdio>
    #include<algorithm>
    #include<cmath>
    #include<cstring>
    #include<string>
    #include<map>
    #include<set>
    #include<queue>
    #include<vector>
    #include<stack>
    #define y0 abc111qqz
    #define y1 hust111qqz
    #define yn hez111qqz
    #define j1 cute111qqz
    #define tm crazy111qqz
    #define lr dying111qqz
    using namespace std;
    #define REP(i, n) for (int i=0;i<int(n);++i)  
    typedef long long LL;
    typedef unsigned long long ULL;
    const int N=1E2+5;
    int A,B,C;
    int d[N][N];
    bool flag;
    struct node
    {
        int d,opt,par,prea,preb;
    }q[N][N];
    
    void print(int x,int y)
    {
     //    cout<<"x:"<<x<<"y:"<<y<<endl;
        if (q[x][y].prea!=-1&&q[x][y].preb!=-1)
        {
    //       cout<<"who is 111qqz"<<endl;
          print(q[x][y].prea,q[x][y].preb);
          if (q[x][y].opt==1){
            printf("FILL(%d)
    ",q[x][y].par);
          }
          if (q[x][y].opt==2)
          {
            printf("DROP(%d)
    ",q[x][y].par);
          }
          if (q[x][y].opt==3)
          {
            printf("POUR(%d,%d)
    ",q[x][y].par,3-q[x][y].par);
          }
        }
    
    }
    void bfs()
    {
        memset(q,-1,sizeof(q));
        queue<int>a;
        queue<int>b;
        a.push(0);
        b.push(0);
        q[0][0].d=0;
        while (!a.empty()&&!b.empty())
        {
          int av = a.front();a.pop();
          int bv = b.front();b.pop();
    //       cout<<"av:"<<av<<"bv:"<<bv<<endl;
          if (av==C||bv==C)
          {
            flag = true;
            //cout<<"yeah~~~~~~~~~~~~~~~"<<endl;
            cout<<q[av][bv].d<<endl;
    //         cout<<"prea:"<<q[av][bv].prea<<"preb:"<<q[av][bv].preb<<endl;
            print(av,bv);
              return;
          }
          if (av<A&&q[A][bv].d==-1)
          {
            q[A][bv].d=q[av][bv].d+1;
            q[A][bv].opt=1;
            q[A][bv].par=1;
            q[A][bv].prea=av;
            q[A][bv].preb=bv;
            a.push(A);
            b.push(bv);
          }
          if (av>0&&q[0][bv].d==-1)
          {
            q[0][bv].d=q[av][bv].d+1;
            q[0][bv].opt=2;
            q[0][bv].par=1;
            q[0][bv].prea=av;
            q[0][bv].preb=bv;
            a.push(0);
            b.push(bv);
    
          }
          if (bv<B&&q[av][B].d==-1)
          {
            q[av][B].d=q[av][bv].d+1;
            q[av][B].opt=1;
            q[av][B].par=2;
            q[av][B].prea = av;
            q[av][B].preb = bv;
            a.push(av);
            b.push(B);
            
          }
          if (bv>0&&q[av][0].d==-1)
          {
            q[av][0].d=q[av][bv].d+1;
            q[av][0].opt=2;
            q[av][0].par=2;
            q[av][0].prea=av;
            q[av][0].preb=bv;
            a.push(av);
            b.push(0);
          }
    
          if (av+bv<=B&&q[0][av+bv].d==-1)
          {
            q[0][av+bv].d=q[av][bv].d+1;
            q[0][av+bv].opt=3;
            q[0][av+bv].par=1;
            q[0][av+bv].prea=av;
            q[0][av+bv].preb=bv;
            a.push(0);
            b.push(av+bv);
          }
          if (av+bv>B&&q[av-(B-bv)][B].d==-1)   //把1往2里倒入的两种情况
          {
          
            int tmp = av-(B-bv);
            q[tmp][B].d=q[av][bv].d+1;
            q[tmp][B].opt=3;
            q[tmp][B].par=1;
            q[tmp][B].prea=av;
            q[tmp][B].preb=bv;
            a.push(tmp);
            b.push(B);
          }
    
          if (bv+av<=A&&q[av+bv][0].d==-1)
          {
            q[av+bv][0].d=q[av][bv].d+1;
            q[av+bv][0].opt=3;
            q[av+bv][0].par=2;
            q[av+bv][0].prea=av;
            q[av+bv][0].preb=bv;
            a.push(av+bv);
            b.push(0);
          }
          if (bv+av>A&&q[A][bv-(A-av)].d==-1)
          {
            int tmp = bv-(A-av);
            q[A][tmp].d=q[av][bv].d+1;
            q[A][tmp].opt=3;
            q[A][tmp].par=2;
            q[A][tmp].prea=av;
            q[A][tmp].preb=bv;
            a.push(A);
            b.push(tmp);
          }
        }
    }
    int main()
    {
        
        flag = false;
        cin>>A>>B>>C;
        bfs();
        if (!flag)
        {
          cout<<"impossible"<<endl;
        }
        
        
        return 0;
    }
  • 相关阅读:
    ORACLE的主要进程DBWn,LGWR的工作原理
    ORACLE PGA
    如何精确获取ORACLE的秒?
    Dictionary.TryGetValue 方法 试用记 Mark
    在C#中如何实现Form与Form之间的通信
    在建立与服务器的连接时出错。在连接到 SQL Server 2005 时,在默认的设置下 SQL Server 不允许进行远程连接可能会导致此失败。 (provider: 命名管道提供程序, error: 40 无法打开到 SQL Server 的连接)
    在C#程序中实现插件架构
    兄弟,你有弄过TestDriven.NET吗?
    由C#风潮想起的-给初学编程者的忠告
    今天真是不在状态的一天,又是一个书写的错误(本文仅为提醒自己,你可以选择跳过)
  • 原文地址:https://www.cnblogs.com/111qqz/p/4680562.html
Copyright © 2011-2022 走看看