VJ Pots
来源:互联网 发布:喷涂机器人及软件编程 编辑:程序博客网 时间:2024/05/16 07:08
H - 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 4Sample Output
6FILL(2)POUR(2,1)DROP(1)POUR(2,1)FILL(2)POUR(2,1)
吐槽:自己和一个智障一样调了半天的程序,最后才发现因为每次对字符串赋值导致TLE
1 #include <iostream> 2 #include <cstring> 3 #include <stack> 4 #define N 110 5 using namespace std; 6 int A,B,C; 7 bool vis[N][N]; 8 bool flag; 9 struct node{ 10 int a,b; 11 int step; 12 int oper; 13 node *former; 14 }point[N*N]; 15 string cnt[6]={"FILL(1)","FILL(2)","DROP(1)","DROP(2)","POUR(1,2)","POUR(2,1)"}; 16 void print(node *p){ 17 stack<int> s; 18 while(p->former!=NULL){ 19 s.push(p->oper); 20 p=p->former; 21 } 22 while(!s.empty()){ 23 cout<<cnt[s.top()]<<endl; 24 s.pop(); 25 } 26 return ; 27 } 28 void bfs(){ 29 int head = 0; 30 int tail = 1; 31 while(head<tail){ 32 node *now = &point[head++]; 33 int a=now->a; 34 int b=now->b; 35 int step=now->step; 36 if(a==C||b==C){ 37 cout<<step<<endl; 38 print(now); 39 flag=true; 40 return ; 41 } 42 for(int i=0;i<3;i++){ 43 switch (i){ 44 case 0: 45 if(a<A&&!vis[A][b]){ 46 vis[A][b]=true; 47 point[tail].a=A; 48 point[tail].b=b; 49 point[tail].step=step+1; 50 point[tail].oper=0; 51 point[tail++].former=now; 52 } 53 if(b<B&&!vis[a][B]){ 54 vis[a][B]=true; 55 point[tail].a=a; 56 point[tail].b=B; 57 point[tail].step=step+1; 58 point[tail].oper=1; 59 point[tail++].former=now; 60 } 61 break; 62 case 1: 63 if(a>0&&!vis[0][b]){ 64 vis[0][b]=true; 65 point[tail].a=0; 66 point[tail].b=b; 67 point[tail].step=step+1; 68 point[tail].oper=2; 69 point[tail++].former=now; 70 } 71 if(b>0&&!vis[a][0]){ 72 vis[a][0]=true; 73 point[tail].a=a; 74 point[tail].b=0; 75 point[tail].step=step+1; 76 point[tail].oper=3; 77 point[tail++].former=now; 78 } 79 break; 80 case 2: 81 if(a>0&&b<B){ 82 if(a>(B-b)&&!vis[a-(B-b)][B]){ 83 vis[a-(b-b)][B]=true; 84 point[tail].a = a-(B-b); 85 point[tail].b = B; 86 point[tail].step = step+1; 87 point[tail].former = now; 88 point[tail++].oper = 4; 89 } 90 else if(!vis[0][a+b]){ 91 vis[0][a+b] = true; 92 point[tail].a = 0; 93 point[tail].b = a+b; 94 point[tail].step = step+1; 95 point[tail].former = now; 96 point[tail++].oper = 4; 97 } 98 } 99 if(a<A&&b>0){100 if(b>(A-a)&&!vis[A][b-(A-a)]){101 vis[A][b-(A-a)]=true;102 point[tail].a = A; 103 point[tail].b = b-(A-a); 104 point[tail].step = step+1; 105 point[tail].former = now; 106 point[tail++].oper = 5; 107 }108 else if(!vis[a+b][0]){109 vis[0][a+b] = true; 110 point[tail].a = a+b; 111 point[tail].b = 0; 112 point[tail].step = step+1; 113 point[tail].former = now; 114 point[tail++].oper = 5; 115 }116 }117 break;118 }119 }120 }121 }122 int main(){123 cin.sync_with_stdio(false);124 while(cin>>A>>B>>C){125 flag=false;126 memset(vis,0,sizeof(vis));127 vis[0][0]=true;128 point[0].a=0;129 point[0].b=0;130 point[0].step=0;131 point[0].oper=-1;132 point[0].former=NULL;133 bfs();134 if(!flag){135 cout<<"impossible"<<endl;136 }137 }138 return 0;139 }
2017-03-18 15:57:53