POJ3414 Pots —— BFS + 模拟
来源:互联网 发布:淘宝开店挣钱吗 编辑:程序博客网 时间:2024/05/19 23:27
题目链接:http://poj.org/problem?id=3414
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
6FILL(2)POUR(2,1)DROP(1)POUR(2,1)FILL(2)POUR(2,1)
Source
题解:
类似的题目,也是有关几个容器倒来倒去,然后求一个目标量:http://blog.csdn.net/dolfamingo/article/details/77804030
此题不过是多了个路径输出,队列把STL换成数组就可以了,不细述。
代码如下:
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>#include <vector>#include <queue>#include <stack>#include <map>#include <string>#include <set>#define ms(a,b) memset((a),(b),sizeof((a)))using namespace std;typedef long long LL;const int INF = 2e9;const LL LNF = 9e18;const int MOD = 1e9+7;const int MAXN = 100+10;struct node{ //con[i]为容器i当前的量;op存操作的信息;pre为上一步所在队列的下标(为了输出路径) int con[2], op[3], step, pre;};int vis[MAXN][MAXN], vol[3]; //vol[0]、vol[1]为两个容器的容量, vol[3]为目标量node que[100010]; //由于要输出路径,就要用数组来作队列了int front, rear;int bfs(){ ms(vis,0); front = rear = 0; node now, tmp; now.con[0] = now.con[1] = 0; now.step = 0; vis[0][0] = 1; que[rear++] = now; while(front!=rear) { now = que[front++]; if(now.con[0]==vol[2] || now.con[1]==vol[2]) return front-1; for(int i = 0; i<2; i++) { tmp = now; //操作1:FILL tmp.con[i] = vol[i]; if(!vis[tmp.con[0]][tmp.con[1]]) { vis[tmp.con[0]][tmp.con[1]] = 1; tmp.op[0] = 1; tmp.op[1] = i; tmp.step = now.step+1; tmp.pre = front-1; que[rear++] = tmp; } tmp = now; //操作2:DROP tmp.con[i] = 0; if(!vis[tmp.con[0]][tmp.con[1]]) { vis[tmp.con[0]][tmp.con[1]] = 1; tmp.op[0] = 2; tmp.op[1] = i; tmp.step = now.step+1; tmp.pre = front-1; que[rear++] = tmp; } tmp = now; //操作三POUR int j = !i; //另外一个容器 int pour = min(tmp.con[i], vol[j]-tmp.con[j]); tmp.con[j] += pour; tmp.con[i] -= pour; if(!vis[tmp.con[0]][tmp.con[1]]) { vis[tmp.con[0]][tmp.con[1]] = 1; tmp.op[0] = 3; tmp.op[1] = i; tmp.op[2] = j; tmp.step = now.step+1; tmp.pre = front-1; que[rear++] = tmp; } } } return -1;}void Print(int i) //输出路径{ if(que[i].pre!=0) Print(que[i].pre); if(que[i].op[0]==1) printf("FILL(%d)\n", que[i].op[1]+1); else if(que[i].op[0]==2) printf("DROP(%d)\n", que[i].op[1]+1); else printf("POUR(%d,%d)\n", que[i].op[1]+1, que[i].op[2]+1);}int main(){ while(scanf("%d%d%d",&vol[0], &vol[1], &vol[2])!=EOF) { int i = bfs(); if(i==-1) puts("impossible"); else { printf("%d\n",que[i].step); Print(i); } }}
- POJ3414 Pots —— BFS + 模拟
- BFS—POJ3414-Pots
- poj3414——Pots(BFS)
- poj3414——Pots
- POJ3414——Pots
- poj3414——Pots
- POJ3414 Pots(BFS)
- poj3414 Pots(BFS)
- POJ3414 Pots BFS
- POJ3414 Pots(BFS)
- poj3414 Pots BFS
- 【POJ3414】Pots(BFS)
- poj3414 Pots(bfs)
- poj3414 pots 倒水模拟
- POJ3414 Pots(路径记录,广搜BFS,模拟)
- poj3414--Pots(bfs,记录路径)
- Pots——BFS
- POJ3414 Pots
- UE4出现texture streaming poor over红字解决方案
- 数组的一些常用方法分析 介绍
- maven仓库--私服(Nexus的配置使用)
- 友元函数和友元类
- 学生信息管理系统(C++)
- POJ3414 Pots —— BFS + 模拟
- 大数据预科班3_小案例
- pandas选取一列画直方图
- Qt--委托
- <9>—— Palindrome Number
- Java虚拟机体系结构
- java中的匿名内部类总结
- Leetcode日志--LongestPalindromicSubstring
- 20muduo_base库源码分析(十一)