题意:

给你两个空瓶子,只有三种操作

一、把一个瓶子灌满

二、把一个瓶子清空

三、把一个瓶子里面的水灌到另一个瓶子里面去(倒满之后要是还存在水那就依然在那个瓶子里面,或者被灌的瓶子有可能没满)

思路:BFS,打印路径时需技巧。

//G++ 840K	0MS
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <stack>
#include <queue>
#include <cmath>
#include <map>
#include <vector>
#include <algorithm>
#define MIN(A,B) ((A)<(B)?(A):(B))
#define DEBUG puts("Hello world!");
#define N 110
using namespace std ; bool visit[N][N] ; struct node
{
int x , y ;
int step ;
}pos , next ; struct way
{
int x , y ;
int stept ;
}before[N][N] ; void
Print_Path ( int x , int y )
{
stack<int>st ;
while ( x != 0 || y != 0 )
{
st.push ( before[x][y].stept ) ;
int temp_x , temp_y ;
temp_x = before[x][y].x ;
temp_y = before[x][y].y ;
x = temp_x ;
y = temp_y ;
}
while ( !st.empty() )
{
switch ( st.top() )
{
case 1:
printf("DROP(1)\n");
break;
case 2:
printf("DROP(2)\n");
break;
case 3:printf("FILL(1)\n");
break;
case 4:printf("FILL(2)\n");
break;
case 5:printf("POUR(1,2)\n");
break;
case 6:printf("POUR(2,1)\n");
break;
}
st.pop ( ) ;
}
return ;
} void
Modify ( node * const p , int const x , int const y , int const step )
{
p->x = x ;
p->y = y ;
p->step = step ;
return ;
} bool
BFS ( int const A , int const B , int const C )
{
queue <node>q ;
next.x = 0 ;
next.y = 0 ;
next.step = 0 ;
visit[0][0] = true ;
q.push(next) ;
while ( !q.empty( ) )
{
pos = q.front ( ) ;
q.pop ( ) ;
if ( C == pos.x || C == pos.y )
{
printf ("%d\n" ,pos.step ) ;
Print_Path ( pos.x , pos.y ) ;
//DEBUG
return true ;
}
if ( !visit[0][pos.y] && 0 != pos.x ) //case 1 : DROP(1)
{
Modify ( & next , 0 , pos.y , pos.step + 1 ) ;
q.push ( next ) ;
visit[0][pos.y] = true ;
before[0][pos.y] = ( struct way ) { pos.x , pos.y,1 } ;
}
if ( !visit[pos.x][0] && 0 != pos.y ) //case 2 : DROP(2)
{
Modify ( & next , pos.x , 0 , pos.step + 1 ) ;
q.push ( next ) ;
visit[pos.x][0] = true ;
before[pos.x][0] = ( struct way ) { pos.x , pos.y , 2 } ;
}
if ( !visit[A][pos.y] && A != pos.x ) //case 3 : FILL(1)
{
Modify ( & next , A , pos.y , pos.step + 1 ) ;
q.push ( next ) ;
visit[A][pos.y] = true ;
before[A][pos.y] = ( struct way ) { pos.x , pos.y , 3 } ;
}
if ( !visit[pos.x][B] && B != pos.y ) //case 4 : FILL(2)
{
Modify ( & next , pos.x , B , pos.step + 1 ) ;
q.push ( next ) ;
visit[next.x][next.y] = true ;
before[pos.x][B] = ( struct way ) { pos.x , pos.y , 4 } ;
}
if ( pos.x > 0 && pos.y < B ) //case 5 : POUR(1,2)
{
int temp ;
temp = MIN ( pos.x , B - pos.y ) ;
if ( !visit[pos.x-temp][pos.y+temp] )
{
Modify ( & next , pos.x - temp , pos.y + temp , pos.step + 1 ) ;
q.push ( next ) ;
visit[next.x][next.y] = true ;
before[next.x][next.y] = ( struct way ) {pos.x , pos.y,5} ;
}
}
if ( pos.x < A && pos.y > 0 ) //case 6: POUR(2,1)
{
int temp ;
temp = MIN ( pos.y , A - pos.x ) ;
if ( !visit[pos.x+temp][pos.y-temp] )
{
Modify ( & next , pos.x + temp , pos.y - temp , pos.step + 1 ) ;
q.push ( next ) ;
visit[next.x][next.y] = true ;
before[next.x][next.y] = (struct way ){pos.x , pos.y , 6} ;
}
}
}
return false ;
} int
main ( )
{
int A ,B , C ;
while ( EOF != scanf ("%d%d%d" , & A , & B , & C ) )
{
if ( !BFS ( A , B , C ) )
{
printf ("impossible\n") ;
}
memset ( visit , 0 , sizeof ( visit ) ) ;
}
return 0 ;
}

POJ 3414 Pots ( BFS , 打印路径 )的更多相关文章

  1. POJ 3414 Pots bfs打印方案

    题目: http://poj.org/problem?id=3414 很好玩的一个题.关键是又16ms 1A了,没有debug的日子才是好日子.. #include <stdio.h> # ...

  2. poj 3414 Pots(bfs+输出路径)

    Description You are given two pots, having the volume of A and B liters respectively. The following ...

  3. POJ 3414 Pots(BFS)

    Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Description You are g ...

  4. POJ - 3414 Pots BFS(著名倒水问题升级版)

    Pots You are given two pots, having the volume of A and B liters respectively. The following operati ...

  5. POJ 3414 Pots (BFS/DFS)

    Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7783   Accepted: 3261   Special Ju ...

  6. poj 3414 Pots bfs+模拟

    #include<iostream> #include<cstring> #define fillA 1 #define pourAB 2 #define dropA 3 #d ...

  7. poj 3414 Pots 【BFS+记录路径 】

    //yy:昨天看着这题突然有点懵,不知道怎么记录路径,然后交给房教了,,,然后默默去写另一个bfs,想清楚思路后花了半小时写了120+行的代码然后出现奇葩的CE,看完FAQ改了之后又WA了.然后第一次 ...

  8. 广搜+输出路径 POJ 3414 Pots

    POJ 3414 Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13547   Accepted: 5718   ...

  9. BFS POJ 3414 Pots

    题目传送门 /* BFS:六种情况讨论一下,BFS轻松解决 起初我看有人用DFS,我写了一遍,TLE..还是用BFS,结果特判时出错,逗了好长时间 看别人的代码简直是受罪,还好自己终于发现自己代码的小 ...

随机推荐

  1. printf 缓冲区问题

    突然发现printf的问题,看了这个很有意思,学习一下 转自:http://blog.csdn.net/shanshanpt/article/details/7385649 昨天在做Linux实验的时 ...

  2. 【BZOJ1036】【LCT版】树的统计Count

    Description 一 棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w.我们将以下面的形式来要求你对这棵树完成一些操作: I. CHANGE u t : 把结点u的权值改为t II. ...

  3. nginx 跨域。。。掉坑里了,小心

    今天公司产品一个功能突然挂掉了...向客户演示之前出现了,手机端显示不能获取下载资源,可是急坏了一票人.. 通过手机端,调查服务器地址调用了http:/2342342.domain.hostname. ...

  4. CentOS 忘记root密码,解决方法

    1.开机后,在倒数5秒结束前,按下任意键 2.在显示centos...的那个界面下,按e键(edit) 3.会出现三行的界面,选择中间 kernel...那行,然后按e键 4.在接着出现的那个界面最后 ...

  5. dedecms---------自由列表标题:网站地图自由列表

    列表HTML存放目录:{cmspath}/ 目录默认页名称:sitemap.xml 命名规则:{listdir}/sitemap.xml 列表模板:{style}/map.htm 循环内的单行记录样式 ...

  6. IE 6最小最大宽度与高度的写法

    最小最大宽度,最小最大高度,这是CSS很常见的一个要求.在现代浏览器中,一个 min-height,min-width 就可以解决问题,但是在IE系列,比如IE6则比较繁琐一点.下面总结一些IE 6下 ...

  7. 在Linux终端执行clear或top命令时出现:'xterm': unknown terminal type

    在Linux终端执行clear或top命令时出现:'xterm': unknown terminal type的错误. 例如: [root@localhost phpmyadmin]# clear ' ...

  8. Linux 使用yum install安装mysql登陆不上解决办法

    CentOS yum安装mysql后 Can’t connect to local MySQL server through socket ‘/var/lib/ CentOS Can’t connec ...

  9. 【Linux】常用命令 lsof查看打开的文件

    Linux系统把软硬件都抽象成文件,所以通过文件可以追踪到很多重要信息,如读取的配置文件.打开的端口等. 下面是常见的用法: 默认测试文件名为text.txt 1,显示打开text.txt的进程: l ...

  10. TransactionScope IsolationLevel 事务隔离级别

    事务有四个特性 第一原子性,事务具有独立的不能被拆分的,不能只做部分,事务的性质是要么全做,要么都不做. 第二统一性,在事务执行之前和事务执行之后的数据是一致. 第三隔离性,事务是独立的,开发者不能查 ...