zoukankan      html  css  js  c++  java
  • ZOJ Problem Set

    ZOJ Problem Set - 1005
    Jugs

    Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge

    In the movie "Die Hard 3", Bruce Willis and Samuel L. Jackson were confronted with the following puzzle. They were given a 3-gallon jug and a 5-gallon jug and were asked to fill the 5-gallon jug with exactly 4 gallons. This problem generalizes that puzzle.

    You have two jugs, A and B, and an infinite supply of water. There are three types of actions that you can use: (1) you can fill a jug, (2) you can empty a jug, and (3) you can pour from one jug to the other. Pouring from one jug to the other stops when the first jug is empty or the second jug is full, whichever comes first. For example, if A has 5 gallons and B has 6 gallons and a capacity of 8, then pouring from A to B leaves B full and 3 gallons in A.

    A problem is given by a triple (Ca,Cb,N), where Ca and Cb are the capacities of the jugs A and B, respectively, and N is the goal. A solution is a sequence of steps that leaves exactly N gallons in jug B. The possible steps are

    fill A
    fill B
    empty A
    empty B
    pour A B
    pour B A
    success

    where "pour A B" means "pour the contents of jug A into jug B", and "success" means that the goal has been accomplished.

    You may assume that the input you are given does have a solution.

    Input

    Input to your program consists of a series of input lines each defining one puzzle. Input for each puzzle is a single line of three positive integers: Ca, Cb, and N. Ca and Cb are the capacities of jugs A and B, and N is the goal. You can assume 0 < Ca <= Cb and N <= Cb <=1000 and that A and B are relatively prime to one another.

    Output

    Output from your program will consist of a series of instructions from the list of the potential output lines which will result in either of the jugs containing exactly N gallons of water. The last line of output for each puzzle should be the line "success". Output lines start in column 1 and there should be no empty lines nor any trailing spaces.

    Sample Input

    3 5 4
    5 7 3
    
    

    Sample Output

    fill B
    pour B A
    empty A
    pour B A
    fill B
    pour B A
    success
    fill A
    pour A B
    fill A
    pour A B
    empty B
    pour A B
    success
    // 不小心内存开的太大出现 段错误 Segmentation Fault
    调整内存就对了
    #include <iostream>
    #include <algorithm>
    
    using namespace std ; 
    
    #define maxn 1000
    #define inf 99999999
    int a , b , n ; 
    int path[maxn] , result[maxn] ; 
    bool visit[maxn][maxn] , flag;  
    int re_step ; 
    
    void DFS(int ca , int cb ,int step){
        if(step >= re_step){
            return;
        }
        if(cb == n){
            if(step < re_step){
                flag = true ; 
                re_step = step ; 
                for(int i=0 ; i<re_step ; i++){
                    result[i] = path[i] ; 
                }
            }
            return;
        }
    
        // fill A 
        if(ca < a){
            if(visit[a][cb] == false){
                visit[a][cb] = true ; 
                path[step] = 1 ; 
                DFS(a , cb , step+1) ; 
                visit[a][cb] = false ; 
            }
            
        }
        // fill B
        if(cb < b){
            if(visit[ca][b] == false){
                visit[ca][b] = true ; 
                path[step] = 2 ; 
                DFS(ca , b , step+1) ;
                visit[ca][b] = false ;      
            }
            
        }
        // empty A
        if(ca > 0){
            if(visit[0][cb] == false){
                visit[0][cb] = true ; 
                path[step] = 3 ; 
                DFS(0 , cb , step+1) ;
                visit[0][cb] = false ; 
            }
             
        }
        // empty B
        if(cb > 0){
            if(visit[ca][0] == false){
                visit[ca][0] = true ; 
                path[step] = 4 ; 
                DFS(ca , 0 , step+1) ;     
                visit[ca][0] = false ; 
            }
            
        }
        // pour A B
        if(ca > 0 && cb < b){
            int num = min(ca , b-cb) ; 
            if(visit[ca-num][cb+num] == false){
                visit[ca-num][cb+num] = true ; 
                path[step] = 5 ; 
                DFS(ca-num , cb+num , step+1) ; 
                visit[ca-num][cb+num] = false ; 
            }
        }
        // pour B A
        if(ca < a && cb > 0){
            int num = min(a-ca , cb) ; 
            if(visit[ca+num][cb-num] == false){
                visit[ca+num][cb-num] = true ; 
                path[step]= 6 ; 
                DFS(ca+num , cb-num , step+1) ; 
                visit[ca+num][cb-num] = false ;  
            }
        }
        return;
    }
    
    
    
    int main(){
    
        while(cin >> a >> b >> n ){
            flag = false ; 
            re_step = inf ; 
    
            DFS(0 , 0 , 0) ; 
    
            if(flag == true){
                for(int i=0 ; i<re_step ; i++){
                    if(result[i] == 1){
                        cout << "fill A" << endl ; 
                    }
                    if(result[i] == 2){
                        cout << "fill B" << endl ; 
                    }
                    if(result[i] == 3){
                        cout << "empty A" << endl ; 
                    }
                    if(result[i] == 4){
                        cout << "empty B" << endl ; 
                    }
                    if(result[i] == 5){
                        cout << "pour A B" << endl ; 
                    }
                    if(result[i] == 6){
                        cout << "pour B A" << endl ; 
                    }
                }
                cout << "success" << endl ; 
            }else{
                cout << "No" << endl ; 
            }
        }
        return 0 ; 
    }


  • 相关阅读:
    Smobiler如何实现.net一键开发,ios和android跨平台运行
    使用Smobiler实现类似美团的界面
    疫情当下,企业系统如何快速实现移动化?
    Smobiler针对百度文字识别SDK动态编译与运行
    smobiler自适应不同手机分辨率
    仓库管理移动应用解决方案——C#开发的移动应用开源解决方案
    移动OA办公——Smobiler第一个开源应用解决方案,快来get吧
    react-native 标题随页面滚动显示和隐藏
    react-native 键盘遮挡输入框
    解决adb网络连接中出现的“由于目标计算机积极拒绝,无法连接”错误
  • 原文地址:https://www.cnblogs.com/yi-ye-zhi-qiu/p/9141826.html
Copyright © 2011-2022 走看看