倒水——bfs
来源:互联网 发布:八爪鱼客户端软件 编辑:程序博客网 时间:2024/06/06 04:47
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 100 Accepted Submission(s) : 30
Special Judge
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 potj is full (and there may be some water left in the poti), or the poti is empty (and all its contents have been moved to the potj).
Write a program to find the shortest possible sequence of these operations that will yield exactlyC liters of water in one of the pots.
3 5 4
6FILL(2)POUR(2,1)DROP(1)POUR(2,1)FILL(2)POUR(2,1)#include<iostream>#include<string.h>#include<queue>using namespace std;
int a,b,c;int logal[1000][1000]={0};struct dalao{ int mla; int mlb; int step; string lalala[2000];};dalao t1,t2;
int bfs(){ memset(logal,0,sizeof(logal));
queue<dalao>k;
t1.mla=0; t1.mlb=0; t1.step=0; //t1.lalala[0]=""; k.push(t1); logal[0][0]=1; //int y=0; while(k.empty()==0) { // cout<<y++<<endl;
//dalao j; t1=k.front(); //cout<<t1.mla<<" "<<t1.mlb<<" "<<" step"<<t1.step<<endl;
k.pop(); if(t1.mla==c||t1.mlb==c) { cout<<t1.step<<endl; for(int i=1;i<=t1.step;i++) { cout<<t1.lalala[i]<<endl; } return 1; } if(t1.mla==0) { t2=t1; t2.mla=a; t2.step++; t2.lalala[t2.step]="FILL(1)"; if(logal[t2.mla][t2.mlb]==0) { logal[t2.mla][t2.mlb]=1; k.push(t2); } } if(t1.mla>0&&t1.mla<=a) { t2=t1; t2.mla=0; t2.step++; t2.lalala[t2.step]="DROP(1)"; if(logal[t2.mla][t2.mlb]==0) { logal[t2.mla][t2.mlb]=1; k.push(t2); } }
if(t1.mlb<b&&t1.mla!=0) { t2=t1; t2.step++; if(t2.mla+t2.mlb<=b) { t2.mlb+=t2.mla; t2.mla=0; } else { t2.mla=t2.mla+t2.mlb-b; t2.mlb=b; } t2.lalala[t2.step]="POUR(1,2)"; if(logal[t2.mla][t2.mlb]==0) { logal[t2.mla][t2.mlb]=1; k.push(t2); } }
if(t1.mlb==0) { //cout<<"****"<<endl; t2=t1; t2.mlb=b; t2.step++; t2.lalala[t2.step]="FILL(2)"; if(logal[t2.mla][t2.mlb]==0) { logal[t2.mla][t2.mlb]=1; k.push(t2); }
} if(t1.mlb>0&&t1.mlb<=b)// { t2=t1; t2.mlb=0; t2.step++; t2.lalala[t2.step]="DROP(2)"; if(logal[t2.mla][t2.mlb]==0) { logal[t2.mla][t2.mlb]=1; k.push(t2); }
}
if(t1.mla<a&&t1.mlb!=0)//这里要特别注意,不要写错顺序 {
t2=t1; t2.step++; if(t2.mla+t2.mlb<=a) { t2.mla+=t2.mlb; t2.mlb=0; } else {
t2.mlb=t2.mla+t2.mlb-a; t2.mla=a;
}
t2.lalala[t2.step]="POUR(2,1)"; if(logal[t2.mla][t2.mlb]==0) { logal[t2.mla][t2.mlb]=1; k.push(t2); } }
} return 0;}int main(){ //int a,b,c; while(cin>>a>>b>>c) { //memset(logal,0,sizeof(logal)); int ans=bfs(); if(ans==0) cout<<"impossible"<<endl;
} return 0;
}
- 倒水——bfs
- 水杯倒水问题 bfs
- uva 10603 倒水 bfs
- 20130421.4 倒水问题(BFS)
- UVA10603 倒水问题 BFS
- codevs1226 倒水问题 bfs
- pots 倒水问题(bfs)
- 7.5.2倒水问题(BFS)
- POJ 3414 Pots(BFS 倒水)
- poj 3414 倒水问题 bfs
- 【codevs1226】倒水问题,BFS练习
- hdu1495 bfs解决倒水问题
- 算法学习-倒水问题(bfs)
- hdu_round1-1005 yyf倒水(bfs)
- (转载)BFS 倒水问题 HDU 1495倒水问题
- 二进制乱搞——Luogu1582 倒水
- POJ 3414 Pots(BFS倒水问题)
- poj 3414 Pots [bfs - 倒水问题]
- KMP(不懂的仔细看看,很有意思)
- 移动宽带设置路由器连不上网?
- POJ
- 从原理上来浅谈 CORS
- 【系统学习SpringBoot】初见Thymeleaf之SpringBoot访问项目中的 jsp/html
- 倒水——bfs
- 通俗易懂讲解HMM(隐马尔可夫模型)
- 利用Theme自定义Activity间的切换动画
- 2017.8.5暑假集训第六天
- [Spring Boot] 1. Spring Boot启动过程源码分析
- 随机快速排序的代码实现
- C++中变量的存储类别和属性
- 维普 _自动导出xml
- Hihicoder 题目1 : Trie树 经典字典树