Pots

来源:互联网 发布:aspen plus软件参数 编辑:程序博客网 时间:2024/05/22 02:29

Pots

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 57   Accepted Submission(s) : 16
Special Judge
Problem 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 ≤ i ≤ 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 potj is full (and there may be some water left in the pot i), or the poti 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 exactlyC liters of water in one of the pots.

 

Input
<p>On the first and only line are the numbers <b>A</b>, <b>B</b>, and <b>C</b>. These are all integers in the range from 1 to 100 and <b>C</b>≤max(<b>A</b>,<b>B</b>).</p>
 

Output
<p>The first line of the output must contain the length of the sequence of operations <b>K</b>. The following <b>K</b> 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 ‘<b>impossible</b>’.</p>
 

Sample Input
3 5 4
 

Sample Output
6FILL(2)POUR(2,1)DROP(1)POUR(2,1)FILL(2)POUR(2,1)
 

Source
PKU
 

题意:

给你两杯水的体积,给你几种操作,使得给出的体积出现时的最小操作次数!

思路:

本以为广搜会超时,但并没有,总共是有六种操作,每一种每一种的搜即可!

代码:

#include<iostream>#include<string>#include<cstdio>#include<algorithm>#include<cmath>#include<iomanip>#include<queue>#include<cstring>using namespace std;int aa,bb,cc,num;bool flag,judge[201][201];string s[1000],tt[1000];void dfs(int a,int b,int step)  //ab代表每个水杯现在体积,step代表操作几次{    if(a==cc||b==cc)    {        flag=1;        if((step-1)<num)  //找最小的        {            num=step-1;            for(int i=1;i<step;i++)                tt[i]=s[i];     //此处需要替换,否则WA        }        return;    }    if(step>5000)   //不可能时的情况        { return; }    if(judge[a][bb]==0)    {        judge[a][bb]=1;        s[step]="FILL(2)";        dfs(a,bb,step+1);        judge[a][bb]=0;    }    if(judge[aa][b]==0)    {        judge[aa][b]=1;;        s[step]="FILL(1)";        dfs(aa,b,step+1);        judge[aa][b]=0;    }    if(!judge[a][0])    {        judge[a][0]=1;        s[step]="DROP(2)";        dfs(a,0,step+1);        judge[a][0]=0;    }    if(!judge[0][b])    {        judge[0][b]=1;        s[step]="DROP(1)";        dfs(0,b,step+1);        judge[0][b]=0;    }    int a1=a,b1=b;    if((b1+a1)<=bb) {b1=b1+a1; a1=0;}    else {a1=a1-(bb-b1); b1=bb;}    if(!judge[a1][b1])    {        judge[a1][b1]=1;        s[step]="POUR(1,2)";        dfs(a1,b1,step+1);        judge[a1][b1]=0;;    }    a1=a,b1=b;    if((b1+a1)<=aa) {a1=b1+a1; b1=0;}    else {b1=b1-(aa-a1); a1=aa;}    if(!judge[a1][b1])    {        judge[a1][b1]=1;        s[step]="POUR(2,1)";        dfs(a1,b1,step+1);        judge[a1][b1]=0;;    }}int main(){   cin>>aa>>bb>>cc;    memset(judge,0,sizeof(judge));    num=1000000;     flag=0;    dfs(0,0,1);    if(flag)    {        cout<<num<<endl;        for(int i=1;i<=num;i++)            cout<<tt[i]<<endl;    }    else    {        cout<<"impossible"<<endl;    }    return 0;}
心得:
气死我了。。。。。。一个水体做了一上午    醉了  。。。。。。自己好水。。。。。。。