POJ 3414 Pots
2015-09-05 16:16
302 查看
题目大意:
有两个容量分别为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;
}
有两个容量分别为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;
}
相关文章推荐
- 【Android基础】-Activity启动模式详解
- graham扫描法求凸包
- spoj -705 New Distinct Substrings--后缀数组
- OpenGL 多重纹理
- [Android界面] 导航栏下划线出现偏移
- 集群、分布式、负载均衡区别与联系
- Ubuntu14.04+eclipse下cocos2d-x3.6正式版环境的搭建
- Android学习系列之(七)Fragment深入
- java入门学习总结——【java菜鸟成长记】
- XCode单元测试
- Ubuntu下搭建Cocos2D-X 3.6开发环境
- 【机器学习基础】理解为什么机器可以学习——Hoeffding不等式
- 集群化部署定时任务apscheduler库 - 推酷
- 对于问题,要打破砂锅问到底,也要懂得不求甚解——不执着于问题本身
- CSSborder制作小三角形
- 验证码
- Volley框架全解析
- js 判断所选时间(或者当前时间)是否在某一时间段的实现代码
- UC/OS II 任务管理(6)之多任务启动
- Missing styles. Is the correct theme chosen for this layout?