Pots
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 13069 |
|
Accepted: 5489 |
|
Special Judge |
Description
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.
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 and C≤max(A,B).
Output
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’.
Sample Input
3 5 4
Sample Output
6
FILL(2)
POUR(2,1)
DROP(1)
POUR(2,1)
FILL(2)
POUR(2,1)
题目大意:
一共两个水杯AB,每次能够倒满,倒空,或者倒入另外一个杯子。问,A和B两个杯子能否倒出第C升的水
遇到的问题和思路:
路径问题,关键是要保存路径。用par[i][j][k],i和j是用来保存水杯的,k是type的三种类型。然后用step[i][j]来保存步数,然后将step清0,起始的位置标为1就可以了。最后就是和HDU1495的喝水问题一样的进行分类讨论就行了。
呼~最近开始不看题解慢慢写题目,写出来感觉好有成就感
给出AC代码
#include<cstdio> #include<cstring> #include<algorithm> #include<queue> using namespace std; struct node{ int va, vb, type; node (int va = 0, int vb = 0, int type = 0) : va(va), vb(vb), type(type) {} }; int a, b, c; node par[105][105][4]; int step[105][105]; queue <node> que; void work(node u){ //type = 1 if (step[a][u.vb] == 0){ step[a][u.vb] = step[u.va][u.vb] + 1; que.push(node(a, u.vb, 1)); // printf("a = %d u.vb = %d\n", a, u.vb); par[a][u.vb][1] = node(u.va, u.vb, u.type); } if (step[u.va][b] == 0){ step[u.va][b] = step[u.va][u.vb] + 1; que.push(node(u.va, b, 1)); par[u.va][b][1] = node(u.va, u.vb, u.type); } //type = 2 if (u.va > 0){ if (u.va >= b - u.vb && step[u.va - b + u.vb][b] == 0){ step[u.va - b + u.vb][b] = step[u.va][u.vb] + 1; que.push(node(u.va - b + u.vb, b, 2)); par[u.va - b + u.vb][b][2] = node(u.va, u.vb, u.type); } else if (u.va < b - u.vb && step[0][u.vb + u.va] == 0){ step[0][u.vb + u.va] = step[u.va][u.vb] + 1; que.push(node(0, u.vb + u.va, 2)); par[0][u.vb + u.va][2] = node(u.va, u.vb, u.type); } } if (u.vb > 0){ if (u.vb >= a - u.va && step[a][u.vb - a + u.va] == 0){ step[a][u.vb - a + u.va] = step[u.va][u.vb] + 1; que.push(node(a, u.vb - a + u.va, 2)); par[a][u.vb - a + u.va][2] = node(u.va, u.vb, u.type); } else if(u.vb < a - u.va && step[u.va + u.vb][0] == 0){ step[u.va + u.vb][0] = step[u.va][u.vb] + 1; que.push(node(u.va + u.vb, 0, 2)); par[u.va + u.vb][0][2] = node(u.va, u.vb, u.type); } } //type = 3 if (u.va > 0 && step[0][u.vb] == 0){ step[0][u.vb] = step[u.va][u.vb] + 1; que.push(node(0, u.vb, 3)); par[0][u.vb][3] = node(u.va, u.vb, u.type); } if (u.vb > 0 && step[u.va][0] == 0){ step[u.va][0] = step[u.va][u.vb] + 1; que.push(node(u.va, 0, 3)); par[u.va][0][3] = node(u.va, u.vb, u.type); } } void prin(node u){ printf("%d\n", step[u.va][u.vb] - 1); vector <node> nd; while (true){ nd.push_back(u); if (step[u.va][u.vb] == 2) break; u = par[u.va][u.vb][u.type]; } for (int i = nd.size() - 1; i >= 0; i--){ if (nd[i].type == 1){ // printf("type = 1 va = %d vb = %d\n", nd[i].va, nd[i].vb); if (nd[i].va == a) printf("FILL(%d)\n", 1); else printf("FILL(%d)\n", 2); } else if (nd[i].type == 2 || i == 0){ // printf("type = 2 va = %d vb = %d\n", nd[i].va, nd[i].vb); if (nd[i].va == 0 || nd[i].va > 0 && nd[i].vb == b){ printf("POUR(%d,%d)\n", 1, 2); } else printf("POUR(%d,%d)\n", 2, 1); } else if (nd[i].type == 3){ // printf("type = 3 va = %d vb = %d\n", nd[i].va, nd[i].vb); if (nd[i].va == 0) printf("DROP(%d)\n", 1); else printf("DROP(%d)\n", 2); } } } void bfs(){ memset(par, 0, sizeof(par)); while (!que.empty()) que.pop(); que.push(node(0, 0, 0)); while (!que.empty()){ node u = que.front(); que.pop(); if (u.va == c || u.vb == c){ prin(u); return ; } work(u); } printf("impossible\n"); } int main(){ while(scanf("%d%d%d", &a, &b, &c) != EOF){ memset(step, 0, sizeof(step)); step[0][0] = 1; bfs(); } return 0; } </node></node></queue></algorithm></cstring></cstdio>