Pots(bfs)
| Time Limit: 1000MS | Memory Limit: 65536K | |||
| Total Submissions: 8266 | Accepted: 3507 | 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) 题意:给定三个数A B C,前两个数代表容量为A和B的容器,有三种操作,FILL,DROP,POUR;求最少经过几次这样的操作可以得到容量为C的水; 思路:因为每个容器都有FILL,DROP,POUR三种操作,将两个容器的状态用队列来维护,两个容器共有六种操作的可能;
另一个关键是打印路径,可以用一个结构体数组保存前驱,通过栈再将路径打印出来;
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
#include<stack>
//各种状态
#define FILL_A 1;
#define FILL_B 2;
#define DROP_A 3;
#define DROP_B 4;
#define POURA_B 5;
#define POURB_A 6;
using namespace std; struct node
{
int x,y;
int step;
};
queue<struct node>que; struct Path
{
int x,y;
int way;
}path[][];//保存前驱; int vis[][];
int a,b,c; //打印路径
void Print_Path(int x, int y)
{
stack<struct Path>st;
while(!st.empty())
st.pop(); while(!(x == && y == ))
{
st.push(path[x][y]);
int sx = path[x][y].x;
int sy = path[x][y].y;
x = sx;
y = sy;
} while(!st.empty())
{
switch(st.top().way)
{
case :printf("FILL(1)\n");break;
case :printf("FILL(2)\n");break;
case :printf("DROP(1)\n");break;
case :printf("DROP(2)\n");break;
case :printf("POUR(1,2)\n");break;
case :printf("POUR(2,1)\n");break;
}
st.pop();
}
} void bfs()
{
while(!que.empty())
que.pop();
que.push((struct node){,,});//初始状态进队列
vis[][] = ;
while(!que.empty())
{
struct node u = que.front();
que.pop();
if(u.x == c || u.y == c)
{
printf("%d\n",u.step);
Print_Path(u.x,u.y);
return;
}
//FILL_A
if(u.x < a && !vis[a][u.y])
{
vis[a][u.y] = ;
que.push((struct node){a,u.y,u.step+});
path[a][u.y] = (struct Path){u.x,u.y,};
}
//FILL_B
if(u.y < b && !vis[u.x][b])
{
vis[u.x][b] = ;
que.push((struct node){u.x,b,u.step+});
path[u.x][b] = (struct Path){u.x,u.y,};
}
//DROP_A
if(u.x > && !vis[][u.y])
{
vis[][u.y] = ;
que.push((struct node){,u.y,u.step+});
path[][u.y] = (struct Path){u.x,u.y,};
}
//DROP_B
if(u.y > && !vis[u.x][])
{
vis[u.x][] = ;
que.push((struct node){u.x,,u.step+});
path[u.x][] = (struct Path){u.x,u.y,};
}
//POURA_B
if(u.x < a && u.y > )
{
int tmp = min(a-u.x,u.y);
if(!vis[u.x+tmp][u.y-tmp])
{
vis[u.x+tmp][u.y-tmp] = ;
que.push((struct node){u.x+tmp,u.y-tmp,u.step+});
path[u.x+tmp][u.y-tmp] = (struct Path){u.x,u.y,};
}
}
//POURB_A
if(u.x > && u.y < b)
{
int tmp = min(u.x,b-u.y);
if(!vis[u.x-tmp][u.y+tmp])
{
vis[u.x-tmp][u.y+tmp] = ;
que.push((struct node){u.x-tmp,u.y+tmp,u.step+});
path[u.x-tmp][u.y+tmp] = (struct Path){u.x,u.y,};
}
}
}
printf("impossible\n");
}
int main()
{
scanf("%d %d %d",&a,&b,&c);
memset(vis,,sizeof(vis));
bfs();
return ;
}
Pots(bfs)的更多相关文章
- POJ 3414 Pots(BFS)
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Description You are g ...
- poj 3414 Pots(bfs+输出路径)
Description You are given two pots, having the volume of A and B liters respectively. The following ...
- POJ3414—Pots(bfs加回溯)
http://poj.org/problem?id=3414 Pots Time Limit: 1000MS Memor ...
- POJ - 3414 Pots BFS(著名倒水问题升级版)
Pots You are given two pots, having the volume of A and B liters respectively. The following operati ...
- POJ3414 Pots —— BFS + 模拟
题目链接:http://poj.org/problem?id=3414 Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
- POJ 3414 Pots (BFS/DFS)
Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7783 Accepted: 3261 Special Ju ...
- poj 3414 Pots bfs+模拟
#include<iostream> #include<cstring> #define fillA 1 #define pourAB 2 #define dropA 3 #d ...
- POJ 3414 Pots bfs打印方案
题目: http://poj.org/problem?id=3414 很好玩的一个题.关键是又16ms 1A了,没有debug的日子才是好日子.. #include <stdio.h> # ...
- POJ 3414 Pots ( BFS , 打印路径 )
题意: 给你两个空瓶子,只有三种操作 一.把一个瓶子灌满 二.把一个瓶子清空 三.把一个瓶子里面的水灌到另一个瓶子里面去(倒满之后要是还存在水那就依然在那个瓶子里面,或者被灌的瓶子有可能没满) 思路: ...
随机推荐
- Java theory and practice: Thread pools and work queues--reference
Why thread pools? Many server applications, such as Web servers, database servers, file servers, or ...
- WisDom.Net 框架设计(三) 数据缓存
WisDom.Net --数据缓存 1.几种缓存方式 1.静态全局变量 C#静态变量使用 static 修饰符进行声明,在类被实例化时创建,通过类进行访问不带有 static 修饰符声明 ...
- RegistryKey 类
表示 Windows 注册表中的项级节点. 此类是注册表封装. 继承层次结构 System.Object System.MarshalByRefObject Microsoft.Win32. ...
- u盘复制提示文件过大
应该有很多个朋友也遇到过同样的问题,就是我们的u盘的明明可用的空间还有很多,甚至一个空的16g的u盘,但从window等操作系统向u盘拷贝文件的时候,却不能容下诸如iso4g的镜像文件,难道是生产u盘 ...
- 新装的mysql,直接安装板
Windows安装MySQL解压版 http://www.cnblogs.com/xiaoit/p/3932241.html my文件 如下: [mysql]# 设置mysql客户端默认字符集defa ...
- NodeJS学习笔记—1.CommonJS规范
由于现在web开发,越来越重视代码的复用和抽象的封装,为了解决代码的组织结构.管理.复用和部署等问题,现在普遍采用的机制是模块机制(module).CommonJS约定桌面应用程序和服务器应用程序需要 ...
- Angularjs总结(六) 上传附件
所用插件:angular-file-upload 这个插件用到的几个指令:nv-file-select(点击选择).uploader(用于绑定控制器中新建的uploader对象) HTML: < ...
- SQL三大范式
第一范式:确保每列的原子性. 如果每列(或者每个属性)都是不可再分的最小数据单元(也称为最小的原子单元),则满足第一范式. 例如:顾客表(姓名.编号.地址.……)其中"地址"列还可 ...
- SpringMVC4+thymeleaf3的一个简单实例(篇五:页面和MySql的数据交互-展示以及存储)
这一篇将介绍怎样把页面数据保存的MySQL数据库,并将数据库内容展示到页面上.首先做一个基础工作,添加以下jar到lib:1: mysql-connector-Java-5.1.40-bin.jar ...
- Java操作hbase总结
用过以后,总得写个总结,不然,就忘喽. 一.寻找操作的jar包. java操作hbase,首先要考虑到使用hbase的jar包. 因为咱装的是CDH5,比较方便,使用SecureCRT工具,远程连接到 ...