POJ - 3414 Pots (BFS+路径记录)

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

题意:两个空杯子倒水,使得任意一个杯子中的水量达到题目要求,有6种操作:装满a,装满b,倒掉a,倒掉b,a->b,b->a。

题解:模拟一下操作,就好了。

 1 #include <queue>
 2 #include <cstring>
 3 #include <iostream>
 4 #include <algorithm>
 5 using namespace std;
 6
 7 const int maxn=111;
 8 string type[7]={"","FILL(1)","FILL(2)","DROP(1)","DROP(2)","POUR(1,2)","POUR(2,1)"};
 9 int a,b,c;
10 struct node{
11     int x,y;
12     int step;
13     int pre;
14     int idx;
15 }next[maxn*maxn];
16 int vis[maxn][maxn];
17
18 void print(int k){
19     if(next[k].pre!=-1){
20         print(next[k].pre);
21         cout<<type[next[k].idx]<<endl;
22     }
23 }
24
25 void bfs(){
26     queue <node> Q;
27     int r=1,w=0;
28     node now;
29     next[0].x=0;next[0].y=0;next[0].pre=-1;next[0].idx=0;next[0].step=0;
30     Q.push(next[0]);
31     vis[0][0]=1;
32     while(!Q.empty()){
33         now=Q.front();Q.pop();
34         if(now.x==c||now.y==c) {cout<<now.step<<endl;print(now.pre);cout<<type[now.idx]<<endl;return ;}
35         for(int i=1;i<=6;i++){
36             next[r].x=now.x;next[r].y=now.y;next[r].idx=i;
37             if(i==1) next[r].x=a;
38             if(i==2) next[r].y=b;
39             if(i==3) next[r].x=0;
40             if(i==4) next[r].y=0;
41             if(i==5){//1-2
42                 int tmp=b-next[r].y;
43                 if(tmp>=next[r].x) {next[r].y+=next[r].x;next[r].x=0;}
44                 else  {next[r].y=b;next[r].x-=tmp;}
45             }
46             if(i==6){//2-1
47                 int tmp=a-next[r].x;
48                 if(tmp>=next[r].y) {next[r].x+=next[r].y;next[r].y=0;}
49                 else {next[r].x=a;next[r].y-=tmp;}
50             }
51             if(!vis[next[r].x][next[r].y]){
52                 vis[next[r].x][next[r].y]=1;
53                 next[r].pre=w;
54                 next[r].step=now.step+1;
55                 Q.push(next[r]);
56                 r++;//一直往外扩散
57             }
58         }
59         w++;//这个相当于从这个点分散出去,在纸上模拟一遍就出来了
60     }
61     cout<<"impossible"<<endl;
62 }
63
64 int main(){
65     memset(vis,0,sizeof(vis));
66     cin>>a>>b>>c;
67     bfs();
68     return 0;
69 }
时间: 2024-10-11 07:39:33

POJ - 3414 Pots (BFS+路径记录)的相关文章

poj 3414 pots (bfs+路径记录)

Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11703   Accepted: 4955   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)        f

POJ 3414 Pots (BFS + 记录路径)

Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10300 Accepted: 4362 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 th

POJ 3414 Pots(bfs打印路径)

题意:给定两个空桶的容量分别为A.B,经过6种操作使获得C升水,求最少操作数: 思路:广搜.最少操作数很简单,练习一下打印路径:打印最短路劲就是每次记录当前状态和上一步,找到终点后查找路径. #include<cstdio> #include<cstring> #include<algorithm> using namespace std; #define INF 0x3f3f3f3f int A,B,C; int shortest[150][150];//更新最短步数

POJ 3414 Pots(BFS 倒水)

题意  你有两个容积分别为a,b杯子  你每次可以将某个杯子中的水倒满或者倒掉或者倒到另一个杯子  问能否通过这两个杯子量出c容量的水 和上一个倒可乐问题类似  只是这个操作更多了点  将两个杯子中各含有的水作为状态  每出队列一个状态  将所有可能到达的状态入队  直到有一个杯子里面水的体积为c   打印路径直接递归就行了 #include <map> #include <cstdio> #include <cstring> using namespace std;

poj 3414 Pots(BFS)(简单题)

Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11551   Accepted: 4900   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)        f

POJ 3414 Pots (BFS/DFS)

Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7783   Accepted: 3261   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)        fi

广搜+输出路径 POJ 3414 Pots

POJ 3414 Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13547   Accepted: 5718   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)

POJ 3414 Pots (经典bfs )

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

POJ 3414 Pots(罐子)

p.MsoNormal { margin-bottom: 10.0000pt; font-family: Tahoma; font-size: 11.0000pt } h1 { margin-top: 5.0000pt; margin-bottom: 5.0000pt; text-align: left; font-family: 宋体; font-weight: bold; font-size: 24.0000pt } span.10 { font-family: "Times New Rom

迷宫bfs+路径记录

给定5*5的地图,1表示墙,0表示空地,从左上角走到左下角 是把所有的可走路径都记录下来了,但是 搜索有递归与非递归形式 本代码使用非递归形式 bfs+路径记录 对于num[i].pre=head的理解是他存在很多条路,每个点是从上一个点走过来的,但总存在一条路是到达终点的,所以,只需要得到到终点的一个head就可以顺着这个路径标记回去了 #include <iostream> #include <cstdio> using namespace std; char a[10][10