POJ 3414--Pots(BFS+回溯路径)
| Time Limit: 1000MS | Memory Limit: 65536K | |||
| Total Submissions: 9963 | Accepted: 4179 | 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)
和之前做过的一道二维bfs一样。仅仅只是这个须要回溯路径。非常easy在结构体中加一个变量来记录上一个状态在队列中的下标(手敲的队列比較好。这个时候在用STL队列好像不慷慨便)最后找到满足条件的状态逆向打印路径(由于记录的都是上一个状态)
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <queue>
using namespace std;
int m,n,c,s,e,p;
typedef struct node
{
int v1,v2,cur,pre,op;
};
bool vis[999][999];
int ans[10010];
node que[10010];
void bfs()
{
p=0;s=0;e=0;int pos;
node t={0,0,0,0,0};
que[e++]=t;
vis[0][0]=1;
while(s<e)
{
node f=que[s];pos=s;s++;
if(f.v1==c||f.v2==c)
{
printf("%d\n",f.op);
int tem=pos;
for(int i=0;i<f.op;i++)
{
ans[p++]=que[tem].cur;
tem=que[tem].pre;
}
for(int i=p-1;i>=0;i--)
{
switch(ans[i])
{
case 1:printf("FILL(1)\n");break;
case 2:printf("FILL(2)\n");break;
case 3:printf("DROP(1)\n");break;
case 4:printf("DROP(2)\n");break;
case 5:printf("POUR(2,1)\n");break;
case 6:printf("POUR(1,2)\n");break;
}
}
return ;
}
if(f.v1!=m)
{
t.v1=m;
t.op=f.op+1;
t.v2=f.v2;
if(!vis[t.v1][t.v2])
{
vis[t.v1][t.v2]=1;
t.cur=1;
t.pre=pos;
que[e++]=t;
}
}
if(f.v2!=n)
{
t.v2=n;
t.op=f.op+1;
t.v1=f.v1;
if(!vis[t.v1][t.v2])
{
vis[t.v1][t.v2]=1;
t.cur=2;
t.pre=pos;
que[e++]=t;
}
}
if(f.v1!=0)
{
t.v1=0;
t.v2=f.v2;
t.op=f.op+1;
if(!vis[t.v1][t.v2])
{
vis[t.v1][t.v2]=1;
t.cur=3;
t.pre=pos;
que[e++]=t;
}
}
if(f.v2!=0)
{
t.v2=0;
t.v1=f.v1;
t.op=f.op+1;
if(!vis[t.v1][t.v2])
{
vis[t.v1][t.v2]=1;
t.cur=4;
t.pre=pos;
que[e++]=t;
}
}
if(f.v2!=0&&f.v1!=m)
{
t.v2=f.v2-(m-f.v1);if(t.v2<0) t.v2=0;
t.v1=f.v1+f.v2; if(t.v1>m) t.v1=m;
t.op=f.op+1;
if(!vis[t.v1][t.v2])
{
vis[t.v1][t.v2]=1;
t.cur=5;
t.pre=pos;
que[e++]=t;
}
}
if(f.v1!=0&&f.v2!=n)
{
t.v1=f.v1-(n-f.v2);if(t.v1<0) t.v1=0;
t.v2=f.v2+f.v1; if(t.v2>n) t.v2=n;
t.op=f.op+1;
if(!vis[t.v1][t.v2])
{
vis[t.v1][t.v2]=1;
t.cur=6;
t.pre=pos;
que[e++]=t;
}
}
}
puts("impossible");
}
int main()
{
while(cin>>m>>n>>c)
{
memset(vis,0,sizeof(vis));
bfs();
}
return 0;
}
版权声明:本文博客原创文章。博客,未经同意,不得转载。
POJ 3414--Pots(BFS+回溯路径)的更多相关文章
- poj 3414 Pots(bfs+输出路径)
Description You are given two pots, having the volume of A and B liters respectively. The following ...
- POJ 3414 Pots ( BFS , 打印路径 )
题意: 给你两个空瓶子,只有三种操作 一.把一个瓶子灌满 二.把一个瓶子清空 三.把一个瓶子里面的水灌到另一个瓶子里面去(倒满之后要是还存在水那就依然在那个瓶子里面,或者被灌的瓶子有可能没满) 思路: ...
- POJ 3414 Pots bfs打印方案
题目: http://poj.org/problem?id=3414 很好玩的一个题.关键是又16ms 1A了,没有debug的日子才是好日子.. #include <stdio.h> # ...
- POJ 3414 Pots(BFS)
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Description You are g ...
- POJ - 3414 Pots BFS(著名倒水问题升级版)
Pots You are given two pots, having the volume of A and B liters respectively. The following operati ...
- 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+记录路径 】
//yy:昨天看着这题突然有点懵,不知道怎么记录路径,然后交给房教了,,,然后默默去写另一个bfs,想清楚思路后花了半小时写了120+行的代码然后出现奇葩的CE,看完FAQ改了之后又WA了.然后第一次 ...
- 广搜+输出路径 POJ 3414 Pots
POJ 3414 Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13547 Accepted: 5718 ...
随机推荐
- Redis在win7上的安装与可视化应用
Redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sorted set ...
- dede 首页或列表页调用文章内容页body内容
在使用dede过程,有的朋友会调调出文章的列表的内容出来,怎么调呢?当然是用dede的传参的数据查询语句了,方法如下: {dede:arclist flag=h typeid=2 row=1 titl ...
- ftp桥接到http服务
先说一下我的需求:我的linodeserver近期ftp和sftp连不上了,port被封了.仅仅有http能够訪问,我没有办法上传文件了.由于我寻常都用beyond compare上传文件,非常方便. ...
- SQL Server :理解数据页结构
原文:SQL Server :理解数据页结构 我们都很清楚SQL Server用8KB 的页来存储数据,并且在SQL Server里磁盘 I/O 操作在页级执行.也就是说,SQL Server 读取或 ...
- MySQL保留关键字
今天在使用hibernate关联映射导出表的时候因为映射了一个表名为option,是MYSQL的关键字,总是生成错误,一开始以为是映射文件和代码问题,检查不出问题才想到可能用到数据库的保留关键字了,查 ...
- 基于JAVA WEB技术旅游服务网站系统设计与实现网上程序代写
基于JAVA WEB技术旅游服务网站系统设计与实现网上程序代写 专业程序代写服务(QQ:928900200) 随着社会的进步.服务行业的服务水平不断发展与提高,宾馆.酒店.旅游等服务行业的信息量和工作 ...
- DeviceIoControl 直接从磁盘扇区读文件
好久没写博客了,近期看了下DeviceIoControl 关于磁盘的应用,特记一文,以备久后查阅. 首先介绍下,文件在磁盘的存储结构(详细能够到网上查询NTFS文件系统相关的教程后者数据恢复方面教程 ...
- swift的struct本节描述结构的类型
<span style="font-size:24px;">struct David { var x = 0;//一个结构的定义,两个字段x,y var y = 0;/ ...
- Codeforces 484E Sign on Fence(是持久的段树+二分法)
题目链接:Codeforces 484E Sign on Fence 题目大意:给定给一个序列,每一个位置有一个值,表示高度,如今有若干查询,每次查询l,r,w,表示在区间l,r中, 连续最长长度大于 ...
- 移动应用开发(IOS/android等一下)在一般图像缓存方案评述(附流程图)
在移动应用开发.我们经常从网络请求到该设备显示遇到的场景图片. 假设多次发动每个请求,废物流.浪费电.: 将图片持久化到磁盘也不失为一种策略:但每次从文件读取图片也存在一定的io开销,就算採用此策略, ...