1. 程式人生 > >POJ 3414 Pots (BFS/DFS)

POJ 3414 Pots (BFS/DFS)

Pots
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7783   Accepted: 3261   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 ≤ ≤ 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 AB, 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)

Source

Northeastern Europe 2002, Western Subregion    

問題連結:POJ3414 Pots。

題意簡述:

  給出兩個壺的容量A和B, 一個目標水量C,對A、B可以有3種操作,求最少經過幾步操作能夠在某個壺中得到目標水量C。輸入A、B和C,輸入最少運算元和操作過程。

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 potj is full (and there may be some water left in the poti), or the poti is empty (and all its contents have been moved to the potj).
問題分析:

  把A和B壺中水量作為狀態,初始狀態為<0,0>,每個操作都是狀態變化的過程。因為有2個壺,所以總共有6種操作。使用BFS搜尋來找到最少的操作步數。同時需要考慮操作的條件,以減少操作來加快程式執行速度。

程式說明:

  搜尋過的狀態就不需要再搜尋了,用陣列notvist[][]來標記搜尋過的狀態。操作的前提條件已經寫在程式中。


BFS實現程式碼

#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>

using namespace std;

const int maxn=110;

string op[7]={"","FILL(1)","FILL(2)","DROP(1)","DROP(2)","POUR(2,1)","POUR(1,2)"};

int l,r;
int a,b,c;
int vis[maxn][maxn],step[maxn*maxn];

struct node{
    int x,y;
    int opr;
    int pre;
}info[maxn*maxn];

void Solve(int x,int y,int opr){
    if(vis[x][y])
        return ;
    vis[x][y]=1;
    info[r].x=x;
    info[r].y=y;
    info[r].opr=opr;
    info[r].pre=l;
    r++;
}

void Print(){
    int ans=0;
    while(l!=0){
        step[ans++]=info[l].opr;
        l=info[l].pre;
    }
    printf("%d\n",ans);
    for(int i=ans-1;i>=0;i--)
        cout<<op[step[i]]<<endl;
}

void BFS(){
    info[0].x=0;
    info[0].y=0;
    vis[0][0]=1;
    l=0;
    r=1;
    int tx,ty;
    while(l!=r){
        if(info[l].x==c || info[l].y==c){
            Print();
            return ;
        }

        tx=a;
        ty=info[l].y;
        Solve(tx,ty,1);

        tx=info[l].x;
        ty=b;
        Solve(tx,ty,2);

        tx=0;
        ty=info[l].y;
        Solve(tx,ty,3);

        tx=info[l].x;
        ty=0;
        Solve(tx,ty,4);

        tx=info[l].x+min(a-info[l].x,info[l].y);
        ty=info[l].y-min(a-info[l].x,info[l].y);
        Solve(tx,ty,5);

        tx=info[l].x-min(b-info[l].y,info[l].x);
        ty=info[l].y+min(b-info[l].y,info[l].x);
        Solve(tx,ty,6);

        l++;
    }
    if(l>=r)
        printf("impossible\n");
}

int main(){

    //freopen("input.txt","r",stdin);

    while(~scanf("%d%d%d",&a,&b,&c)){
        memset(vis,0,sizeof(vis));
        BFS();
    }
    return 0;
}
View Code

 

DFS實現程式碼

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <algorithm>
#include <string.h>
using namespace std;

int a, b, c;
int va, vb;

const int MAX = 999999;
int ans = MAX;

int arr[100000] = {0};
int arr1[100000] = {0};
int k = 0;

int m[200][200] = {0};

// 1:fill(1) 2: drop(1) 3: pour(1,2) 4: fill(2) 5: drop(2) 6: pour(2,1);

int dfs(int a, int b, int cnt)
{
    if(m[a][b] )
    {
        return 0;
    }
    else
    {
        m[a][b] = 1;
    }
    if(a == c || b == c)
    {
        if(ans >= cnt)
        {
            ans = cnt;
            for(int i = 0; i < cnt; i++)
            {
                arr1[i] = arr[i];
            }
            m[a][b] = 0;
            return 0;
        }
    }

    arr[cnt] = 1;
    dfs(va, b, cnt + 1);
    
    arr[cnt] = 2;
    //printf("cnt = %d\n", cnt);
    dfs(0, b, cnt + 1);
    
    arr[cnt] = 3;
    if(vb - b < a)
    {
        dfs(a - vb + b, vb, cnt + 1);
    }
    else
    {
        dfs(0, b + a, cnt + 1);
    }
    
    arr[cnt] = 4;
    dfs(a, vb, cnt + 1);
    
    arr[cnt] = 5;
    dfs(a, 0, cnt + 1);
    
    arr[cnt] = 6;
    if(va - a < b)
    {
        dfs(va, b - va + a, cnt + 1);
    }
    else
    {
        dfs(a + b, 0, cnt + 1);
    }

    m[a][b] = 0;
}

int main()
{
    scanf("%d%d%d", &va, &vb, &c);
    a = 0;
    b = 0;
    dfs(0, 0, 0);

    if(ans == MAX)
    {
        printf("impossible\n");
    }
    else
    {
        printf("%d\n", ans);
        
        for(int i = 0; i < ans; i++)
        {
            if(arr1[i] == 1)
            {
                printf("FILL(1)\n");
            }
            
            else if(arr1[i] == 2)
            {
                printf("DROP(1)\n");
            }
            
            else if(arr1[i] == 3)
            {
                printf("POUR(1,2)\n");
            }
            
            else if(arr1[i] == 4)
            {
                printf("FILL(2)\n");
            }
            
            else if(arr1[i] == 5)
            {
                printf("DROP(2)\n");
            }
            
            else
            {
                printf("POUR(2,1)\n");
            }
        }
    }

    return 0;
}
View Code