POJ 3414 BFS 模拟求最快方式
来源:互联网 发布:node.js 开发web的优势 编辑:程序博客网 时间:2024/06/06 04:01
点击打开链接
题意:给出两个容积分别为 a 和 b 的pot,按照以下三种操作方式,求出能否在一定步数后,使者两个pot的其中一个的水量为c。
1.FILL(i):将ipot倒满水。
2.DROP(i):将ipot倒空水。
3.POUR(i,j): 将ipot的水倒到jpot上,直至要么ipot为空,要么jpot为满。
bfs求最短路径
#include<iostream>#include<cstring>#include<cstdio>#include<cstdlib>#include<cmath>#include<algorithm>#include<queue>#include<set>#include<map>#define PI acos(-1)#define eps 0.00000001#define LL long longusing namespace std;struct node{ int next; int a,b,num,op;}p[1000010];int a,b,c,top;int acc[1000010];int vis[1000][1000],j;int BFS(){ queue<node>Q; node to; int top=0; p[top].a=0; ///记录第一个pot 存放的值 p[top].b=0; ///记录第一个pot 存放的值 p[top].next=-1; ///记录上一次pot 的下标 p[top].num=0; /// ///记录当前pot 的下标 p[top].op=-1; /// ///记录当前pot 是啥操作之后的结果 Q.push(p[top]); top++; memset(vis,0,sizeof(vis)); while(!Q.empty()) { node t=Q.front(); Q.pop(); if(vis[t.a][t.b]) continue; if(t.a==c||t.b==c) { j=0; int k=t.next; acc[j++]=t.op; while(p[k].next!=-1) { acc[j++]=p[k].op; k=p[k].next; } return 1; } vis[t.a][t.b]=1; for(int i=1;i<=6;i++) { if(i==1) { p[top].a=a; p[top].b=t.b; } if(i==2) { p[top].a=t.a; p[top].b=b; } if(i==3) { p[top].a=0; p[top].b=t.b; } if(i==4) { p[top].a=t.a; p[top].b=0; } if(i==5) { p[top].a=(t.a+t.b-b<0?0:(t.a-b+t.b)); p[top].b=(t.a+t.b-b<0?(t.a+t.b):b); } if(i==6) { p[top].a=(t.a+t.b-a<0?(t.a+t.b):a); p[top].b=(t.a+t.b-a<0?0:(t.a-a+t.b)); } p[top].next=t.num; p[top].op=i; p[top].num=top; Q.push(p[top++]); } } return 0;}int main(){ while(~scanf("%d%d%d",&a,&b,&c)) { top=0; int flag=BFS(); if(!flag) { printf("impossible\n"); continue; } printf("%d\n",j); for(int i=j-1;i>=0;i--) { if(acc[i]==1) printf("FILL(1)\n"); if(acc[i]==2) printf("FILL(2)\n"); if(acc[i]==3) printf("DROP(1)\n"); if(acc[i]==4) printf("DROP(2)\n"); if(acc[i]==5) printf("POUR(1,2)\n"); if(acc[i]==6) printf("POUR(2,1)\n"); } } return 0;}
1 0
- POJ 3414 BFS 模拟求最快方式
- poj 3414 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-3803(模拟Sed,BFS)
- POJ 3414 bfs求路径(建bfs树然后树上寻找路径)
- poj 1308 他四叔么? BFS+模拟
- poj 1175 starry night#BFS#模拟
- POJ 3087 Shuffle'm Up(BFS +模拟)
- poj 1606 Jugs and poj 3414 Pots(数组模拟BFS) uva 571 (数论)
- poj 2215 bfs求最短路
- POJ 3170(bfs求最短路)
- POJ 3414 Pots BFS
- poj 3414 搜索BFS
- ViewPager实现Tab布局
- MB466光驱位改下SSD硬盘
- 另一个前端优化
- GCD笔记和问题汇总
- 线程
- POJ 3414 BFS 模拟求最快方式
- C++学习笔记--STL(映射:map)
- 项目记录25--unity-tolua框架 View02---BasePanel.lua
- 数据库范式
- 产品经理必备软件——Axure使用详解(2)
- 《JAVA常用算法手册》 算法经典题 3.鸡兔同笼
- 2016 qut第一次训练赛简单题
- jdbc连接mysql的方法
- getScrollx/getScrollY的理解