- POTS
总时间限制:
-
描述
-
You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:
- FILL(i) fill the pot i (1 ≤ i ≤ 2) from the tap;
- DROP(i) empty the pot i to the drain;
- 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.
输入
-
On the first and only line are the numbers A, B, and C. These are all integers in the range from 1 to 100 and C≤max(A,B).
输出
-
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’.
样例输入
-
3 5 4
样例输出
6 FILL(2) POUR(2,1) DROP(1) POUR(2,1) FILL(2) POUR(2,1)
注意点有:1.main内定义的变量没初始化找了好久错误;2.queue队列中元素要相互指针的话要建一个结构数组,不然指针指向的元素一直改变会导致所有都指向最后那一个元素。
代码如下:
#include<cstdio> #include <iostream> #include <queue> #include <string.h> using namespace std; int uu[117][117]; int a,b,c; int ux[1000]; typedef struct hh{ int n; int func; int x,y;//after func; struct hh* p; }hh; hh stm1; int main() { cin>>a>>b>>c; hh x = {0,0,0,0,NULL}; int t = -1; queue<hh>hq; uu[0][0] = 1; hh stm[500]; hq.push(x); hh py = {0}; while(!hq.empty()) { t++; stm[t] = hq.front(); if(stm[t].x==c||stm[t].y==c) { py = stm[t]; break; } else { if(stm[t].x!=a) { stm1.n = stm[t].n+1; stm1.func = 1; stm1.x = a; stm1.y = stm[t].y; stm1.p = &stm[t]; if(uu[stm1.x][stm1.y]); else { uu[stm1.x][stm1.y] = 1; hq.push(stm1); } } if(stm[t].y!=b) { stm1.n = stm[t].n+1; stm1.func = 2; stm1.y = b; stm1.x = stm[t].x; stm1.p = &stm[t]; if(uu[stm1.x][stm1.y]); else { uu[stm1.x][stm1.y] = 1; hq.push(stm1); } } if(stm[t].y!=b&&stm[t].x!=0) { stm1.n = stm[t].n+1; stm1.func = 3; if(stm[t].x >= (b - stm[t].y)) { stm1.x =stm[t].x-( b-stm[t].y); stm1.y = b; } else { stm1.x = 0; stm1.y = stm[t].x+stm[t].y; } stm1.p = &stm[t]; if(uu[stm1.x][stm1.y]); else { uu[stm1.x][stm1.y] = 1; hq.push(stm1); } } if(stm[t].x!=a&&stm[t].y!=0) { stm1.n = stm[t].n+1; stm1.func = 4; if(stm[t].y >= (a - stm[t].x)) { stm1.y = stm[t].y- (a-stm[t].x); stm1.x = a; } else { stm1.y = 0; stm1.x = stm[t].x+stm[t].y; } stm1.p = &stm[t]; if(uu[stm1.x][stm1.y]); else { uu[stm1.x][stm1.y] = 1; hq.push(stm1); } } if(stm[t].x!=0) { stm1.n = stm[t].n+1; stm1.func = 5; stm1.x = 0; stm1.y = stm[t].y; stm1.p = &stm[t]; if(uu[stm1.x][stm1.y]); else { uu[stm1.x][stm1.y] = 1; hq.push(stm1); } } if(stm[t].y!=0) { stm1.n = stm[t].n+1; stm1.func = 6; stm1.y = 0; stm1.x = stm[t].x; stm1.p = &stm[t]; if(uu[stm1.x][stm1.y]); else { uu[stm1.x][stm1.y] = 1; hq.push(stm1); } } } hq.pop(); } if(py.n==0) cout<<"impossible"<<endl; else { cout<<py.n<<endl; while(py.p) { ux[py.n] = py.func; py = *py.p; } for(int i=1;ux[i]!=0;i++) { if(ux[i] == 1) cout<<"FILL(1)"<<endl; if(ux[i] == 2) cout<<"FILL(2)"<<endl; if(ux[i] == 3) cout<<"POUR(1,2)"<<endl; if(ux[i] == 4) cout<<"POUR(2,1)"<<endl; if(ux[i] == 5) cout<<"DROP(1)"<<endl; if(ux[i] == 6) cout<<"DROP(2)"<<endl; } } return 0; }