POJ 3414 Pots
来源:互联网 发布:mac上如何打开rar文件 编辑:程序博客网 时间:2024/06/03 08:41
题目大意:
有两个容量分别为a,b的杯子,需要经过若干次变换使任意一个杯子的容量为c,求最短的变换路径
#include <iostream>#include <queue>#include <cstring>using namespace std;int a,b,k;int vis[105][105];struct node{ int x,y,step; string path;};string p[] = {"FILL(2)","FILL(1)","POUR(2,1)","POUR(1,2)","DROP(1)","DROP(2)"};void print(node cur){ cout << cur.step << endl; for (int i = 0; i < cur.path.size(); ++i) cout << p[cur.path[i]-'0'] << endl;}int BFS(){ node cur,next; cur.x = 0; cur.y = 0; cur.step = 0; cur.path = ""; queue <node> q; q.push(cur); while(!q.empty()) { cur = q.front(); q.pop(); if (cur.x == k|| cur.y == k) { print(cur); return 0; } /*fill*/ if (cur.x != a) { next = {a,cur.y,cur.step+1}; if (!vis[a][cur.y]) { next.path = cur.path + "1"; q.push(next); vis[a][cur.y] = 1; } } if (cur.y != b) { next = {cur.x,b,cur.step+1}; if (!vis[cur.x][b]) { next.path = cur.path + "0"; q.push(next); vis[cur.x][b] = 1; } } /*drop*/ next = {0,cur.y,cur.step+1}; if(!vis[0][cur.y]) { next.path = cur.path + "4"; q.push(next); vis[0][cur.y] = 1; } next = {cur.x,0,cur.step+1}; if (!vis[cur.x][0]) { next.path = cur.path + "5"; q.push(next); vis[cur.x][0] = 1; } /*pour*/ if (cur.x != a && cur.y != 0) { int t1 = cur.x, t2 = cur.y; if (a - t1 <= t2) next = {a,t2-a+t1,cur.step+1}; else next = {t1+t2,0,cur.step+1}; if (!vis[next.x][next.y]) { next.path = cur.path + "2"; vis[next.x][next.y] = 1; q.push(next); } } if (cur.y != b && cur.x != 0) { int t1 = cur.x, t2 = cur.y; if (b - t2 <= t1) next = {t1-b+t2,b,cur.step+1}; else next = {0,t1+t2,cur.step+1}; if (!vis[next.x][next.y]) { next.path = cur.path + "3"; vis[next.x][next.y] = 1; q.push(next); } } } return -1;}int main(){ while(cin >> a >> b >> k) { memset(vis,0,sizeof(vis)); if (BFS() == -1) cout << "impossible" << endl; } return 0;}
0 0
- POJ 3414 Pots
- poj 3414 pots
- POJ 3414 Pots
- POJ 3414 Pots BFS
- POJ 3414 Pots
- poj 3414 Pots
- poj 3414 Pots
- POJ 3414 Pots
- poj 3414 Pots
- POJ 3414(Pots)
- POJ 3414 Pots
- POJ 3414 Pots
- POJ-3414-Pots
- POJ 3414 pots
- POJ 3414 Pots
- POJ 3414 Pots
- POJ 3414 Pots
- poj 3414 Pots
- graham扫描法求凸包
- 集群、分布式、负载均衡区别与联系
- 【Android基础】-Activity启动模式详解
- 技术角度wordpress结构优缺点分析
- netty学习(四)----伪异步IO的弊端
- POJ 3414 Pots
- [Android界面] 导航栏下划线出现偏移
- Leetcode: H-Index II
- leetcode: (235) Lowest Common Ancestor of a Binary Search Tree
- 算法时间复杂度以及代码实现
- 移动端布局浅谈
- 简单四则运算
- 解决方案:An error was encountered while running(Domain=FBSOpenApplicationErrorDomain, Code=4)
- Hadoop 案例6-----TopN问题:求最大的K个值并排序