Pots
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)

分析:bfs加状态记录.水题

code:

#include <cstdio>
#include <queue>
#include <cstdlib>
#include <cstring>
#include <iostream>using namespace std;
typedef long long ll;
struct dat{int a, b;int choice, c, d;struct dat *front;
};
dat *star;
int vis[105][105], flag, goal, cupa, cupb;void prin(dat *p,int ans)
{if (p->front == NULL){cout << ans << endl;return ;}if(p->front!=NULL){prin(p->front,ans+1);}switch (p->choice){case 0:{printf("FILL(%d)\n", p->c);break;}case 1:{printf("POUR(%d,%d)\n", p->c, p->d);break;}case 2:{printf("DROP(%d)\n", p->c);break;}}
}dat *bfs()
{star = (dat*) malloc (sizeof(dat));queue <dat*> q;star->front = NULL;star->choice = -1;star->a = 0;star->b = 0;star->c=0,star->d=0;q.push(star);while (q.size()){dat *now = q.front();//  prin(now,1);//cout<<now->a<<' '<<now->b<<endl;q.pop();if (now->a == goal || now->b == goal){flag = 1;return now;}dat *nex;nex=(dat*) malloc (sizeof(dat));//nex=now;for (int i = 0; i < 6; i++){nex=(dat*) malloc (sizeof(dat));nex->a=now->a,nex->b=now->b,nex->choice=now->choice,nex->c=now->c,nex->d=now->d;switch (i){case 0:{nex->a = cupa;nex->choice = i/2;nex->c = 1;nex->front=now;if (vis[nex->a][nex->b] == 0){//  cout<<nex->a<<' '<<nex->b<<endl;q.push(nex);vis[nex->a][nex->b] = 1;}break;}case 1:{//nex = now;nex->b = cupb;nex->choice = i / 2;nex->c = 2;nex->front=now;if (vis[nex->a][nex->b] == 0){//cout<<nex->a<<' '<<nex->b<<endl;q.push(nex);vis[nex->a][nex->b] = 1;}break;}case 2:{if(now->choice==-1) break;if (now->a >= cupb - now->b){nex->b = cupb;nex->a = now->a - (cupb - now->b);nex -> choice = i / 2;nex->c = 1;nex->d = 2;nex->front=now;if (vis[nex->a][nex->b] == 0){// cout<<nex->a<<' '<<nex->b<<endl;q.push(nex);vis[nex->a][nex->b] = 1;}break;}else{nex->a = 0;nex->b = now->a + now->b;nex->choice = i / 2;nex->c = 1;nex->d = 2;nex->front=now;if (vis[nex->a][nex->b] == 0){//  cout<<nex->a<<' '<<nex->b<<endl;q.push(nex);vis[nex->a][nex->b] = 1;}break;}}case 3:{if(now->choice==-1) break;if (now->b >= cupa - now->a){nex->a = cupa;nex->b = now->b - (cupa - now->a);nex->choice = i / 2;nex->c = 2;nex->d = 1;nex->front=now;if (vis[nex->a][nex->b] == 0){//  cout<<nex->a<<' '<<nex->b<<endl;q.push(nex);vis[nex->a][nex->b] = 1;}break;}else{nex->b = 0;nex->a = now->a + now->b;nex->choice = i / 2;nex->c = 2;nex->d = 1;nex->front=now;if (vis[nex->a][nex->b] == 0){//  cout<<nex->a<<' '<<nex->b<<endl;q.push(nex);vis[nex->a][nex->b] = 1;}break;}}case 4:{if(now->choice==-1) break;nex->choice = i / 2;nex->a = 0;nex->c = 1;nex->front=now;if (vis[nex->a][nex->b] == 0){//  cout<<nex->a<<' '<<nex->b<<endl;q.push(nex);vis[nex->a][nex->b] = 1;}break;}case 5:{if(now->choice==-1) break;nex->choice = i / 2;nex->b = 0;nex->c = 2;nex->front=now;if (vis[nex->a][nex->b] == 0){// cout<<nex->a<<' '<<nex->b<<endl;q.push(nex);vis[nex->a][nex->b] = 1;}break;}}}}return NULL;
}void solve()
{memset(vis, 0, sizeof vis);flag = 0;dat *p = bfs();if (flag)prin(p,0);elsecout << "impossible" << endl;
}int main(void)
{while (cin >> cupa >> cupb >> goal){solve();}
}

[kuangbin带你飞]专题一 简单搜索 - H - Pots相关推荐

  1. poj 3728 Catch That Cow ([kuangbin带你飞]专题一 简单搜索)

    题目大意:题目链接 就是给你N,K,每次有三种惭怍+1,-1,*2,,问多少次操作能到K 解题思路,搜索直接算,.,,,哎,啥时候这种垃圾搜索我能直接A 啊,太菜了 #include<cstdi ...

  2. [kuangbin带你飞]专题1 简单搜索 J - Fire! UVA - 11624

    题目: Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of t ...

  3. [kuangbin带你飞]专题一 简单搜索D - Fliptile(POJ 3279)

    题目大意 给一个N行M列的矩阵,值分别为0和1,每次你可以选择将一个变成相反状态,同时,它周围的四个数也会变为相反状态. 问:最少翻转多少次,可以将所有值都变成0 多个解,输出翻转次数最少的(若有次数 ...

  4. Catch That Cow POJ - 3278 [kuangbin带你飞]专题一 简单搜索

    Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. ...

  5. kuangbin带你飞专题合集

    题目列表 [kuangbin带你飞]专题一 简单搜索 [kuangbin带你飞]专题二 搜索进阶 [kuangbin带你飞]专题三 Dancing Links [kuangbin带你飞]专题四 最短路 ...

  6. “kuangbin带你飞”专题计划——专题十四:数论基础

    写在前面 1.目前还没啥写的.开始时间:2021-05-13(其实博客上看得到该博客创建时间的) 2.上一个专题刷的是网络流(博客总结),属于第一次接触.本来想的是一周特别高效,然后一周略划水,结果是 ...

  7. (2021-07-14~)“kuangbin带你飞”专题计划——专题十三:基础计算几何

    目录 前言 参考博客 自己总结的东西: 难度判断? 题目 1.[TOYS POJ - 2318 ](解决) 2.[Toy Storage POJ - 2398 ](解决) 3.[Segments PO ...

  8. [kuangbin带你飞]专题十二 基础DP1 题解+总结

    kuangbin带你飞:点击进入新世界 总结: 简单dp,最近在做,持续更新. 文章目录 总结: 1.Max Sum Plus Plus 2.Ignatius and the Princess IV ...

  9. kuangbin带你飞 专题1-23 题单

    kuangbin大神,对于打过ACM比赛的ACMer,无人不知无人不晓. 在此,附上vjudge平台上一位大神整理的[kuangbin带你飞]专题目录链接. [kuangbin带你飞专题目录1-23] ...

  10. [kuangbin带你飞]专题1

    专题一 简单搜索 POJ 1321 棋盘问题 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大 ...

最新文章

  1. 程序员转型AI,成功几率有几分?
  2. 信息系统管理十大知识领域
  3. 情人节,我表白了CSDN小姐姐后,竟然...【为表白写了一个绘图工具,让我不再手残】
  4. javaweb笔记1
  5. debian查看ip地址命令_鲜为人知而又实用的 Linux 命令大全
  6. DG Lecture 2 part 2: points, vectors, directional derivative
  7. python遗传算法工具箱的使用_Python遗传算法框架——Geatpy学习笔记(一)
  8. mySQL里有2000w数据,redis中只存20w的数据,如何保证redis中的数据都是热点数据
  9. python和javaweb开发哪个更火_python和Java哪个更适合web开发?
  10. 小米和美的互投,大明湖畔的董明珠怎么办?
  11. 微信支付V3版本的 签名生成,验签,解密,统一下单的简单封装
  12. 孙悟空吃蟠桃c语言编程,孙悟空吃光蟠桃是爱国行为
  13. 今天公开猎头顾问业绩过百万的秘密,谷露猎头系统3.0版谍报速递
  14. 3D_systems_touch控制Dobot魔术师
  15. Vue.js 事件处理器,医院的胸牌佩戴在哪,给医生护士佩戴赞赏胸牌,对服务质量打分。
  16. 软件外包网站TOP5
  17. 购买域名之后要怎么用
  18. 电影 -- 小黄狗的窝
  19. IBM小型机+Oracle数据库+EMC存储设备,IOE简介
  20. 九月十月百度人搜,阿里巴巴,腾讯华为小米搜狗笔试面试八十题(10.14)

热门文章

  1. Adobe Acrobat Reader离线安装包下载
  2. 2009年具有中国特色的脑筋急转弯
  3. 阿里云OS和Android之争100问
  4. 《power BI 视频7》六合一图表案例
  5. Redis安装教程(保姆级详细图文)
  6. 实验前准备:CPU学习实验的头文件.vh
  7. c语言实验报告约瑟夫环,C语言约瑟夫环的实现
  8. android 下载服务器的txt文档
  9. 如何使用简单的接入点扩展您的Wi-Fi网络
  10. linux运行Windows模拟器,如何安装和使用Wine,以便在Linux上运行Windows应用程序?...