1. 程式人生 > >poj 3414 搜尋

poj 3414 搜尋

Pots
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 8211 Accepted: 3481 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)
這是一道廣搜的題目,下面是程式碼:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>

using namespace std;

const int maxn=101;
int vis[maxn][maxn],q[maxn*maxn*maxn][5];
int A,B,C,fa;

int bfs(){
    int front=0,rear=0,a,b,na,nb,nfront,ans;
    q[rear][0]=0,q[rear][1]=0,q[rear][2]=0,q[rear++][4]=0;
    vis[0][0]=1;
    while(rear>front){
         a=q[front][0],b=q[front][1],ans=q[front][4];
         nfront=front,front++;
         if(a==C||b==C){//到達目標
              fa=nfront;
              return 1;
         }
         na=A,nb=b;
         if(!vis[na][nb]){//fill 1
             vis[na][nb]=1;
             q[rear][0]=na,q[rear][1]=nb;
             q[rear][2]=1,q[rear][3]=nfront;
             q[rear++][4]=ans+1;
         }
         na=a,nb=B;
          if(!vis[na][nb]){//fill 2
             vis[na][nb]=1;
             q[rear][0]=na,q[rear][1]=nb;
             q[rear][2]=2,q[rear][3]=nfront;
             q[rear++][4]=ans+1;
         }
         na=0,nb=b;
         if(!vis[na][nb]){//drop 1
             vis[na][nb]=1;
             q[rear][0]=na,q[rear][1]=nb;
             q[rear][2]=3,q[rear][3]=nfront;
             q[rear++][4]=ans+1;
         }
         na=a,nb=0;
         if(!vis[na][nb]){//drop 2
             vis[na][nb]=1;
             q[rear][0]=na,q[rear][1]=nb;
             q[rear][2]=4,q[rear][3]=nfront;
             q[rear++][4]=ans+1;
         }
         int full;
         full=(a+b)/B;
         full?(na=a+b-B,nb=B):(na=0,nb=a+b);
          if(!vis[na][nb]){//pour 1 to 2 
             vis[na][nb]=1;
             q[rear][0]=na,q[rear][1]=nb;
             q[rear][2]=5,q[rear][3]=nfront;
             q[rear++][4]=ans+1;
         }
         full=(a+b)/A;
         full?(na=A,nb=a+b-A):(na=a+b,nb=0);
          if(!vis[na][nb]){//pour 2 to 1
             vis[na][nb]=1;
             q[rear][0]=na,q[rear][1]=nb;
             q[rear][2]=6,q[rear][3]=nfront;
             q[rear++][4]=ans+1;
         }
    }
    return 0;
}

void print(int fa){//列印路徑
     if(fa==0) return;
     print(q[fa][3]);
     if(q[fa][2]<=2)
        printf("FILL(%d)\n",q[fa][2]);
     else if(q[fa][2]<=4)printf("DROP(%d)\n",q[fa][2]/2);
     else {
        if(q[fa][2]==5)printf("POUR(1,2)\n");
        else printf("POUR(2,1)\n");
     }
     return;
}

int main(){
    int ok;
    while(scanf("%d%d%d",&A,&B,&C)!=EOF){
             memset(vis,0,sizeof(vis));
             ok=bfs();
             if(ok){
                printf("%d\n",q[fa][4]);
                print(fa);
             }
             else
                printf("impossible\n");
     }
     return 0 ;
}