Pots--(dfs)
来源:互联网 发布:中国邮政数据传媒中心 编辑:程序博客网 时间:2024/06/06 04:59
Problem 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 pot i to pot j; after this operation either the pot j is full (and there may be some water left in the pot i), or the pot i is empty (and all its contents have been moved to the pot j).
Write a program to find the shortest possible sequence of these operations that will yield exactly C liters of water in one of the pots.
Input
<p>On the first and only line are the numbers <b>A</b>, <b>B</b>, and <b>C</b>. These are all integers in the range from 1 to 100 and <b>C</b>≤max(<b>A</b>,<b>B</b>).</p>
Output
<p>The first line of the output must contain the length of the sequence of operations <b>K</b>. The following <b>K</b> lines must each describe one operation. If there are several sequences of minimal length, output any one of them. If the desired result can’t be achieved, the first and only line of the file must contain the word ‘<b>impossible</b>’.</p>
Sample Input
3 5 4
Sample Output
6FILL(2)POUR(2,1)DROP(1)POUR(2,1)FILL(2)POUR(2,1)
题意:给出两个容量为A和B的桶,问是否能够通过一系列操作使得某个桶内水的体积能达到C,
解题思路:感觉好像dfsbfs都行,不过dfs代码好敲,一直还在害怕dfs会超时,这个题目错了几次错在数组的大小上
代码:
#include<iostream>#include<string>#include<cstdio>#include<algorithm>#include<cmath>#include<iomanip>#include<queue>#include<cstring>using namespace std;int aa,bb,cc,num;bool flag,ok[201][201]; //判断是否出现过当前体积状态string s[1000],ss[1000]; //s用来dfs,ss存放最终结果void dfs(int a,int b,int step) //a,b是当前体积,step是步数,因为没有一个固定的结束,用step来控制回溯{ if(a==cc||b==cc) { flag=true; if((step-1)<num) //组成c后还要判断是不是最短路径 { num=step-1; for(int i=1;i<step;i++) ss[i]=s[i]; } return; } if(step>4000) { return; } if(ok[a][bb]==false) { ok[a][bb]=true; s[step]="FILL(2)"; dfs(a,bb,step+1); ok[a][bb]=false; } if(ok[aa][b]==false) { ok[aa][b]=true;; s[step]="FILL(1)"; dfs(aa,b,step+1); ok[aa][b]=false; } if(!ok[a][0]) { ok[a][0]=true; s[step]="DROP(2)"; dfs(a,0,step+1); ok[a][0]=false; } if(!ok[0][b]) { ok[0][b]=true; s[step]="DROP(1)"; dfs(0,b,step+1); ok[0][b]=false; } int a1=a,b1=b; if((b1+a1)<=bb) {b1=b1+a1; a1=0;} //a桶倒光了 else {a1=a1-(bb-b1); b1=bb;} //b桶倒满了 if(!ok[a1][b1]) { ok[a1][b1]=true; s[step]="POUR(1,2)"; dfs(a1,b1,step+1); ok[a1][b1]=false;; } a1=a,b1=b; if((b1+a1)<=aa) {a1=b1+a1; b1=0;} else {b1=b1-(aa-a1); a1=aa;} if(!ok[a1][b1]) { ok[a1][b1]=true; s[step]="POUR(2,1)"; dfs(a1,b1,step+1); ok[a1][b1]=false;; }}int main(){ scanf("%d%d%d",&aa,&bb,&cc); memset(ok,false,sizeof(ok)); num=10000000; flag=false; dfs(0,0,1); if(flag) { printf("%d\n",num); for(int i=1;i<=num;i++) cout<<ss[i]<<endl;; } else { printf("impossible\n"); } return 0;}
阅读全文
0 0
- Pots--(dfs)
- POJ 3414 Pots(简单模拟+dfs)
- POJ 3414 Pots (DFS || BFS)
- POJ 3414 Pots DFS
- poj 3414 Pots (DFS)
- poj3414 Pots【dfs】
- POJ 3414 Pots(BFS)||SDUT(Pots)
- (BFS)3414 Pots
- POJ 3414(Pots)
- POJ3414(Pots)
- pots(BFS)
- Pots(BFS)
- Pots(POJ 3414)
- POJ3414 Pots(BFS)
- 【POJ3414】Pots(BFS)
- poj_3414 Pots(bfs)
- Pots (模拟倒水)
- Pots (bfs)
- 给自定义控件中的子控件自定义点击事件
- 欢迎使用CSDN-markdown编辑器
- MongoDB七(索引)
- SPOJ ORDERSET Order statistic set 简单平衡树 或 树状数组
- mac mysql 以及重置密码
- Pots--(dfs)
- 微信小程序-滚动消息通知效果
- Python_11
- C++变量存储类型与标识符生命期
- UVa1600 习题6-5 巡逻机器人 (Patrol Robot,ACM/ICPC Hanoi 2006)
- 基于模板匹配的字符识别(Matlab) 字符识别模板匹配方法
- JavaScript基础学习
- MST 最小生成树
- androidstudio报错之Java.lang.NoClassDefFoundError: de.greenrobot.event.EventBus