Pots POJ
来源:互联网 发布:数控车床g32实例编程 编辑:程序博客网 时间:2024/05/17 21:12
一个比较麻烦一点的BFS
题意是有两个容量分别为A,B的空水杯,可以对他们进行如下操作:
1. 选择其中一个水杯倒满水
2. 选择其中一个水杯清空其中的水
3. 选择一个水杯,将其中的水倒入到另一个水杯,直到另一个水杯满或者选择的水杯没有水了
希望通过这些操作使其中的一个水杯中水量为C
整体依然是BFS,只是在寻找下一步可能情况的时候比较麻烦,我直接写了对应操作的三个函数,然后记录下当前节点的操作,和当前节点的父节点,寻得解之后回溯逆序输出答案即可
另外要注意无解的情况,不需要跑BFS,当C不能整除 A,B的公约数时一定无解。
注意BFS时也需要通过visit数组记录下当前已经实现了的情况,不然会超时
//leehaoze#include <iostream>#include <deque>#include <string>#include <vector>#include <queue>#include <cstdio>#include <stack>#include <algorithm>#include <cstring>#include <cctype>#include <cstdio>#include <cmath>#include <cstdlib>using namespace std;const int INF = 1<<29;#define INC_SAT( val ) (val = ((val)+1 > (val)) ? (val)+1 : (val))#define ARR_SIZE( a ) ( sizeof( (a) ) / sizeof( (a[0]) ) )#define ULL unsigned long longstruct Pot{ Pot(int s,int w):size_(s),water_(w){} Pot(const Pot&cp):size_(cp.size_),water_(cp.water_){} int size_; int water_;};struct Comb{ Comb(Pot A,Pot B,int step,int pre_order):A_(A),B_(B),step_(step),pre_order_(pre_order),father_(NULL){} Comb(const Comb&cp):A_(cp.A_),B_(cp.B_),step_(cp.step_){} Pot A_; Pot B_; int step_; int pre_order_; Comb *father_;};int A,B,C;bool visit[101][101];void Input(){ scanf("%d%d%d",&A,&B,&C); for (int i = 0; i < 101; ++i) { for (int j = 0; j < 101; ++j) { visit[i][j] = false; } }}int gcd(int a, int b) { return b ? gcd(b, a%b) : a;}bool Match(Comb &now){ return now.A_.water_ == C || now.B_.water_ == C;}void Fill(Comb &temp, int i){ if(i == 1){ temp.A_.water_ = temp.A_.size_; } else{ temp.B_.water_ = temp.B_.size_; }}void Drop(Comb &temp,int i){ if(i == 1){ temp.A_.water_ = 0; } else{ temp.B_.water_ = 0; }}void Pour(Comb &temp,int i){ if(i == 1){ int B_left = temp.B_.size_ - temp.B_.water_; if(temp.A_.water_ >= B_left){ temp.B_.water_ = temp.B_.size_; temp.A_.water_ -= B_left; } else{ temp.B_.water_ += temp.A_.water_; temp.A_.water_ = 0; } } else{ int A_left = temp.A_.size_ - temp.A_.water_; if(temp.B_.water_ >= A_left){ temp.A_.water_ = temp.A_.size_; temp.B_.water_ -= A_left; } else{ temp.A_.water_ += temp.B_.water_; temp.B_.water_ = 0; } }}void BFS(){ queue<Comb *> S; S.push(new Comb(Pot(A,0),Pot(B,0),0,-1)); while(!S.empty()){ Comb *now = S.front(); S.pop(); if(Match(*now)){ cout << now->step_ << endl; stack<string> ans; while(now != NULL){ switch (now->pre_order_){ case 0: ans.push("FILL(1)"); break; case 1: ans.push("FILL(2)"); break; case 2: ans.push("DROP(1)"); break; case 3: ans.push("DROP(2)"); break; case 4: ans.push("POUR(1,2)"); break; case 5: ans.push("POUR(2,1)"); break; default:break; } now = now->father_; } while(!ans.empty()){ cout << ans.top() << endl; ans.pop(); } return ; } for (int i = 0; i < 6; ++i) { Comb *temp = new Comb(*now); switch (i) { case 0: Fill(*temp, 1); break; case 1: Fill(*temp, 2); break; case 2: Drop(*temp, 1); break; case 3: Drop(*temp, 2); break; case 4: Pour(*temp, 1); break; case 5: Pour(*temp, 2); break; default: break; } temp->step_++; temp->pre_order_ = i; temp->father_ = now; if(!visit[temp->A_.water_][temp->B_.water_]) { S.push(temp); visit[temp->A_.water_][temp->B_.water_] = true; } } }}int main() {#ifdef LOCAL freopen("IN.txt", "r", stdin);#endif std::ios::sync_with_stdio(false); Input(); if (C % gcd(A, B) != 0) puts("impossible"); else BFS();}
0 0
- Pots POJ
- Pots (POJ
- Pots POJ
- Pots POJ
- Pots POJ
- Pots POJ
- POJ 3414 Pots
- poj 3414 pots
- POJ 3414 Pots
- POJ 3414 Pots BFS
- POJ 3414 Pots
- poj 3414 Pots
- poj 3414 Pots
- POJ 3414 Pots
- poj 3414 Pots
- POJ 3414(Pots)
- POJ 3414 Pots
- POJ 3414 Pots
- 判断区间内素数问题的Java表示
- hash与hashmap
- IntelliJ Idea 常用快捷键列表
- vncserver密码修改
- iOS 百度地图使用鹰眼时,出现image not found
- Pots POJ
- Java Socket 通信
- Linux环境下.vimrc的配置
- Bean的生命周期2
- c++版本的opencv函数
- 换了卡上不了4G网怎么办?
- c++实验3
- 微信消息的封装
- jQuery学习教程六: jQuery 效果