Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 8540 | Accepted: 3614 | 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 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
6 FILL(2) POUR(2,1) DROP(1) POUR(2,1) FILL(2) POUR(2,1) 分析:简单广搜+模拟倒水过程,关键点在输出路径,两个二维数组,一个保存当前状态的前一个状态, 一个保存前一个状态到当期状态的操作,然后递归打印路径。#include <iostream> #include <cstdio> #include <string> #include <queue> using namespace std; bool visit[110][110]; int pos[110][110];//保存操作 int A, B, C; int dd[] = {1, 2, 11, 22, 12, 21};// 把1倒空,把2倒空,把1倒满,把2倒满,1->2,2->1 struct Data { int x, y; int sum; }; Data my[110][110];//保存状态 queue<Data>q; Data daoshui(Data data, int num)//模拟倒水过程 { Data c = data; c.sum = data.sum + 1; switch(num) { case 1: c.x = 0; if (!visit[c.x][c.y]) { pos[c.x][c.y] = 1; my[c.x][c.y] = data; } break; case 2: c.y = 0; if (!visit[c.x][c.y]) { pos[c.x][c.y] = 2; my[c.x][c.y] = data; } break; case 11: c.x = A; if (!visit[c.x][c.y]) { pos[c.x][c.y] = 11; my[c.x][c.y] = data; } break; case 22: c.y = B; if (!visit[c.x][c.y]) { pos[c.x][c.y] = 22; my[c.x][c.y] = data; } break; case 12: if (B - data.y > data.x) { c.x = 0; c.y = data.y + data.x; } else { c.x = data.x - (B - data.y); c.y = B; } if (!visit[c.x][c.y]) { pos[c.x][c.y] = 12; my[c.x][c.y] = data; } break; case 21: if (A - data.x > data.y) { c.y = 0; c.x = data.x + data.y; } else { c.y = data.y - (A - data.x); c.x = A; } if (!visit[c.x][c.y]) { pos[c.x][c.y] = 21; my[c.x][c.y] = data; } break; } return c; } void print(int num) { switch(num) { case 1: puts("DROP(1)"); break; case 2: puts("DROP(2)"); break; case 11:puts("FILL(1)"); break; case 22: puts("FILL(2)");break; case 12:puts("POUR(1,2)"); break; case 21:puts("POUR(2,1)");break; } } void print2(int num, Data temp)//递归打印路径 { if (num == 0) { return ; } print2(num - 1, my[temp.x][temp.y]); print(pos[temp.x][temp.y]); } int main() { int f = 0; scanf("%d%d%d", &A, &B, &C); if (C == 0) { printf("0\n"); return 0; } Data data; data.x = 0; data.y = 0; data.sum = 0; visit[0][0] = true; q.push(data); while (!q.empty()) { data = q.front(); q.pop(); for (int i = 0; i < 6; i++) { Data c = daoshui(data, dd[i]); if (c.x == C || c.y == C) { f = 1; printf("%d\n", c.sum); Data temp = c; print2(c.sum, temp); break; } if (!visit[c.x][c.y]) { visit[c.x][c.y] = true; q.push(c); } } if (f == 1) break; } if (f == 0) printf("impossible\n"); return 0; }