POJ 3414 Pots(BFS+回溯)
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 11705 | Accepted: 4956 | 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)
POUR(2,1)
DROP(1)
POUR(2,1)
FILL(2)
POUR(2,1)
Source
field=source&key=Northeastern+Europe+2002" style="text-decoration:none">Northeastern Europe 2002
, Western Subregion假设搜不到就输出impossible
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue> using namespace std; int n,m,k;
int ans;
int v[110][110]; struct node
{
int x;
int y;
int z;
int cnt;
} a[1000010]; void DFS(int kk)
{
int pt = a[kk].cnt;
if(pt<=0)
{
return ;
}
DFS(pt);
if(a[pt].x == 1)
{
if(a[pt].y == 1)
{
printf("FILL(1)\n");
}
else
{
printf("FILL(2)\n");
}
}
else if(a[pt].x == 2)
{
if(a[pt].y == 1)
{
printf("DROP(1)\n");
}
else
{
printf("DROP(2)\n");
}
}
else if(a[pt].x == 3)
{
if(a[pt].y == 1)
{
printf("POUR(1,2)\n");
}
else
{
printf("POUR(2,1)\n");
}
}
} void BFS()
{
ans = 1;
queue<node>q;
memset(v,0,sizeof(v));
struct node t,f;
t.x = 0;
t.y = 0;
t.z = 0;
t.cnt = 0;
a[0].x = 0;
a[0].y = 0;
a[0].cnt = 0;
q.push(t);
v[t.x][t.y] = 1;
while(!q.empty())
{
t = q.front();
q.pop();
for(int i=1; i<=3; i++)
{
for(int j=1; j<=2; j++)
{
f.x = t.x;
f.y = t.y;
if(i == 1)
{
if(j == 1 && f.x!=n)
{
f.x = n;
}
else if(j == 2 && f.y!=m)
{
f.y = m;
}
}
else if(i == 2)
{
if(j == 1 && f.x!=0)
{
f.x = 0;
}
else if(j == 2 && f.y!=0)
{
f.y = 0;
}
}
else if(i == 3)
{
if(j == 1 && (f.x!=0 && f.y!=m))
{
if(f.x>=m-f.y)
{
f.x = f.x - m + f.y;
f.y = m;
}
else
{
f.y = f.y + f.x;
f.x = 0;
}
}
else if(j == 2 && (f.y!=0 && f.x!=n))
{
if(f.y>=n-f.x)
{
f.y = f.y - n + f.x;
f.x = n;
}
else
{
f.x = f.x + f.y;
f.y = 0;
}
}
}
if(v[f.x][f.y] == 0)
{
f.cnt = ans;
f.z = t.z + 1;
a[ans].x = i;
a[ans].y = j;
a[ans].cnt = t.cnt;
q.push(f);
v[f.x][f.y] = 1;
if(f.x == k || f.y == k)
{
printf("%d\n",f.z);
DFS(ans);
if(i == 1)
{
if(j == 1)
{
printf("FILL(1)\n");
}
else
{
printf("FILL(2)\n");
}
}
else if(i == 2)
{
if(j == 1)
{
printf("DROP(1)\n");
}
else
{
printf("DROP(2)\n");
}
}
else if(i == 3)
{
if(j == 1)
{
printf("POUR(1,2)\n");
}
else
{
printf("POUR(2,1)\n");
}
}
return ;
}
ans++;
}
}
}
}
printf("impossible\n");
} int main()
{
while(scanf("%d%d%d",&n,&m,&k)!=EOF)
{
BFS();
}
return 0;
}
POJ 3414 Pots(BFS+回溯)的更多相关文章
- POJ 3414 Pots bfs打印方案
题目: http://poj.org/problem?id=3414 很好玩的一个题.关键是又16ms 1A了,没有debug的日子才是好日子.. #include <stdio.h> # ...
- POJ 3414 Pots(BFS)
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Description You are g ...
- 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(著名倒水问题升级版)
Pots You are given two pots, having the volume of A and B liters respectively. The following operati ...
- 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 , 打印路径 )
题意: 给你两个空瓶子,只有三种操作 一.把一个瓶子灌满 二.把一个瓶子清空 三.把一个瓶子里面的水灌到另一个瓶子里面去(倒满之后要是还存在水那就依然在那个瓶子里面,或者被灌的瓶子有可能没满) 思路: ...
- poj 3414 Pots 【BFS+记录路径 】
//yy:昨天看着这题突然有点懵,不知道怎么记录路径,然后交给房教了,,,然后默默去写另一个bfs,想清楚思路后花了半小时写了120+行的代码然后出现奇葩的CE,看完FAQ改了之后又WA了.然后第一次 ...
- BFS POJ 3414 Pots
题目传送门 /* BFS:六种情况讨论一下,BFS轻松解决 起初我看有人用DFS,我写了一遍,TLE..还是用BFS,结果特判时出错,逗了好长时间 看别人的代码简直是受罪,还好自己终于发现自己代码的小 ...
- POJ 3414 Pots(罐子)
POJ 3414 Pots(罐子) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 You are given two po ...
随机推荐
- AbstractAction
package cn.tz.action.abs; import java.io.File; import java.io.IOException; import java.text.SimpleDa ...
- Spring @Value 用法小结,#与$的区别
20161016更新:这货其实是SpEL的功能,来这里看看吧: Spring 4 官方文档学习(五)核心技术之SpEL 起因 一直的用法是 @Value("${jdbc.driverClas ...
- EBS安装完成后,对数据库相关配置的改动
EBS安装完成后,对数据库相关配置的改动 1.转为ASM,数据文件,控制文件,redo log,archived log从文件系统转移至ASM 2.禁用resource manager 由于发现系统的 ...
- Android Binder分析二:Natvie Service的注冊
这一章我们通过MediaPlayerService的注冊来说明怎样在Native层通过binder向ServiceManager注冊一个service,以及client怎样通过binder向Servi ...
- 撤销正在审核的app
一个app还未通过审核,但是新版本已经出来了,怎样才能撤销正在审核的app呢? 方法:在 是binary deatils里用 reject this binary.之后,即可以重新上传代码了.
- selenium+python自动化82-只截某个元素的图
前言 selenium截取全图小伙伴们都知道,曾经去面试的时候,面试官问:如何截图某个元素的图?不要全部的,只要某个元素...小编一下子傻眼了, 苦心人,天不负,终于找到解决办法了. selenium ...
- CRFPP/CRF++编译安装与部署
CRFPP/CRF++编译安装与部署 下载CRF++ https://taku910.github.io/crfpp/#download 说明:在上面网站中下载CRF++ 0.58 解压 tar zx ...
- e为无理数的证明
from: http://math.fudan.edu.cn/gdsx/XXYD.HTM
- Python爬虫之selenium爬虫,模拟浏览器爬取天猫信息
由于工作需要,需要提取到天猫400个指定商品页面中指定的信息,于是有了这个爬虫.这是一个使用 selenium 爬取天猫商品信息的爬虫,虽然功能单一,但是也算是 selenium 爬虫的基本用法了. ...
- Javascript 中“靠”的使用
Javascript中call的使用自己感觉蛮纠结的,根据文档很好理解,其实很难确定你是否真正的理解. call 方法应用于:Function 对象调用一个对象的一个方法,以另一个对象替换当前对象.c ...