POJ3414:Pots
kuangbin专题一:H题
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 18771 | Accepted: 7948 | 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)
题意:给出两个杯子 容量分别为 a, b, 对两个杯子进行六种操作 FILE(1) FILE(2) DROP(1) DROP(2) POUR(1,2) POUR( 2,1)
问最少操作多少次 可以得到 水量c (c<max(a,b) ) 并且输出 操作过程 否则 输出 impossible
思路:看到最少操作多少次 第一思路是 广度优先搜索 , 但是本题要求输出 操作过程 ,所以不用广搜 用深搜 ,搜索的同时记下路径
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <limits> using namespace std ; #define maxn 120 bool visit[maxn][maxn] ; int path[maxn*100] , result[maxn*100] ; bool flag ; int step ; int a , b , c ; void DFS(int x , int y , int step2 ){ if(step2 > step){//肯定不是最优解 排除 return; } if(x==c||y==c){ if(step2 < step ){ // 逐渐得到最优解 flag = true ; // 找到解 step = step2 ; for(int i=0 ; i< step ; i++){ //存放最优解路径 防止接下来的搜索改变最优路径 result[i] = path[i] ; } } return; } // 六种操作 // FILE(1) if(x<a && !visit[a][y]){ visit[a][y] = 1 ; path[step2] = 1 ; DFS(a , y , step2+1) ; visit[a][y] = 0 ; } // FILE(2) if(y<b &&!visit[x][b]){ visit[x][b] = 1 ; path[step2] = 2 ; DFS(x , b , step2 + 1) ; visit[x][b] = 0 ; } // DROP(1) if(x>0 && !visit[0][y]){ visit[0][y] = 1 ; path[step2] = 3 ; DFS(0 , y , step2 + 1 ) ; visit[0][y] = 0 ; } // DROP(2) if(y>0 && !visit[x][0]){ visit[x][0] = 1 ; path[step2] = 4 ; DFS(x , 0 , step2 + 1 ) ; visit[x][0] = 0 ; } int min_num = 0 ; // POUR(1,2) min_num = min(x , b-y) ; if(x>0 && y<b && !visit[x-min_num][y+min_num]){ visit[x-min_num][y+min_num] = 1 ; path[step2] = 5 ; DFS(x-min_num , y+min_num , step2 + 1 ) ; visit[x-min_num][y+min_num] = 0 ; } // POUR(2,1) min_num = min(y , a - x ) ; if(y>0 && x<a && !visit[x+min_num][y-min_num]){ visit[x+min_num][y-min_num] = 1 ; path[step2] = 6 ; DFS(x+min_num , y-min_num , step2+1) ; visit[x+min_num][y-min_num] = 0 ; } return; } int main() { while(~scanf("%d%d%d" , &a , &b , &c)) { memset(visit , 0 , sizeof(visit)) ; visit[0][0] = 1 ; flag = false ; step = INT_MAX ; DFS(0 , 0 , 0 ) ; if(flag) { printf("%d " , step) ; for(int i= 0 ; i<step ; i++) { // 由最优解操作顺序编号输出顺序操作过程 if( result[i] == 1 ) { printf("FILL(1) ") ; } if(result[i] == 2) { printf("FILL(2) ") ; } if(result[i] == 3) { printf("DROP(1) ") ; } if(result[i] == 4) { printf("DROP(2) ") ; } if(result[i] == 5) { printf("POUR(1,2) "); } if(result[i] == 6) { printf("POUR(2,1) "); } } } else { printf("impossible ") ; } } return 0 ; }