Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 10300 | Accepted: 4362 | Special Judge |
Description
You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:
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, and C. These are all integers in the range from 1 to 100 andC≤max(A,B).
Output
The first line of the output must contain the length of the sequence of operationsK. The followingK 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)
Source
#include <cstdio> #include <queue> #include <cstring> using namespace std; int A, B, C, ans; int re[100000], cnt = 0; bool vis[105][105]; char out[7][10] = {"","FILL(1)", "FILL(2)", "DROP(1)", "DROP(2)", "POUR(1,2)", "POUR(2,1)"}; struct NUM { int a, b; //a, b表示两个杯子中水的体积 int step; //step表示操作次数 int now; //now表示当前的操作序号 int pre[5000][10]; //pre记录顺序 }; void BFS() { memset(vis, false, sizeof(vis)); queue<NUM> q; NUM st, t, cur; memset(st.pre, 0, sizeof(st.pre)); st.a = 0; st.b = 0; st.step = 0; st.now = 0; q.push(st); while(!q.empty()) { t = q.front(); cur = t; q.pop(); if(t.a == C || t.b == C) { printf("%d\n", t.step); while(t.now) //迭代得到操作序列 { re[cnt++] = t.now; t.now = t.pre[t.step--][t.now]; } return; } for(int i = 1; i <= 6; i++) { t = cur; //每次要将t还原! if(i == 1)//fill(1) { t.now = 1; t.a = A; t.step ++; if(!vis[t.a][t.b]) { vis[t.a][t.b] = true; t.pre[t.step][t.now] = cur.now; q.push(t); } } if(i == 2) //fill(2) { t.now = 2; t.b = B; t.step ++; if(!vis[t.a][t.b]) { vis[t.a][t.b] = true; t.pre[t.step][t.now] = cur.now; q.push(t); } } if(i == 3) // drop(1) { t.now = 3; t.a = 0; t.step ++; if(!vis[t.a][t.b]) { vis[t.a][t.b] = true; t.pre[t.step][t.now] = cur.now; q.push(t); } } if(i == 4)//drop(2) { t.now = 4; t.b = 0; t.step ++; if(!vis[t.a][t.b]) { vis[t.a][t.b] = true; t.pre[t.step][t.now] = cur.now; q.push(t); } } if(i == 5)//pour(1,2) { t.now = 5; int tmp = t.b; if(t.b + t.a >= B) { t.b = B; t.a -= (B - tmp); } else { t.b += t.a; t.a = 0; } t.step ++; if(!vis[t.a][t.b]) { vis[t.a][t.b] = true; t.pre[t.step][t.now] = cur.now; q.push(t); } } if(i == 6) //pour(2,1) { t.now = 6; int tmp = t.a; if(t.b + t.a >= A) { t.a = A; t.b -= (A - tmp); } else { t.a += t.b; t.b = 0; } t.step ++; if(!vis[t.a][t.b]) { vis[t.a][t.b] = true; t.pre[t.step][t.now] = cur.now; q.push(t); } } } } ans = -1; return; } int main() { scanf("%d %d %d", &A, &B, &C); BFS(); if(ans == -1) printf("impossible\n"); else for(int i = cnt - 1; i >= 0; i--) printf("%s\n", out[re[i]]); }
原文地址:http://blog.csdn.net/tc_to_top/article/details/43699407