poj 3414 Pots【bfs+回溯路径 正向输出】
题目地址:http://poj.org/problem?id=3414
| Time Limit: 1000MS | Memory Limit: 65536K | |||
| Total Submissions: 12080 | Accepted: 5104 | 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)
#include<stdio.h>
#include<string.h>
#include <queue>
#include <stack>
#include <algorithm> using namespace std; int a, b, c;//a b容器的大小 c目标体积 bool vis[101][101]; struct node
{
char ch;
int x,y;
int pre;
int self;
}op[100000];
int e=0; struct path
{
int u, v;
int cnt;
int pre;
int self;
}; //int road[10000], step; stack<int>p;
bool ok;
int ans; void bfs()
{
queue<path>q;//创建队列
memset(vis, false, sizeof(vis));//初始化未访问 path s; s.u=0; s.v=0; s.cnt=0; s.pre=-1; s.self=-1;
q.push(s); int num=-1;
vis[0][0]=true; ok=false; ans=0; while(!q.empty())
{
path cur=q.front(); q.pop(); if(cur.u==c || cur.v==c){
ans=cur.cnt;
ok=true;
int fa=cur.self;
while(fa!=-1){
p.push(fa);
fa=op[fa].pre;
}
break;
}//如果找到了目标状态 //进行6种操作
path temp;
//Fill A
temp=cur; temp.u=a; temp.cnt=cur.cnt+1;
if(!vis[temp.u][temp.v]){
temp.pre=cur.self; num++; temp.self=num;
q.push(temp);
vis[temp.u][temp.v]=true;//标记状态访问
op[e].pre=cur.self;
op[e].ch='F'; op[e++].x=1;
} //File B
temp=cur; temp.v=b; temp.cnt=cur.cnt+1;
if(!vis[temp.u][temp.v]){
temp.pre=cur.self; num++; temp.self=num;
q.push(temp);
vis[temp.u][temp.v]=true;
op[e].pre=cur.self;
op[e].ch='F'; op[e++].x=2;
} //Drop A
temp=cur; temp.u=0; temp.cnt=cur.cnt+1;
if(!vis[temp.u][temp.v]){
temp.pre=cur.self; num++; temp.self=num;
q.push(temp);
vis[temp.u][temp.v]=true;
op[e].pre=cur.self;
op[e].ch='D'; op[e++].x=1;
}
//Drop B
temp=cur; temp.v=0; temp.cnt=cur.cnt+1;
if(!vis[temp.u][temp.v]){
temp.pre=cur.self; num++; temp.self=num;
q.push(temp);
vis[temp.u][temp.v]=true;
op[e].pre=cur.self;
op[e].ch='D'; op[e++].x=2;
} //pour(A, B)
temp.v=cur.u+cur.v; temp.u=0; temp.cnt=cur.cnt+1;
if(temp.v>b){
temp.v=b;//装满B
temp.u=cur.u+cur.v-b;
}
if(!vis[temp.u][temp.v]){
temp.pre=cur.self; num++; temp.self=num;
q.push(temp);
vis[temp.u][temp.v]=true;
op[e].pre=cur.self;
op[e].ch='P'; op[e].x=1; op[e++].y=2;
} //pour(B, A)
temp.u=cur.u+cur.v; temp.v=0; temp.cnt=cur.cnt+1;
if(temp.u>a){
temp.u=a;//装满A
temp.v=cur.u+cur.v-a;
}
if(!vis[temp.u][temp.v]){
temp.pre=cur.self; num++; temp.self=num;
q.push(temp);
vis[temp.u][temp.v]=true;
op[e].pre=cur.self;
op[e].ch='P'; op[e].x=2; op[e++].y=1;
} }
} int main()
{
scanf("%d %d %d", &a, &b, &c);
e=0;
bfs(); if(!ok){
printf("impossible\n");
}
else{
printf("%d\n", ans);
while(!p.empty())
{
int dd=p.top(); p.pop(); if(op[dd].ch=='F'){
printf("FILL(%d)\n", op[dd].x );
}
else if(op[dd].ch=='D'){
printf("DROP(%d)\n", op[dd].x );
}
else{
printf("POUR(%d,%d)\n", op[dd].x, op[dd].y );
}
}
} return 0;
}
POUR(2,1) DROP(1) POUR(2,1) FILL(2) POUR(2,1) 代码:
poj 3414 Pots【bfs+回溯路径 正向输出】的更多相关文章
- POJ 3414 Pots ( BFS , 打印路径 )
题意: 给你两个空瓶子,只有三种操作 一.把一个瓶子灌满 二.把一个瓶子清空 三.把一个瓶子里面的水灌到另一个瓶子里面去(倒满之后要是还存在水那就依然在那个瓶子里面,或者被灌的瓶子有可能没满) 思路: ...
- POJ 3414--Pots(BFS+回溯路径)
Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9963 Accepted: 4179 Special Ju ...
- 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打印方案
题目: http://poj.org/problem?id=3414 很好玩的一个题.关键是又16ms 1A了,没有debug的日子才是好日子.. #include <stdio.h> # ...
- 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)
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Description You are g ...
- 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了.然后第一次 ...
随机推荐
- PRINTDLG 打印对话框操作
typedef struct tagPD { DWORD lStructSize; HWND hwndOwner; HGLOBAL hDevMode; HGLOBAL hDevNames; HDC h ...
- 转载: vim使用技巧
两篇很牛的vim使用技巧 来源: ChinaUnix博客 日期: 2009.07.06 10:18 (共有条评论) 我要评论 读本文之前请注意:1. 本文的目标是提供一些vim的使用技巧,利用 ...
- 1、easyUI-创建 CRUD普通dataGrid(表格)
在实现功能之前,我们要做以下几个准备: 分以下几个步骤:开发工具,easyUI包,目录结构,创建数据库,创建相应的页面视图,后台代码编写,优化: 第一步:开发工具 我的开发工具是Hbuild,开发语言 ...
- Javascript中的函数中的this值
看下面这段代码会在控制台上输出什么内容? <script> var url="fang.com"; var obj={ url:"soufun.com&quo ...
- 《从零开始学Swift》学习笔记(Day 20)——函数中参数的传递引用
原创文章,欢迎转载.转载请注明:关东升的博客 参数的传递引用 类是引用类型,其他的数据类型如整型.浮点型.布尔型.字符.字符串.元组.集合.枚举和结构体全部是值类型. 有的时候就是要将一个值类型参数以 ...
- RabbitMQ安装篇
一切不是自己实战,且跑不起来的程序都是在耍流氓! 先下载: http://www.erlang.org/downloads erlang 包
- passback_params 支付回调的 原样返回字段 自定义字段的存放字段
开放平台文档中心 https://docs.open.alipay.com/204/105465/ passback_params String 否 512 公用回传参数,如果请求时传递了该参数,则返 ...
- VSpy之C Code Interface的使用
Spy3 要运行 CCodeInterface 功能,需要安装运行环境,建议安装 Visual Studio2003,2005,2008,2010 或更新的版本.当然也可以安装 VC express ...
- MFC重绘原理的关键理解
// ====================Windows重绘消息与函数========================== 得到桌面窗口的句柄,然后再绘图HWND GetDesktopWindow ...
- 洛谷 P1462 通往奥格瑞玛的道路
洛谷 题意:要求在限定油耗内,求最小花费的最大值. 求最小值最大很容易想到二分答案.所以我们往二分的方向去想. 我们二分一个费用,然后要保证到终点时满足限定油耗,所以跑最短路. 不过松弛条件要改一下: ...