SRM 582 Div II Level Three: ColorTheCells, Brute Force 算法
来源:互联网 发布:长沙牛耳软件学院 编辑:程序博客网 时间:2024/05/14 13:12
题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12581
Burte Force 算法,求解了所有了情况,注意 next_permutation 函数的用法。
#include <iostream>#include <vector>#include <limits>#include <algorithm>using namespace std;class ColorTheCells{private:vector <int> dryingTime;/* 需要干燥的时间 */vector <long> dryedTime;/* 正好干燥的时间,0表示还没有paint */vector <int> paint_order;/* paint格子的顺序 */public:long calc(int seq);int minimalTime(vector <int> dryingTime);};#define MOVE_TIME 1L#define PAINT_TIME 1L#define INFINITY_TIME LONG_MAX /* 参考Ueddy代码实现 */int ColorTheCells::minimalTime(vector<int> dryingTime){int num = dryingTime.size();long minTime = INFINITY_TIME;int i, j;this->dryingTime = dryingTime;paint_order.clear();/* paint 的顺序 */dryedTime.clear();for (i = 0; i < num; i++) {paint_order.push_back(i);dryedTime.push_back(0);}/* brute force,枚举所有的情况,选出最小时间 */do {for (i = 0; i < (1<<num); i++) {for (j = 0; j < num; j++) {dryedTime[j] = 0;}minTime = min(minTime, calc(i));}} while (next_permutation(paint_order.begin(), paint_order.end()));return minTime;}/** * seq为一个序列,0表示在被paint位置左边paint, 1表示在右边paint,paint的顺序由 * paint_order指定。 */long ColorTheCells::calc(int seq){long time;/* 所用时间 */int num = dryingTime.size();int paint_dir;/* paint方向,在被paint位置左边还是右边paint */int cur_pos;/* 当前位置 */int paint_pos;/* paint位置,不是被paint的位置 */int bias;/* 从当前位置向左走还是向右走 */cur_pos = 0;time = 0;for (int i = 0; i < num; i++) {paint_dir = seq & 1;seq /= 2;paint_dir = paint_dir ? 1 : -1;paint_pos = paint_order[i] + paint_dir;if (paint_pos < 0 || paint_pos >= num) {/* 位置不合法 */return INFINITY_TIME;}bias = 1;/* 从当前位置向右走 */if (cur_pos - paint_pos > 0) {/* 从当前位置向左走 */bias = -1;}/* 用for循环到达paint位置,中间遇到还没有干燥的格子要等待 */for (; cur_pos != paint_pos; cur_pos += bias) {if (dryedTime[cur_pos+bias] > time) {/* 等待干燥 */time = dryedTime[cur_pos+bias];}time += MOVE_TIME;/* 移动时间 */}time += PAINT_TIME;/* 到达目的地,paint时间 */dryedTime[paint_order[i]] = time + dryingTime[paint_order[i]];/* 更新该格子干燥时间 */}return time;}
- SRM 582 Div II Level Three: ColorTheCells, Brute Force 算法
- SRM 212 Div II Level Three: LargestCircle,Brute Force
- SRM 212 Div II Level Two: WinningRecord,Brute Force
- Test SRM Level Three: LargestCircle, Brute Force
- SRM 207 Div II Level Three: CaptureThemAll,BFS算法
- SRM 585 Div II Level Three: EnclosingTriangleColorful
- Test SRM Level Two: CountExpressions, Brute Force
- SRM 583 Div II Level Three:GameOnABoard,Dijkstra最短路径算法
- SRM 584 Div Ii Level Three: Excavations2, Dynamic Programming
- SRM 578 Div II Level Three: WolfInZooDivTwo,Dynamic Programming,求教!
- SRM 582 Div II Level Two SpaceWarDiv2
- SRM 584 Div II Level Two: Egalitarianism,DFS,BFS算法
- SRM 586 Div II Level Three: StringWeightDiv2,Dynamic Programming or Math
- Brute-Force算法实现
- Brute-Force算法
- BF 算法(Brute Force)
- BF(Brute-Force)算法
- C++ Brute Force算法
- C#清除回收站
- 黑马程序员-笔记-01-基础
- C6455CSL芯片支持库:第一节 VCP2/TCP2 Viterbi译码 Turbo译码
- 关于allegro16.6 orCAD capture CIS 创建网表出现错误的问题解决方法
- linux/unix系统错误定义errno.h
- SRM 582 Div II Level Three: ColorTheCells, Brute Force 算法
- 黑马程序员-笔记-02-基础
- Android中启动其他Activity并返回结果
- matlab中size()函数的用法
- FTP探测下载ecos,cygwin等相关资料
- TimePicker点击改变时间时java.lang.NullPointerException异常
- Image与byte之间互转
- php语言基础学习笔记
- SRM 583 Div Level Two:IDNumberVerification