1. 程式人生 > >POJ 3414 Pots(bfs)

POJ 3414 Pots(bfs)

Pots
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 17456 Accepted: 7407 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


#include <iostream>  
#include <cstring>
#include <cstdio>  
#include <algorithm>  
using namespace std;  
const int MAX = 110;  
int a, b, c, r, l, vis[MAX][MAX], step[MAX*MAX];  
string opr[7] = {" ", "FILL(1)", "FILL(2)", "DROP(1)", "DROP(2)", "POUR(2,1)", "POUR(1,2)"};  
  
struct Info  
{  
       int x;  
       int y;  
       int operation;  
       int pre;//連結串列回溯找路徑 
}info[MAX*MAX];  
  
void solve(int xx, int yy, int ss)  
{  
     if (vis[xx][yy])  
         return ;  
     vis[xx][yy] = 1;  
     info[r].x = xx;  
     info[r].y = yy;  
     info[r].operation = ss;  
     info[r].pre = l;  
     r++;  
}  
  
void print()  
{  
     int i, ans;  
     ans = 0;  
     while (l != 0){  
           step[ans++] = info[l].operation;  
           l = info[l].pre;  
     }  
       
     cout << ans << endl;  
     for (i = ans - 1; i >= 0; i--){  
         cout << opr[step[i]] << endl;  
     }  
}  
  
  
void BFS()  
{  
     info[0].x = 0;  
     info[0].y = 0;  
     vis[0][0] = 1;  
     l = 0;  
     r = 1;  
     while (l != r){     
           if (info[l].y == c || info[l].x == c){  
               print();  
               return ;  
           }  
             
           int tmpx, tmpy;  
           //fill(1)   
           tmpx = a;  
           tmpy = info[l].y;  
           solve(tmpx, tmpy, 1);  
             
           //fill(2)   
           tmpx = info[l].x;  
           tmpy = b;  
           solve(tmpx, tmpy, 2);  
             
           //drop(1);   
           tmpx = 0;  
           tmpy = info[l].y;  
           solve(tmpx, tmpy, 3);  
              
           //drop(2)   
           tmpx = info[l].x;  
           tmpy = 0;  
           solve(tmpx, tmpy, 4);  
             
           //fill(2, 1)   
           tmpx = info[l].x + min(a - info[l].x, info[l].y);  
           tmpy = info[l].y - min(a - info[l].x, info[l].y);  
           solve(tmpx, tmpy, 5);  
             
           //fill(1, 2)   
           tmpx = info[l].x - min(b - info[l].y, info[l].x);  
           tmpy = info[l].y + min(b - info[l].y, info[l].x);  
           solve(tmpx, tmpy, 6);  
             
           l++;  
     }  
     if (l >= r)  
         cout << "impossible" << endl;  
}  
  
  
int main()  
{  
    cin >> a >> b >> c;  
    memset(vis, 0, sizeof(vis));  
    BFS();  
      
    system("pause");  
}