POJ 3414 POTS BFS
2015-09-15 11:52
281 查看
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.
InputOn 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).
OutputThe 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 Input3 5 4
Sample Output6
FILL(2)
POUR(2,1)
DROP(1)
POUR(2,1)
FILL(2)
POUR(2,1)
题意:就是给你2个瓶子要得到给定的升水,有六个操作:
1.充满瓶1;2.充满瓶2;3. 把瓶1 的水倒入瓶2;4把瓶2的水倒入瓶1;5清空瓶1 ;6清空瓶2
思路:刚开始以为每次都有六种操作,就直接让这种状态进入队列,然后提交显示内存超限了,又改了一下以为是有些操作没有成功进入队列,譬如1瓶的水为0 ,那他就没必要在进行清空之类的操作了,加上之后还是内存超限,后来觉得如果能够标记掉之前出现过的状态就好了,所以用2维数组标记之前出现过的状态。。
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
int a,b,c;
struct point {
int va;
int vb;
int step;
int op[1005];
}p;
int visit[105][105];
int bfs(){
queue <point> q;
p.va = 0;
p.vb = 0;
p.step = 0;
p.op[p.step] = 0;
visit[p.va][p.vb] = 1;
q.push (p);
point k;
while(!q.empty()){
p = q.front();
q.pop();
if(p.va == c || p.vb == c){
printf("%d\n",p.step);
for(int j = 1; j <= p.step ;++j){
switch(p.op[j]){
case 1:printf("FILL(1)\n");break;
case 2:printf("FILL(2)\n");break;
case 3:printf("POUR(1,2)\n");break;
case 4:printf("POUR(2,1)\n");break;
case 5:printf("DROP(1)\n");break;
case 6:printf("DROP(2)\n");break;
}
}
return 1;
}
for(int i = 1; i < 7 ; ++i){//六种操作
k = p;
int sum;
switch(i){
case 1:
k.va = a;
k.step += 1 ;
k.op[k.step] = 1 ;
break;
case 2:
k.vb = b;
k.step += 1 ;
k.op[k.step] = 2 ;
break;
case 3:
sum = k.va + k.vb ;
if(sum <= b){
k.vb = sum;
k.va = 0;
}
else{
k.va = sum - b;
k.vb = b;
}
k.step += 1;
k.op[k.step] = 3 ;
break;
case 4:
sum = k.va + k.vb ;
if(sum <= a){
k.va = sum;
k.vb = 0;
}
else{
k.vb = sum - a;
k.va = a;
}
k.step += 1;
k.op[k.step] = 4 ;
break;
case 5:
k.va = 0;
k.step += 1 ;
k.op[k.step] = 5 ;
break;
case 6:
k.vb = 0;
k.step += 1 ;
k.op[k.step] = 6 ;
break;
}
if(visit[k.va][k.vb] == 0){
q.push (k);
visit[k.va][k.vb] = 1;
}
}
}
return 0;
}
int main(){
while(~scanf("%d%d%d",&a,&b,&c)){
memset(visit,0,sizeof(visit));
if(!bfs()){
printf("impossible\n");
}
}
return 0;
}
这题还是值得注意的,菜鸟不太会bfs,继续学好bfs,fighting。。。。。。。
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.
InputOn 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).
OutputThe 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 Input3 5 4
Sample Output6
FILL(2)
POUR(2,1)
DROP(1)
POUR(2,1)
FILL(2)
POUR(2,1)
题意:就是给你2个瓶子要得到给定的升水,有六个操作:
1.充满瓶1;2.充满瓶2;3. 把瓶1 的水倒入瓶2;4把瓶2的水倒入瓶1;5清空瓶1 ;6清空瓶2
思路:刚开始以为每次都有六种操作,就直接让这种状态进入队列,然后提交显示内存超限了,又改了一下以为是有些操作没有成功进入队列,譬如1瓶的水为0 ,那他就没必要在进行清空之类的操作了,加上之后还是内存超限,后来觉得如果能够标记掉之前出现过的状态就好了,所以用2维数组标记之前出现过的状态。。
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
int a,b,c;
struct point {
int va;
int vb;
int step;
int op[1005];
}p;
int visit[105][105];
int bfs(){
queue <point> q;
p.va = 0;
p.vb = 0;
p.step = 0;
p.op[p.step] = 0;
visit[p.va][p.vb] = 1;
q.push (p);
point k;
while(!q.empty()){
p = q.front();
q.pop();
if(p.va == c || p.vb == c){
printf("%d\n",p.step);
for(int j = 1; j <= p.step ;++j){
switch(p.op[j]){
case 1:printf("FILL(1)\n");break;
case 2:printf("FILL(2)\n");break;
case 3:printf("POUR(1,2)\n");break;
case 4:printf("POUR(2,1)\n");break;
case 5:printf("DROP(1)\n");break;
case 6:printf("DROP(2)\n");break;
}
}
return 1;
}
for(int i = 1; i < 7 ; ++i){//六种操作
k = p;
int sum;
switch(i){
case 1:
k.va = a;
k.step += 1 ;
k.op[k.step] = 1 ;
break;
case 2:
k.vb = b;
k.step += 1 ;
k.op[k.step] = 2 ;
break;
case 3:
sum = k.va + k.vb ;
if(sum <= b){
k.vb = sum;
k.va = 0;
}
else{
k.va = sum - b;
k.vb = b;
}
k.step += 1;
k.op[k.step] = 3 ;
break;
case 4:
sum = k.va + k.vb ;
if(sum <= a){
k.va = sum;
k.vb = 0;
}
else{
k.vb = sum - a;
k.va = a;
}
k.step += 1;
k.op[k.step] = 4 ;
break;
case 5:
k.va = 0;
k.step += 1 ;
k.op[k.step] = 5 ;
break;
case 6:
k.vb = 0;
k.step += 1 ;
k.op[k.step] = 6 ;
break;
}
if(visit[k.va][k.vb] == 0){
q.push (k);
visit[k.va][k.vb] = 1;
}
}
}
return 0;
}
int main(){
while(~scanf("%d%d%d",&a,&b,&c)){
memset(visit,0,sizeof(visit));
if(!bfs()){
printf("impossible\n");
}
}
return 0;
}
这题还是值得注意的,菜鸟不太会bfs,继续学好bfs,fighting。。。。。。。
相关文章推荐
- 解决HttpServletResponse输出的中文乱码问题
- 115个Java面试题和答案——终极列表(下)
- tiny.box.show()弹出框
- jquery.validate.js校验select2解决方案,Jquery插件select2校验解决方案
- 深入理解 Win32 PE 文件格式
- 正则语法及常用正则
- 详细理解TranslateAnimation
- iOS 如何设置子视图alpha不同于父视图
- os开发之封装自定义的结构体对象,CGRect,CGSize常用结构体基本使用
- 经典书籍_框架整合
- xamarin中android环境安装步骤和ios环境部署!
- 关于MVC中View使用自定义方法
- 单点登录SSO的实现原理
- sql语句:CASE WHEN END 的用法
- 性能,不是不重要,而是,它没有可维护性重要
- 时间戳与时间相互转换(php .net精确到毫秒)
- svn 密码清除
- 使用C语言操作MySQL数据库
- 使用JQuery插件Pinto 和Lightbox 制作带网格布局的响应式(Responsible)画廊(gallery)
- 格式化操作符( % )