【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;
}
最新文章
- java分享第十七天-02(封装操作excel类)
- mysql实现分组和组内序号
- C语言的基本输入与输出函数(全解)
- PHP面向对象编程之深入理解方法重载与方法覆盖(多态)
- Linux/Ubuntu常用快捷键
- C++之编码问题(Unicode,ASCII,本地默认)
- OOP 概述
- 关于viewWithTag的一点说明
- List列表 OrderBy
- maven 教程一 入门
- Maven工具的介绍,配置及使用
- 为table元素添加操作日志
- github及git使用
- Spring03-AOP
- 如何使用 Deepfakes 换脸
- WPF工具开发: 第三库选择
- api文档生成器apidoc的安装和使用
- XPath高级用法(冰山一角)
- FZU Problem 2030 括号问题
- WebStorm配置Node.js IDE