kuangbin专题一:H题,POJ3414:Pots

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:

  1. FILL(i)        fill the pot i (1 ≤ i ≤ 2) from the tap;
  2. DROP(i)      empty the pot i to the drain;
  3. 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\n" , step) ;
            for(int i= 0 ; i<step ; i++) {
                // 由最优解操作顺序编号输出顺序操作过程 
                if( result[i] == 1 ) {
                    printf("FILL(1)\n") ;
                }
                if(result[i] == 2) {
                    printf("FILL(2)\n") ;
                }
                if(result[i] == 3) {
                    printf("DROP(1)\n") ;
                }
                if(result[i] == 4) {
                    printf("DROP(2)\n") ;
                }
                if(result[i] == 5) {
                    printf("POUR(1,2)\n");
                }
                if(result[i] == 6) {
                    printf("POUR(2,1)\n");
                }

            }
        } else {
            printf("impossible\n") ;
        }

    }

    return 0 ;
}

 

转载于:https://www.cnblogs.com/yi-ye-zhi-qiu/p/7643107.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值