题目: http://poj.org/problem?id=3414

很好玩的一个题。关键是又16ms 1A了,没有debug的日子才是好日子。。

 #include <stdio.h>
#include <string.h>
#include <queue>
#include <algorithm>
#include <stack>
using namespace std;
int a, b, c;
bool vis[][];
enum Ways{FILL_A, FILL_B, DROP_A, DROP_B, POUR_AB, POUR_BA}; struct Path
{
int px, py;
Ways way;
}path[][]; struct status
{
int x, y, step;
}; void print_path(int x, int y)
{
stack<struct Path>s;
while(!(x == && y == ))
{
s.push(path[x][y]);
int tx = path[x][y].px;
int ty = path[x][y].py;
x = tx;
y = ty;
}
while(!s.empty())
{
switch(s.top().way)
{
case FILL_A: printf("FILL(1)\n");break;
case FILL_B: printf("FILL(2)\n");break;
case DROP_A: printf("DROP(1)\n");break;
case DROP_B: printf("DROP(2)\n");break;
case POUR_AB: printf("POUR(1,2)\n");break;
case POUR_BA: printf("POUR(2,1)\n");break;
}
s.pop();
}
} queue<struct status>q;
void bfs()
{
while(!q.empty())
{
struct status u = q.front();
q.pop();
if(u.x == c || u.y == c)
{
printf("%d\n", u.step);
print_path(u.x, u.y);
return;
} if(u.x < a && !vis[a][u.y])
{
q.push((struct status){a, u.y, u.step+});
vis[a][u.y] = ;
path[a][u.y] = (struct Path){u.x, u.y, FILL_A};
} if(u.y < b && !vis[u.x][b])
{
q.push((struct status){u.x, b, u.step+});
vis[u.x][b] = ;
path[u.x][b] = (struct Path){u.x, u.y, FILL_B};
} if(u.x > && !vis[][u.y])
{
q.push((struct status){, u.y, u.step+});
vis[][u.y] = ;
path[][u.y] = (struct Path){u.x, u.y, DROP_A};
} if(u.y > && !vis[u.x][])
{
q.push((struct status){u.x, , u.step+});
vis[u.x][] = ;
path[u.x][] = (struct Path){u.x, u.y, DROP_B};
} if(u.x > && u.y < b)
{
int tmp = min(u.x, b-u.y);
if(!vis[u.x-tmp][u.y+tmp])
{
q.push((struct status){u.x-tmp, u.y+tmp, u.step+});
vis[u.x-tmp][u.y+tmp] = ;
path[u.x-tmp][u.y+tmp] = (struct Path){u.x, u.y, POUR_AB};
}
} if(u.x < a && u.y > )
{
int tmp = min(a-u.x, u.y);
if(!vis[u.x+tmp][u.y-tmp])
{
q.push((struct status){u.x+tmp, u.y-tmp, u.step+});
vis[u.x+tmp][u.y-tmp] = ;
path[u.x+tmp][u.y-tmp] = (struct Path){u.x, u.y, POUR_BA};
}
}
}
printf("impossible\n");
} int main()
{
scanf("%d %d %d", &a, &b, &c);
memset(vis, , sizeof(vis));
q.push((struct status){, , });
vis[][] = ;
bfs();
return ;
}

POJ 3414 Pots bfs打印方案的更多相关文章

  1. POJ 3414 Pots ( BFS , 打印路径 )

    题意: 给你两个空瓶子,只有三种操作 一.把一个瓶子灌满 二.把一个瓶子清空 三.把一个瓶子里面的水灌到另一个瓶子里面去(倒满之后要是还存在水那就依然在那个瓶子里面,或者被灌的瓶子有可能没满) 思路: ...

  2. POJ 3414 Pots(BFS)

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

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

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

  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. BFS POJ 3414 Pots

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

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

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

随机推荐

  1. iOS数据处理之SQLite数据库

    1. 数据库管理系统 1> SQL语言概述 SQL: SQL是Structured Query Language(结构化查询语言)的缩写.SQL是专为数据库而建立的操作命令集, 是一种功能齐全的 ...

  2. mono & apache install

    1.red hat 6安装完后网卡是默认不启动的 作为双生兄弟的CENTOS同样如是 第一步 设置网卡开机启动 进入 路径此目录下修改网卡配置文件 如果网卡驱动正常 会有如下文件 只要修改 ifcfg ...

  3. c#的方法重写和的java方法重写有什么区别

    java code: package example; class m1 { public int getInt() { return 0; } } class m2 extends m1 { pub ...

  4. Java json设置时间格式,Jackson设置时间格式,json设置单引号

    Java json设置时间格式,Jackson设置时间格式,json设置单引号 >>>>>>>>>>>>>>> ...

  5. 对进度条progressbar的调整

    进度条的理解,感觉这个进度条不是那么简单,系统给我们定制了几个普通的,但是如果还需要有更加好的效果,需要自己去调试. <ProgressBar android:layout_width=&quo ...

  6. Android Toast 设置到屏幕中间,自定义Toast的实现方法,及其说明

    http://blog.csdn.net/wangfayinn/article/details/8065763 Android Toast用于在手机屏幕上向用户显示一条信息,一段时间后信息会自动消失. ...

  7. C#微信公众号开发 -- (一)开发之前的准备

    本系列文章讲述的是利用C#语言开发微信公众号的实例教程,主要是服务号的开发(因为订阅号不能获取微信开发的高级接口) 想要开发微信服务公众号,首先必须要有一个认证的微信服务号,这样才能够使用微信提供的所 ...

  8. java gui可见即可得

    http://www.eclipse.org/windowbuilder/ http://download.eclipse.org/windowbuilder/WB/release/R20130927 ...

  9. SqlServer 由于未在SqlServer的此实例上安装复制组件解决方法

    sqlserver2005在复制订阅时出现: “由于未在SqlServer的此实例上安装复制组件,Microsoft SQL server 无法访问这些组件,请参阅SQL Server……” 解决方法 ...

  10. ios Toll-Free Bridging

    有一些数据类型是能够在 Core Foundation Framework 和 Foundation Framework 之间交换使用的.这意味着,对于同一个数据类型,你既可以将其作为参数传入 Cor ...