POJ 3414 Pots 我沒有用廣搜做

Pots

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 25   Accepted Submission(s) : 11
Special Judge
Problem 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)
 
    這是一道廣搜題,可是如果用廣搜我覺得會非常麻煩,不好考慮而且代碼冗長。 
    仔細考慮問題可以發現倒水的倒法只有兩種,
                 1). 水->n;n->m;m->0;        //如此往復;
                 2). 水->m;m->n;n->0;           //如此往復;
    我們只要找出兩種倒法哪一種可以先得出我們所需要的解,即最優解即可,數據也不是很大,兩個暴力枚舉也是可以的。
    但是我們還要思考什麼樣子的數字是沒有解,如果沒有解兩個循環會被關了,及其尷尬。
    仔細思考,通過數學知識我們可以發現輸入的C不是N,M的最大公因數的整數倍的時候會無解。

   下面是AC代碼,我要早點打完回去看TI了,我再重申一次,LGD是不可戰勝的。

#include <cstdio>
#include <iostream>
#include <string>
using namespace std;
int ans1[5000],ans2[5000];
string ss[7]={"\0","FILL(1)","FILL(2)","POUR(1,2)","POUR(2,1)","DROP(1)","DROP(2)" };   //最後輸出用的
int gcd(int n,int m){                                                                   //大家熟悉的輾轉相除法
    if(m<=n){
        return m==0?n:gcd(m,n%m);
    }else{
        return gcd(m,n);
    }
}
int main()
{
    int n,m,c;
    while(~scanf("%d%d%d",&n,&m,&c))                                                    //輸入
    {
        int r1=0,r2=0,sw=0,fw=0;                                                        //sw,fw分別代表兩個杯子裏剩下的水,r1,r2是記錄步數用的
        if(c%gcd(n,m)!=0){printf("impossible\n");continue;}
        while(sw!=c&&fw!=c)
        {
            if(!sw)
            {
                sw=m;
                ans1[r1++]=2;
            }
            else if(fw==n)
            {
                fw=0;
                ans1[r1++]=5;
            }
            else
            {
                ans1[r1++]=4;
                fw+=sw;                                                                  //先把水全部倒進一個瓶子裏
                sw=0;                                                                    //因爲水倒掉了,所以歸零
                if(fw>=n){sw=fw-n;fw=n;}                                                 //把多出來的水倒回去
            }
        }
        sw=0,fw=0;
        while(sw!=c&&fw!=c)
        {
            if(!fw)
            {
                fw=n;
                ans2[r2++]=1;
            }
            else if(sw==m)
            {
                sw=0;
                ans2[r2++]=6;
            }
            else
            {
                ans2[r2++]=3;                                                               //和上面同理
                sw+=fw;
                fw=0;
                if(sw>=m){fw=sw-m;sw=m;}
            }
        }
            if(r1>r2)                                                                       //輸出函數
            {
                cout<<r2<<endl;
                for(int i=0;i<r2;i++)
                cout<<ss[ans2[i]]<<endl;
            }
            else
            {
                cout<<r1<<endl;
                for(int i=0;i<r1;i++)
                cout<<ss[ans1[i]]<<endl;
            }
    }
    return 0;
}




發佈了48 篇原創文章 · 獲贊 8 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章