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

Northeastern Europe 2002, Western Subregion

#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");  
}  


全部评论

相关推荐

Hakasee:我的简历和你的基本一样,上周去了上海,boss投了三百家, 三家线下面试 第一家没有做题,全是八股和项目,因为第一次面试不怎么熟练,挂了 第二家,给你几个题目(①css垂直居中文字,字体每两秒闪烁一下以及点击弹窗,②给你一个链接,实现可视化地图,③然后是八股,图片性能优化,以及对图片app有什么想法),45分钟内做完,然后老板面试) 第三家特别偏僻,有点阴森,到了之后让了一个工位给我,有四个题目,①格式化时间 年月日当前时间星期几② 正则表达式提取新闻文字,③在文本域输入文字生成选择题以及选项④生成商品排版还是什么来着 三家都是不超过50人的小公司 两家线上牛客笔试(卡伦特,七牛云,但是笔试不仅要考前端,还要考后端,算法,甚至数学题 我的建议是如果只做了这两个vue项目且不怎么熟练的情况下,先沉淀沉淀,把react学了,上海好的公司基本都是react查看图片
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务