poj 3278 BFS

Pots

Time Limit: 1000MS
Memory Limit:65536K

Total Submissions: 6087
Accepted: 2563
Special Judge

Description

You are given two pots, having the volume of A and Bliters 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 ito 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 Cliters 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: //#define DEBUG
   2: #include <iostream>
   3: #include <queue>
   4: #include <stack>
   5: #include <cstring>
   6: #include <cstdio>
   7: using namespace std;
   8: int m[110][110][4];
   9: struct node{
  10:     int x;
  11:     int y;
  12: };
  13: int a,b,c;
  14: void bfs()
  15: {
  16:     memset(m,-1,sizeof(m));
  17:     queue<node> q;
  18:     node start,temp,current;
  19:     int x,y;
  20:     bool flag = false;
  21:     start.x = 0;
  22:     start.y = 0;
  23:     q.push(start);
  24:     m[0][0][3] = 0;
  25:     while(!q.empty())
  26:     {
  27:         current = q.front();
  28:         q.pop();
  29:         if(current.x == c || current.y == c)
  30:         {   flag = true;
  31:             break;
  32:         }
  33:         if(m[a][current.y][3] == -1)
  34:         {
  35:             m[a][current.y][0] = current.x;
  36:             m[a][current.y][1] = current.y;
  37:             m[a][current.y][2] = 1;
  38:             m[a][current.y][3] = m[current.x][current.y][3] + 1;
  39:             temp.x = a;
  40:             temp.y = current.y;
  41:             q.push(temp);
  42:         }
  43:         if(m[current.x][b][3] == -1)
  44:         {
  45:             m[current.x][b][0] = current.x;
  46:             m[current.x][b][1] = current.y;
  47:             m[current.x][b][2] = 2;
  48:             m[current.x][b][3] = m[current.x][current.y][3] + 1;
  49:             temp.x = current.x;
  50:             temp.y = b;
  51:             q.push(temp);
  52:         }
  53:         if(m[0][current.y][3] == -1)
  54:         {
  55:             m[0][current.y][0] = current.x;
  56:             m[0][current.y][1] = current.y;
  57:             m[0][current.y][2] = 3;
  58:             m[0][current.y][3] = m[current.x][current.y][3] + 1;
  59:             temp.x = 0;
  60:             temp.y = current.y;
  61:             q.push(temp);
  62:         }
  63:         if(m[current.x][0][3] == -1)
  64:         {
  65:             m[current.x][0][0] = current.x;
  66:             m[current.x][0][1] = current.y;
  67:             m[current.x][0][2] = 4;
  68:             m[current.x][0][3] = m[current.x][current.y][3] + 1;
  69:             temp.x = current.x;
  70:             temp.y = 0;
  71:             q.push(temp);
  72:         }
  73:         if(current.x+current.y<=b)
  74:         {temp.x = 0; temp.y = current.x+current.y;}
  75:         else {temp.x = current.x+current.y-b; temp.y = b;}
  76:         if(m[temp.x][temp.y][3] == -1)
  77:         {
  78:             m[temp.x][temp.y][0] = current.x;
  79:             m[temp.x][temp.y][1] = current.y;
  80:             m[temp.x][temp.y][2] = 5;
  81:             m[temp.x][temp.y][3] = m[current.x][current.y][3] + 1;
  82:             q.push(temp);
  83:         }
  84:         if(current.x+current.y<=a)
  85:         {temp.x = current.x+current.y; temp.y = 0;}
  86:         else {temp.x = a;temp.y = current.x+current.y-a;}
  87:         if(m[temp.x][temp.y][3] == -1)
  88:         {
  89:             m[temp.x][temp.y][0] = current.x;
  90:             m[temp.x][temp.y][1] = current.y;
  91:             m[temp.x][temp.y][2] = 6;
  92:             m[temp.x][temp.y][3] = m[current.x][current.y][3] + 1;
  93:             q.push(temp);
  94:         }
  95:     }
  96:     if(flag == false) printf("impossible\n");
  97:     else{
  98:     printf("%d\n",m[current.x][current.y][3]);
  99:     stack<int> s;
 100:     int tempx,tempy;
 101:     while(current.x != -1 && current.y != -1)
 102:     {
 103:         s.push(m[current.x][current.y][2]);
 104:         tempx= m[current.x][current.y][0];
 105:         tempy = m[current.x][current.y][1];
 106:         current.x = tempx;
 107:         current.y = tempy;
 108:     }
 109:     while(!s.empty())
 110:     {
 111:         int top = s.top();
 112:         switch(top)
 113:         {
 114:         case 1:    printf("FILL(1)\n");
 115:                 break;
 116:         case 2: printf("FILL(2)\n");
 117:                 break;
 118:         case 3: printf("DROP(1)\n");
 119:                 break;
 120:         case 4: printf("DROP(2)\n");
 121:                 break;
 122:         case 5: printf("POUR(1,2)\n");
 123:                 break;
 124:         case 6: printf("POUR(2,1)\n");
 125:                 break;
 126:         default:break;
 127:         }
 128:         s.pop();
 129:     }
 130:     }
 131: }
 132: int main()
 133: {
 134: #ifdef DEBUG
 135:     //freopen("random.txt","r",stdin);
 136:     //freopen("result.txt","w",stdout);
 137: #endif
 138:     scanf("%d%d%d",&a,&b,&c);
 139:     bfs();
 140: #ifdef DEBUG
 141:     int aa,bb;
 142:     while(cin>>aa>>bb)
 143:     cout<<m[aa][bb][0]<<' '<<m[aa][bb][1]<<' '<<m[aa][bb][2]<<' '<<m[aa][bb][3]<<endl;
 144: #endif
 145:     return 0;
 146: }
posted on 2012-02-13 09:03  bovine  阅读(219)  评论(0编辑  收藏  举报