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

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 zero

	Load_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 step
			cost.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 step
			cost.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){
	ifstream	infile;
 	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-MTP
obj=main.o MTP.o

$(exe):$(obj)
	$(cc) -o $(exe) $(obj)
main.o:main.cpp Matrix615.h
	$(cc) -c main.cpp
MTP.o:MTP.cpp Matrix615.h
	$(cc) -c MTP.cpp
clean:
	rm -rf *.o $(exe)






  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值