Pots(bfs+路径记录与回溯)
来源:互联网 发布:c语言手册下载 编辑:程序博客网 时间:2024/06/18 15:20
F - Pots
POJ - 3414其实是一道很经典但是看起来难,其实没那么难的一道bfs+路径回溯题目
bfs是基础,而对于路径回溯刚开始那么清楚,其实发现是一个很简单的问题标记路径就可以解决
#include <iostream>#include <cstdio>#include <cstring>#include <queue>#include <algorithm>using namespace std;struct node{ int x,y; int step;}st,ed;int a,b,c;int book[105][105];int pre_x[105][105];int pre_y[105][105];char opr[][10]={"","DROP(1)","DROP(2)","FILL(1)","FILL(2)","POUR(1,2)","POUR(2,1)"};int flag;void pri(int x,int y){//printf("%d %d %d\n",book[x][y],x,y);if(book[x][y]==0||book[x][y]==-1){return ;}pri(pre_x[x][y],pre_y[x][y]);printf("%s\n",opr[book[x][y]]);return ;}void bfs(){memset(book,0,sizeof(book));memset(pre_x,0,sizeof(pre_x));memset(pre_y,0,sizeof(pre_y)); st.x=0;st.y=0;st.step=0; book[st.x][st.y]=-1; queue<node>Q; Q.push(st); while(!Q.empty()) { st=Q.front(); Q.pop(); if(st.x==c||st.y==c) { flag=1; printf("%d\n",st.step); pri(st.x,st.y); return ; } for(int i=1;i<=6;i++) { if(i==1) { ed.x=0;ed.y=st.y; } if(i==2) { ed.x=st.x;ed.y=0; } if(i==3) { ed.x=a;ed.y=st.y; } if(i==4) { ed.x=st.x;ed.y=b; } if(i==5)//x->y { if(st.x<=b-st.y) { ed.x=0;ed.y=st.y+st.x; } else if(st.x>b-st.y) { ed.x=st.x-(b-st.y);ed.y=b; } } if(i==6)//y->x { if(a-st.x>=st.y) { ed.y=0;ed.x=st.y+st.x; } else if(a-st.x<st.y) { ed.x=a;ed.y=st.y-(a-st.x); } } ed.step=st.step+1; if(book[ed.x][ed.y]!=0) { continue; } //printf("%d %d %d\n",ed.x,ed.y,i); book[ed.x][ed.y]=i; pre_x[ed.x][ed.y]=st.x; pre_y[ed.x][ed.y]=st.y; Q.push(ed); } } return ;}int main(){ scanf("%d %d %d",&a,&b,&c);bfs(); if(!flag){printf("impossible\n");} return 0;}
1 0
- Pots(bfs+路径记录与回溯)
- POJ 3414--Pots(BFS+回溯路径)
- poj 3414 Pots (bfs+路径记录)
- POJ 3414 Pots(BFS记录路径)
- poj 3414 Pots (BFS + 记录路径)
- POJ 3414 Pots(bfs+路径记录)
- poj3414--Pots(bfs,记录路径)
- POJ---- Pots ----BFS的路径回溯问题
- poj Pots(BFS)(路径记录)(模拟)
- POJ3414 Pots(路径记录,广搜BFS,模拟)
- POJ 3414 Pots【BFS+记录路径】
- POJ 3414 Pots (BFS + 记录路径)
- poj-3414-Pots【BFS+记录路径】
- POJ 3414 Pots(模拟bfs+路径记录)
- 【POJ 3414】 Pots (bfs + 模拟 + 路径记录)
- POJ 3414 Pots(BFS+回溯)
- POJ 3414 Pots ( BFS , 打印路径 )
- POJ -3414 Pots(BFS + 打印路径)
- JVM基本概念和基本垃圾回收(GC)算法
- 高效率工具
- 【数位DP】DP special train T2 山峰数 题解
- 算法训练 出现次数最多的整数
- OpenCV学习笔记七:几何变换
- Pots(bfs+路径记录与回溯)
- 动画
- Bzoj 1115: [POI2009]石子游戏Kam
- 一个列子演示java中软引用的回收时机
- 2956 排队问题
- 数据库设计中的范式形式
- Ubuntu 14.04 LTS 搜狗拼音输入法不能使用的情况
- python -- 学习资料
- session用法解答,转载自http://www.jb51.net/article/42500.htm