一个c++的动态规划算法

来源:互联网 发布:2017网络理财黑名单 编辑:程序博客网 时间:2024/04/29 11:27

main.cpp

实现动态规划,最短路径。

main.cpp

#include <iostream>#include <fstream>#include <string>#include <vector>#include <algorithm>#include <map>#include <set>#include <cmath>#include <inttypes.h>#include <boost/lexical_cast.hpp>#include <boost/algorithm/string.hpp>#include "Matrix615.h"using namespace std;void usage(){cout << " Dynamic-MTP <input.matrix> " << endl;exit(0);}int main(int argc,char **argv){int nrow = 5;int ncol = 5;if (argc < 2) {usage();}string matrix_file = argv[optind++];Matrix615<int> hw(nrow,ncol-1),vw(nrow-1,ncol);// initiate 2 matrixes with all zeroLoad_matrix(matrix_file,hw,vw,nrow,ncol);Matrix615<int> cost(nrow,ncol),move(nrow,ncol);int optCost = optimalCost(hw,vw,cost,move,nrow-1,ncol-1);cout << "Optimal Cost is " << optCost << endl;return 0;}


Matrix615.h

#ifndef MATRIX651_H#define MATRIX651_H#include <iostream>#include <fstream>#include <string>#include <vector>#include <algorithm>#include <map>#include <set>#include <cmath>#include <inttypes.h>#include <boost/lexical_cast.hpp>#include <boost/algorithm/string.hpp>template<class T>class Matrix615{public:std::vector<std::vector <T> > data;Matrix615(int nrow,int ncol,T val = 0){data.resize(nrow);for (int i=0;i<nrow;++i){data[i].resize(ncol,val);}}int rowNums(){return (int)data.size();}int colNums(){return (data.size() == 0) ? 0 : (int)data[0].size();}};int  optimalCost(Matrix615<int>& hw,Matrix615<int>& vw,Matrix615<int>& cost,Matrix615<int>& move,int r,int c);void Load_matrix(std::string &matrix_file, Matrix615<int> &hw, Matrix615<int> &vw, int nrow, int ncol);#endif

MTP.cpp


#include "Matrix615.h"using namespace std;int optimalCost(Matrix615<int>& hw,Matrix615<int>& vw,Matrix615<int>& cost,Matrix615<int>& move,int r,int c){if (cost.data[r][c] == 0){if ( (r==0) && (c==0) ){cost.data[r][c] = 0;}else if (r==0){move.data[r][c] = 0;// I am in first row, only because I moved left before this stepcost.data[r][c] = optimalCost(hw,vw,cost,move,r,c-1) + hw.data[r][c-1];}else if (c==0){move.data[r][c] = 1;// I am in first col, only because I moved down before this stepcost.data[r][c] = optimalCost(hw,vw,cost,move,r-1,c) + hw.data[r-1][c];}else{int hcost = optimalCost(hw,vw,cost,move,r,c-1) + hw.data[r][c-1];int vcost = optimalCost(hw,vw,cost,move,r-1,c) + hw.data[r-1][c];if (hcost > vcost){cost.data[r][c] = vcost;move.data[r][c] = 1;}else {cost.data[r][c] = hcost;move.data[r][c] = 0;}}}return cost.data[r][c];}void Load_matrix(string &matrix_file,Matrix615<int> &hw, Matrix615<int> &vw, int nrow, int ncol){ifstreaminfile; infile.open(matrix_file.c_str());if (! infile ){cerr << "fail to open input file " << matrix_file << endl;exit(0);}string lineStr;while (getline(infile,lineStr,'\n')){if (lineStr[0] == '\n' || lineStr[0] == ' ' || lineStr[0] == '\t'){continue;}if (lineStr[0] == '#'){vector<string> lineVec;boost::split(lineVec,lineStr, boost::is_any_of(":, \t\n"), boost::token_compress_on);if (lineVec[0] == "#hw"){int __row_n = 0;getline(infile,lineStr,'\n');while (getline(infile,lineStr,'\n')){vector<string> f;boost::split(f,lineStr, boost::is_any_of(":, \t\n"), boost::token_compress_on);if (lineStr[0] == ' ' || lineStr[0] == '\n' || f[0] == "S\\S"){continue;}for (int __col_n=1; __col_n<f.size();++__col_n){hw.data[__row_n][__col_n-1] = boost::lexical_cast<int>(f[__col_n]);}++__row_n;if (__row_n > nrow - 1){break;}}}if (lineVec[0] == "#vw"){int __row_n = 0;getline(infile,lineStr,'\n');while (getline(infile,lineStr,'\n')){vector<string> f;boost::split(f,lineStr, boost::is_any_of(":, \t\n"), boost::token_compress_on);if (lineStr[0] == ' ' || lineStr[0] == '\n' || f[0] == "S\\S"){continue;}for (int __col_n=1; __col_n<f.size();++__col_n){vw.data[__row_n][__col_n-1] = boost::lexical_cast<int>(f[__col_n]);}++__row_n;if (__row_n > nrow - 2){break;}}}}}infile.close();}


Makefile 编译:


cc=g++exe=Dynamic-MTPobj=main.o MTP.o$(exe):$(obj)$(cc) -o $(exe) $(obj)main.o:main.cpp Matrix615.h$(cc) -c main.cppMTP.o:MTP.cpp Matrix615.h$(cc) -c MTP.cppclean:rm -rf *.o $(exe)






0 0