There are total N stations in a Metro Line.Those stations can be divided into three types: U(ground),D(underground) and C(composite).The construction cost of each station differs throughout the Metro Line. It should be noted that the adjacent station type can not be repeated. Given the construction costs of three types for each station,you are required to find out the minimum cost of constructing this Metro Line.For example,
U | D | C | |
Station1 | 1 | 9 | 9 |
Station2 | 9 | 1 | 9 |
Station3 | 9 | 9 | 1 |
#include <iostream>
#include <math.h>
using namespace std;
#define N 3
int minCost(int i,int j,int *p)
{
int result=*(p+i*3+j);
if(i<N-1)
{
result+=min(minCost(i+1,(j+1)%3,p),
minCost(i+1,(j+2)%3,p));
}
return result;
}
int main()
{
int stationCost[N*3]={1,9,9,9,1,9,9,9,1};
int a=minCost(0,0,stationCost);
int b=minCost(0,1,stationCost);
int c=minCost(0,2,stationCost);
int result=min(min(a,b),c);
cout<<result<<endl;
return 0;
}