[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. Creating a new dynamic form project, business modeling.

    The domain logic is like there are a bunch of objects, as well as a lot of configurations, according ...

  2. mybatis 插入语句name no find

    1.可参考连接:https://www.cnblogs.com/thomas12112406/p/6217211.html 2.dao层的配置 void addUser(@Param("un ...

  3. JavaScript字符串逆序

    如何对字符串进行倒序呢?你首先想到的方法就是生成一个栈,从尾到头依次取出字符串中的字符压入栈中,然后把栈连接成字符串. var reverse = function( str ){ var stack ...

  4. 5.0docer 网络链接

    docker0 :linux的虚拟网桥 虚拟网桥特点: 1.可以设置ip地址 2.相当于拥一个隐藏的虚拟网卡     安装网桥工具 apt-get install bridge-utils brctl ...

  5. Coursera在线学习---第八节.K-means聚类算法与主成分分析(PCA)

    一.K-means聚类中心初始化问题. 1)随机初始化各个簇类的中心,进行迭代,直到收敛,并计算代价函数J. 如果k=2~10,可以进行上述步骤100次,并分别计算代价函数J,选取J值最小的一种聚类情 ...

  6. inet_select_addr

    当通过输出设备向目的地址发送报文时,如果没有源地址,则需要调用inet_select_addr来选择ip地址作为源地址: /* 选择ip地址 通过设备找到ip控制块,从ip控制块中遍历地址列表中的主地 ...

  7. sicily 数据结构 1014. Translation

    Description You have just moved from Waterloo to a big city. The people here speak an incomprehensib ...

  8. HDU 5118 GRE Words Once More!

    题目链接:HDU-5118 题意:给定一个有向无环图,每条边有一个权值.标定一些特定节点为“特殊节点”.从节点1出发到某“特殊节点”结束的路径,称为一个“GRE单词”.单词由路径上的权值组成.给定一组 ...

  9. django给视图添加缓存功能

    在开发过程中,有些视图只是查询数据,而且查询的数据一般不会变化.例如,做地址模块时,省市县都是不会变的.如果用户每次请求地址视图时,都要执行视图返回数据,会给服务端带来不必要的压力.这时候可以用到缓存 ...

  10. How ConcurrentHashMap offers higher concurrency without compromising thread safety

    https://www.ibm.com/developerworks/library/j-jtp08223/