(杯子倒水问题)Pots(p3414)

来源:互联网 发布:网络协议与分析 编辑:程序博客网 时间:2024/06/02 21:00

这个题搞了好久,到了快凌晨三点钟了,终于出来了。都是自己太不小心。还真的以为别人的代码是0MS,而我的只是TL的命。


结果是一个vist[][]里写错了一个,搞得我在输出的时候一直打圈圈了,好辛苦。。有收获,只是有一点不值得。哈哈。小心为好。


写得有一点长了,


#include<iostream>#include<cstdio>#include<cstring> #include<ctype.h>#include<vector>#include<algorithm>#include<queue>#include<stack>using namespace std; #define N 101struct you{int a,b;} pre[N][N];   //记录前一个结点bool vist[N][N];  int a,b,c;int fang[N][N];  //记录当前的操作方法,struct my{int d;int p;int a,b;};int main(){int i,j,k;(cin>>a>>b>>c);{queue<my> q;while (!q.empty())q.pop();my temp;temp.d=0;temp.a=0;temp.b=0;temp.p=0;memset(vist,false,sizeof(vist));vist[0][0]=true;q.push(temp);my cur;for (i=0;i<N;i++){for (j=0;j<N;j++)pre[i][j].a=i,pre[i][j].b=j;}bool ans=true;while (!q.empty()){temp=q.front();q.pop();if (temp.a==c||temp.b==c){ans=false;break;}if (temp.a==a){if (!vist[0][temp.b]){vist[0][temp.b]=true;cur.d=temp.d+1;cur.a=0;cur.b=temp.b;q.push(cur);pre[cur.a][cur.b].a=temp.a;pre[cur.a][cur.b].b=temp.b;fang[cur.a][cur.b]=1;}}if (temp.b==b){if (!vist[temp.a][0]){vist[temp.a][0]=true;cur.d=temp.d+1;cur.a=temp.a;cur.b=0;q.push(cur);pre[cur.a][cur.b].a=temp.a;pre[cur.a][cur.b].b=temp.b;fang[cur.a][cur.b]=2;}}if (temp.a==0){if (!vist[a][temp.b]){vist[a][temp.b]=true;cur.d=temp.d+1;cur.a=a;cur.b=temp.b;q.push(cur);pre[cur.a][cur.b].a=temp.a;pre[cur.a][cur.b].b=temp.b;fang[cur.a][cur.b]=3;}}if (temp.b==0){if (!vist[temp.a][b]){vist[temp.a][b]=true;cur.d=temp.d+1;cur.a=temp.a;cur.b=b;q.push(cur);pre[cur.a][cur.b].a=temp.a;pre[cur.a][cur.b].b=temp.b;fang[cur.a][cur.b]=4;}}if (temp.a<a&&temp.b){if (temp.a+temp.b<=a){if (!vist[temp.a+temp.b][0]){vist[temp.a+temp.b][0]=true;cur.d=temp.d+1;cur.a=temp.a+temp.b;cur.b=0;q.push(cur);pre[cur.a][cur.b].a=temp.a;pre[cur.a][cur.b].b=temp.b;fang[cur.a][cur.b]=5;}}else{if (!vist[a][temp.b+temp.a-a]){vist[a][temp.b+temp.a-a]=true;cur.d=temp.d+1;cur.a=a;cur.b=temp.b+temp.a-a;q.push(cur);pre[cur.a][cur.b].a=temp.a;pre[cur.a][cur.b].b=temp.b;fang[cur.a][cur.b]=5;}}}if (temp.b<b&&temp.a){if (temp.a+temp.b<=b){if (!vist[0][temp.a+temp.b]){vist[0][temp.a+temp.b]=true;cur.d=temp.d+1;cur.b=temp.a+temp.b;cur.a=0;q.push(cur);pre[cur.a][cur.b].a=temp.a;pre[cur.a][cur.b].b=temp.b;fang[cur.a][cur.b]=6;}}else{if (!vist[temp.a+temp.b-b][b]){vist[temp.a+temp.b-b][b]=true;//vist[a][temp.b+temp.a-a]=true;         就是这个一个地方啊,,cur.d=temp.d+1; cur.b=b;cur.a=temp.b+temp.a-b;q.push(cur);pre[cur.a][cur.b].a=temp.a;pre[cur.a][cur.b].b=temp.b;fang[cur.a][cur.b]=6;}}}}if (ans){cout<<"impossible"<<endl;return 0;}cout<<temp.d<<endl;stack<you> p;you t;t.a=temp.a;t.b=temp.b;while (!p.empty()){p.pop();}k=0;while ((t.a||t.b)){//k++;p.push(t);i=t.a;j=t.b;t.a=pre[i][j].a;t.b=pre[i][j].b;}//cout<<k<<endl;while (!p.empty()){t=p.top();p.pop();//cout<<t.a<<' '<<t.b<<' '<<fang[t.a][t.b]<<endl;switch(fang[t.a][t.b]){case 4: cout<<"FILL(2)"<<endl; break;case 3: cout<<"FILL(1)"<<endl; break;case 1: cout<<"DROP(1)"<<endl; break;case 2: cout<<"DROP(2)"<<endl; break;case 5: cout<<"POUR(2,1)"<<endl; break;case 6: cout<<"POUR(1,2)"<<endl; break;/*case 1: cout<<"DROP("<<2<<')'<<endl;break;case 2: cout<<"DROP("<<t.b<<')'<<endl; break;case 3: cout<<"FILL("<<t.a<<')'<<endl; break;case 4: cout<<"FILL("<<t.b<<')'<<endl; break;case 5: cout<<"POUR("<<t.a<<','<<t.b<<')'<<endl; break;case 6: cout<<"POUR("<<t.b<<','<<t.a<<')'<<endl; break;*/}}}return 0;}





Pots
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 6621 Accepted: 2782 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 ≤ 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

On the first and only line are the numbers A, B, andC. 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 operationsK. 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

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

Source




原创粉丝点击