poj 3414 Pots ( bfs )
题目:http://poj.org/problem?id=3414
题意:给出了两个瓶子的容量A,B, 以及一个目标水量C,
对A、B可以有如下操作:
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).
问经过哪几个操作后能使得任意一个瓶子的残余水量为C。
若不可能得到则输出impossible
#include <iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<stack>
#include<queue>
#include<iomanip>
#include<cmath>
#include<map>
#include<vector>
#include<algorithm>
using namespace std; int vis[][];
int a,b,c;
struct node
{
int x,y,step;
}pos,next; struct way
{
int x,y,f;
}before[][]; void pri(int x,int y)
{
stack<int>st;
while(x!=||y!=)
{
st.push(before[x][y].f);
int tx=before[x][y].x;
int ty=before[x][y].y;
x=tx; y=ty;
}
while(!st.empty())
{
switch(st.top())
{
case :printf("DROP(1)\n"); break;
case :printf("DROP(2)\n"); break;
case :printf("FILL(1)\n"); break;
case :printf("FILL(2)\n"); break;
case :printf("POUR(1,2)\n"); break;
case :printf("POUR(2,1)\n"); break;
}
st.pop();
}
}
int bfs()
{
queue<node>q;
next.x=; next.y=; next.step=;
vis[][]=;
q.push(next);
while(!q.empty())
{
pos=q.front();
//cout<<pos.x<<" "<<pos.y<<" "<<pos.step<<" ";
//cout<<before[pos.x][pos.y].x<<" "<<before[pos.x][pos.y].y<<" "<<before[pos.x][pos.y].f<<endl;
q.pop();
if(pos.x==c||pos.y==c)
{
cout<<pos.step<<endl;
pri(pos.x,pos.y);
return ;
}
if(!vis[][pos.y]&&pos.x!=)
{
next.x=; next.y=pos.y; next.step=pos.step+;
q.push(next);
vis[][pos.y]=;
before[][pos.y]=(struct way){pos.x,pos.y,};
}
if(!vis[pos.x][]&&pos.y!=)
{
next.x=pos.x; next.y=; next.step=pos.step+;
q.push(next);
vis[pos.x][]=;
before[pos.x][]=(struct way){pos.x,pos.y,};
}
if(!vis[a][pos.y]&&pos.x!=a)
{
next.x=a; next.y=pos.y; next.step=pos.step+;
q.push(next);
vis[a][pos.y]=;
before[a][pos.y]=(struct way){pos.x,pos.y,};
}
if(!vis[pos.x][b]&&pos.y!=b)
{
next.x=pos.x; next.y=b; next.step=pos.step+;
q.push(next);
vis[pos.x][b]=;
before[pos.x][b]=(struct way){pos.x,pos.y,};
}
if(pos.x>&&pos.y<b)
{
int t=min(pos.x,b-pos.y);
if(!vis[pos.x-t][pos.y+t])
{
q.push((struct node){pos.x-t,pos.y+t,pos.step+});
vis[pos.x-t][pos.y+t]=;
before[pos.x-t][pos.y+t]=(struct way){pos.x,pos.y,};
}
}
if(pos.x<a&&pos.y>)
{
int t=min(pos.y,a-pos.x);
if(!vis[pos.x+t][pos.y-t])
{
q.push((struct node){pos.x+t,pos.y-t,pos.step+});
vis[pos.x+t][pos.y-t]=;
before[pos.x+t][pos.y-t]=(struct way){pos.x,pos.y,};
}
}
}
return ;
}
int main()
{
cin>>a>>b>>c;
memset(vis,,sizeof(vis));
if(bfs()==)
cout<<"impossible"<<endl;
return ;
}
poj 3414 Pots ( bfs )的更多相关文章
- POJ 3414 Pots(罐子)
POJ 3414 Pots(罐子) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 You are given two po ...
- poj 3414 Pots (bfs+线索)
Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10071 Accepted: 4237 Special J ...
- POJ 3414 Pots(BFS+回溯)
Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11705 Accepted: 4956 Special J ...
- poj 3414 Pots 【BFS+记录路径 】
//yy:昨天看着这题突然有点懵,不知道怎么记录路径,然后交给房教了,,,然后默默去写另一个bfs,想清楚思路后花了半小时写了120+行的代码然后出现奇葩的CE,看完FAQ改了之后又WA了.然后第一次 ...
- poj 3414 Pots【bfs+回溯路径 正向输出】
题目地址:http://poj.org/problem?id=3414 Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
- poj 3414 Pots(广搜BFS+路径输出)
转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:id=3414">http://poj.org/probl ...
- 【POJ - 3414】Pots(bfs)
Pots 直接上中文 Descriptions: 给你两个容器,分别能装下A升水和B升水,并且可以进行以下操作 FILL(i) 将第i个容器从水龙头里装满(1 ≤ i ≤ 2); DRO ...
- POJ 3414 Pots (dfs,这个代码好长啊QAQ)
Description You are given two pots, having the volume of A and B liters respectively. The following ...
- POJ 3414 pots (未解决)
http://poj.org/problem?id=3414 #include <iostream> #include <cstdio> #include <queue& ...
随机推荐
- Ajax 之【文件上传】
// 前台 var formData = new FormData(); var file = document.getElementById('myFile').files[0]; formData ...
- 解决 windows2012 下无法安装 sql2008R2
Press the Windows logo key, type control panel, and then click the Control Panel icon. Note If you a ...
- google map api 学习笔记
(1)地图的缩放监听函数 google.maps.event.addlistener(map,"zoom_change",function(){ 缩放级别变化后的函数. }); ( ...
- 免信用卡注册美国App Store账号
对于一些应用国内的App Store无法下载让人很郁闷,而自己又有点轻微的强迫症.于是开始尝试免信用卡注册iCloud账号. Apple的官方网站上的教程,见http://support.apple. ...
- Battle Over Cities (25)(DFS、连通图)
It is vitally important to have all the cities connected by highways in a war. If a city is occupied ...
- EasyUI Layout Full - Not Correct in IE8
EasyUI Full布局在IE10,IE9下正常,IE8无效果,标记一下有知道的可以留个言! 如图 IE 10 IE 8
- having与where区别
having后可以跟组函数如avg(sal)而where后不可以有, 如果条件不是必须使用组函数最好还是使用where
- Mac环境下装node.js,npm,express;(包括express command not found)
1. 下载node.js for Mac 地址: http://nodejs.org/download/ 直接下载 pkg的,双击安装,一路点next,很容易就搞定了. 安装完会提醒注意 node和n ...
- Android Studio 单刷《第一行代码》系列目录
前言(Prologue) 本系列将使用 Android Studio 将<第一行代码>(书中讲解案例使用Eclipse)刷一遍,旨在为想入坑 Android 开发,并选择 Android ...
- Extjs-4.2.1(二)——使用Ext.define自定义类
鸣谢:http://www.cnblogs.com/youring2/archive/2013/08/22/3274135.html --------------------------------- ...