[poj3414]Pots
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 16925   Accepted: 7168   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

题目大意:两只杯子,分别N容量M容量,问要怎样凑出K容量

                DROP(x):倒空x杯

                FILL(x):倒满x被

                POUR(x,y):将x杯中的水倒入y

试题分析:思路很明确的一道题,直接写就行,就算锻炼代码能力了  难得1AC

代码

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<stack>
#include<vector>
#include<algorithm>
//#include<cmath> using namespace std;
const int INF = 9999999;
#define LL long long inline int read(){
int x=0,f=1;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=getchar()) x=x*10+c-'0';
return x*f;
}
int N,M,K;
struct data{
int a,b,fr,st;
int fg;
}Que[200001];
int l=1,r=1;
/*
1:Fill(1)
2:Fill(2)
3:POUR(1,2)
4:ROUP(2,1)
5:DROP(1)
6:DROP(2)
*/
bool vis[201][201];
bool alflag=false; void print(data k){
if(k.fr!=-1){
print(Que[k.fr]);
if(k.fg==1) puts("FILL(1)");
if(k.fg==2) puts("FILL(2)");
if(k.fg==3) puts("POUR(1,2)");
if(k.fg==4) puts("POUR(2,1)");
if(k.fg==5) puts("DROP(1)");
if(k.fg==6) puts("DROP(2)");
}
} void BFS(){
Que[l].a=0,Que[l].b=0,Que[l].fr=-1,Que[l].st=0;
vis[0][0]=true;
int x,y,step;
while(l<=r){
x=Que[l].a,y=Que[l].b,step=Que[l].st;
if(x!=N&&!vis[N][y]){
vis[N][y]=true;
Que[++r].fg=1;
Que[r].a=N;
Que[r].b=y;
Que[r].st=step+1;
Que[r].fr=l;
if(Que[r].a==K||Que[r].b==K){
printf("%d\n",step+1);
print(Que[r]);
alflag=true;
return ;
}
}
if(y!=M&&!vis[x][M]){
vis[x][M]=true;
Que[++r].fg=2;
Que[r].a=x;
Que[r].b=M;
Que[r].st=step+1;
Que[r].fr=l;
if(Que[r].a==K||Que[r].b==K){
printf("%d\n",step+1);
print(Que[r]);
alflag=true;
return ;
}
}
if(x&&y!=M){
int xx,yy;
if(x-(M-y)<=0) yy=y+x,xx=0;
else yy=M,xx=x-(M-y);
if(!vis[xx][yy]){
vis[xx][yy]=true;
Que[++r].fg=3;
Que[r].a=xx;
Que[r].b=yy;
Que[r].st=step+1;
Que[r].fr=l;
if(Que[r].a==K||Que[r].b==K){
printf("%d\n",step+1);
print(Que[r]);
alflag=true;
return ;
}
}
}
if(y&&x!=N){
int xx,yy;
if(y-(N-x)<=0) xx=y+x,yy=0;
else xx=N,yy=y-(N-x);
if(!vis[xx][yy]){
vis[xx][yy]=true;
Que[++r].fg=4;
Que[r].a=xx;
Que[r].b=yy;
Que[r].st=step+1;
Que[r].fr=l;
if(Que[r].a==K||Que[r].b==K){
printf("%d\n",step+1);
print(Que[r]);
alflag=true;
return ;
}
}
}
if(x&&!vis[0][y]){
vis[0][y]=true;
Que[++r].fg=5;
Que[r].a=0;
Que[r].b=y;
Que[r].st=step+1;
Que[r].fr=l;
if(Que[r].a==K||Que[r].b==K){
printf("%d\n",step+1);
print(Que[r]);
alflag=true;
return ;
}
}
if(y&&!vis[x][0]){
vis[x][0]=true;
Que[++r].fg=6;
Que[r].a=x;
Que[r].b=0;
Que[r].st=step+1;
Que[r].fr=l;
if(Que[r].a==K||Que[r].b==K){
printf("%d\n",step+1);
print(Que[r]);
alflag=true;
return ;
}
}
l++;
}
} int main(){
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
N=read(),M=read(),K=read();
BFS();
if(!alflag){
printf("impossible");
return 0;
}
return 0;
}

【BFS】Pots的更多相关文章

  1. 【bfs】抓住那头牛

    [题目] 农夫知道一头牛的位置,想要抓住它.农夫和牛都位于数轴上,农夫起始位于点N(0≤N≤100000),牛位于点K(0≤K≤100000).农夫有两种移动方式: 1.从X移动到X-1或X+1,每次 ...

  2. 【bfs】拯救少林神棍(poj1011)

    Description 乔治拿来一组等长的木棒,将它们随机地砍断,使得每一节木棍的长度都不超过50个长度单位.然后他又想把这些木棍恢复到为裁截前的状态,但忘记了初始时有多少木棒以及木棒的初始长度.请你 ...

  3. 【bfs】Knight Moves

    [题目描述] 输入nn代表有个n×nn×n的棋盘,输入开始位置的坐标和结束位置的坐标,问一个骑士朝棋盘的八个方向走马字步,从开始坐标到结束坐标可以经过多少步. [输入] 首先输入一个nn,表示测试样例 ...

  4. 【bfs】1252 走迷宫

    [题目描述] 一个迷宫由R行C列格子组成,有的格子里有障碍物,不能走:有的格子是空地,可以走. 给定一个迷宫,求从左上角走到右下角最少需要走多少步(数据保证一定能走到).只能在水平方向或垂直方向走,不 ...

  5. 【bfs】献给阿尔吉侬的花束

    [题目描述] 阿尔吉侬是一只聪明又慵懒的小白鼠,它最擅长的就是走各种各样的迷宫.今天它要挑战一个非常大的迷宫,研究员们为了鼓励阿尔吉侬尽快到达终点,就在终点放了一块阿尔吉侬最喜欢的奶酪.现在研究员们想 ...

  6. 【bfs】迷宫问题

    [题目描述] 定义一个二维数组: int maze[5][5] = { 0,1,0,0,0, 0,1,0,1,0, 0,0,0,0,0, 0,1,1,1,0, 0,0,0,1,0, }; 它表示一个迷 ...

  7. 【bfs】仙岛求药

    [题目描述] 少年李逍遥的婶婶病了,王小虎介绍他去一趟仙灵岛,向仙女姐姐要仙丹救婶婶.叛逆但孝顺的李逍遥闯进了仙灵岛,克服了千险万难来到岛的中心,发现仙药摆在了迷阵的深处.迷阵由M×N个方格组成,有的 ...

  8. 【bfs】BZOJ1102- [POI2007]山峰和山谷Grz

    最后刷个水,睡觉去.Bless All! [题目大意] 给定一个地图,为FGD想要旅行的区域,地图被分为n*n的网格,每个格子(i,j) 的高度w(i,j)是给定的.若两个格子有公共顶点,那么他们就是 ...

  9. poj3278-Catch That Cow 【bfs】

    http://poj.org/problem?id=3278 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submis ...

随机推荐

  1. 逃生(HDU4857 + 反向拓扑排序)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4857 题面是中文题面,就不解释题意了,自己点击链接去看下啦~这题排序有两个条件,一个是按给定的那个序列 ...

  2. fs.createReadStream(filepath).pipe(response);这句是什么意思?

    'use strict'; var fs = require('fs'), url = require('url'), path = require('path'), http = require(' ...

  3. Python 源码学习之内存管理 -- (转)

    Python 的内存管理架构(Objects/obmalloc.c): _____ ______ ______ ________ [ int ] [ dict ] [ list ] ... [ str ...

  4. python模块中requests参数stream

    PS:这个参数真没用过 当下载大的文件的时候,建议使用strea模式. 默认情况下是false,他会立即开始下载文件并存放到内存当中,倘若文件过大就会导致内存不足的情况. 当把get函数的stream ...

  5. centos 快捷键

    centos 快捷键大全 时间:2013-02-23 14:54来源:blog.csdn.net 举报 点击:225次 新手通常会不太习惯GNOME或KDE的界面操作,不过还好,LINUX的快捷键大多 ...

  6. Mac OSX下Appium驱动iPhone真机

    1.安装Xcode.Command Line Tools和Appium. 2.安装brew:/usr/bin/ruby -e "$(curl -fsSL https://raw.github ...

  7. .net连接sql server的几种连接字符串的写法

    .net连接sql server的几种连接字符串的写法 1, 混合验证模式登录 server=电脑名 或 电脑IP;database=数据库名;uid=数据库登录名;password=数据库登录密码 ...

  8. python安装基础

    . python安装 //先查看是否存在python的包,如果没有,那可以用yum或去python的官网安装 [root@localhost ~]# rpm -qa|grep python pytho ...

  9. 在Dubbo中使用高效的Java序列化(Kryo和FST)

    在Dubbo中使用高效的Java序列化(Kryo和FST) 作者:沈理 文档版权:Creative Commons 3.0许可证 署名-禁止演绎 完善中…… TODO 生成可点击的目录 目录 序列化漫 ...

  10. ArcGIS Server 基于Token安全验证

    写在前面:只使用token并不能起到安全验证的作用,ArcGIS Server文件夹的权限是开放的,我们不需要登录Server平台即可访问服务,所以我们应该将Token验证和文件夹的安全性结合起来使用 ...