【BFS】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:
- FILL(i) fill the pot i (1 ≤ i ≤ 2) from the tap;
- DROP(i) empty the pot i to the drain;
- 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 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)
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的更多相关文章
- 【bfs】抓住那头牛
[题目] 农夫知道一头牛的位置,想要抓住它.农夫和牛都位于数轴上,农夫起始位于点N(0≤N≤100000),牛位于点K(0≤K≤100000).农夫有两种移动方式: 1.从X移动到X-1或X+1,每次 ...
- 【bfs】拯救少林神棍(poj1011)
Description 乔治拿来一组等长的木棒,将它们随机地砍断,使得每一节木棍的长度都不超过50个长度单位.然后他又想把这些木棍恢复到为裁截前的状态,但忘记了初始时有多少木棒以及木棒的初始长度.请你 ...
- 【bfs】Knight Moves
[题目描述] 输入nn代表有个n×nn×n的棋盘,输入开始位置的坐标和结束位置的坐标,问一个骑士朝棋盘的八个方向走马字步,从开始坐标到结束坐标可以经过多少步. [输入] 首先输入一个nn,表示测试样例 ...
- 【bfs】1252 走迷宫
[题目描述] 一个迷宫由R行C列格子组成,有的格子里有障碍物,不能走:有的格子是空地,可以走. 给定一个迷宫,求从左上角走到右下角最少需要走多少步(数据保证一定能走到).只能在水平方向或垂直方向走,不 ...
- 【bfs】献给阿尔吉侬的花束
[题目描述] 阿尔吉侬是一只聪明又慵懒的小白鼠,它最擅长的就是走各种各样的迷宫.今天它要挑战一个非常大的迷宫,研究员们为了鼓励阿尔吉侬尽快到达终点,就在终点放了一块阿尔吉侬最喜欢的奶酪.现在研究员们想 ...
- 【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, }; 它表示一个迷 ...
- 【bfs】仙岛求药
[题目描述] 少年李逍遥的婶婶病了,王小虎介绍他去一趟仙灵岛,向仙女姐姐要仙丹救婶婶.叛逆但孝顺的李逍遥闯进了仙灵岛,克服了千险万难来到岛的中心,发现仙药摆在了迷阵的深处.迷阵由M×N个方格组成,有的 ...
- 【bfs】BZOJ1102- [POI2007]山峰和山谷Grz
最后刷个水,睡觉去.Bless All! [题目大意] 给定一个地图,为FGD想要旅行的区域,地图被分为n*n的网格,每个格子(i,j) 的高度w(i,j)是给定的.若两个格子有公共顶点,那么他们就是 ...
- poj3278-Catch That Cow 【bfs】
http://poj.org/problem?id=3278 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submis ...
随机推荐
- hdu1002 A + B Problem II(大数题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1002 A + B Problem II Time Limit: 2000/1000 MS (Java/ ...
- Angular2.0 基础: 环境搭建
最近在学习Angular2的使用,其实看过Angular2 文档的都知道,相比于之前的Angular1,Angular2 的改动还是挺大的. 而对于‘angular2 的本地开发环境的搭建的中,我们首 ...
- perl 在win下输出中文乱码问题
use utf8; my $name = '你好'; binmode(STDOUT, ":encoding(gbk)"); print $name,"\n"; ...
- 再思linux内核在中断路径内不能睡眠/调度的原因(2010)【转】
转自:http://blog.csdn.net/maray/article/details/5770889 Linux内核中断路径中不能睡眠,为什么? 这里就行了很深入的讨论,值得一看:http:// ...
- glob模块的使用
glob模块 功能描述:glob模块可以使用Unix shell风格的通配符匹配符合特定格式的文件和文件夹,跟windows的文件搜索功能差不多.glob模块并非调用一个子shell实现搜索功能,而是 ...
- 【bzoj4562】HAOI2016食物链
记忆化搜索水过去了…… QwQ #include<bits/stdc++.h> #define N 400010 typedef long long ll; using namespace ...
- Python简单的制作图片验证码
-人人可以学Python--这里示范的验证码都是简单的,你也可以把字符扭曲 人人可以学Python.png Python第三方库无比强大,PIL 是python的一个d第三方图片处理模块,我们也可以使 ...
- 获取windows 网卡GUID和ip信息
# coding: UTF-8 import _winreg GUID=dict() num = 0 netCfgInstanceID = None hkey = _winreg.OpenKey(_w ...
- inux权限管理(1)
1.linux系统文件普通权限 2.文件所属主的设置,组的指定 3.特殊权限 4.acl权限 5.su命令及其注意事项和sudo权限 6.权限管理的注意点 0.首先,在linux下用户账户是分角色的, ...
- HDU-1151
Air Raid Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...