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 ...
随机推荐
- spring---aop(9)---Spring AOP中引入增强
写在前面 Spring将introduction通知看作一种特殊类型的拦截通知.用Spring的行话来讲,对方法的增强叫做Wearing(织入),而对类的增强叫introduction(引入).Int ...
- Maven根据不同环境打包不同配置文件
开发项目时会遇到这个问题:开发环境,测试环境,生产环境的配置文件不同,打包时经常要手动更改配置文件,更改的少还可以接受,但是如果需要更多个配置文件,手动的方法就显得非常笨重了. 下面介绍一种方法,利用 ...
- SpringMVC怎么获取前台传来的数组
var tollerlist = new Array(); for(var k in objToller){ tollerlist.push(k); } $.ajax({ type:"pos ...
- Asp.net处理程序(第六篇)
四.Web服务处理程序 对于Web服务来说,标准的方式是使用SOAP协议,在SOAP中,请求和回应的数据通过XML格式进行描述.在Asp.net 4.0下,对于Web服务来说,还可以选择支持Ajax访 ...
- How to Find Processlist Thread id in gdb !!!!!GDB 使用
https://mysqlentomologist.blogspot.jp/2017/07/ Saturday, July 29, 2017 How to Find Process ...
- Running multiple instances of Xamarin Studio on a Mac
I love developing software on my MacBook Air! I got the latest version with the maximum possible spe ...
- MVC对集合筛选,不使用Where(),而使用FindAll()
当想对集合筛选的时候,经常想到用Where过滤,而实际上List<T>.FindAll()也是不错的选择. 如果有一个订单,属性有下单时间.区域等等.如何使用List<T>.F ...
- 汉字转拼音的Java类库——JPinyin
原文:http://blog.csdn.net/stuxuhai/article/details/8932715 [JPinyin主要特性]1.准确.完善的字库:Unicode编码从4E00-9FA5 ...
- 什么是软件project?
Normal 0 7.8 pt 0 2 false false false MicrosoftInternetExplorer4 /* Style Definitions */ table.MsoNo ...
- pgm转jpg
clc;clear all;for i=1:40for j=1:10image=imread(strcat('N:\FACE\orl_faces\s',...int2str(i),'\',int2st ...