1번째 행부터 시작해서 column을 순차적으로 돌면서 dp값을 갱신한다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<cstdio> | |
#include<vector> | |
#define MAX 10000000 | |
using namespace std; | |
int offset[3][4][2] = { { {-1,0}, {-1, 1} }, { {0,-1},{-1,-1},{-1,0},{-1,1} }, { {0,-1},{-1,-1}, {-1,0} } }; | |
int move_len[] = {2,4,3}; | |
int min(int a,int b){ | |
if (a<b) return a; | |
else return b; | |
} | |
int main(void) | |
{ | |
int N; | |
int loop = 0; | |
scanf("%d",&N); | |
while (N != 0){ | |
vector<vector<int>> arr(N,vector<int>(3,0)); | |
for (int i=0;i<N;i++){ | |
for (int j=0; j<3; j++) | |
scanf("%d",&arr[i][j]); | |
} | |
vector<vector<int>> dp(N,vector<int>(3,0)); | |
dp[0][1] = arr[0][1]; | |
dp[0][2] = arr[0][1]+arr[0][2]; | |
for (int i=1;i<N;i++){ | |
for (int j=0; j<3; j++) | |
{ | |
dp[i][j] = MAX; | |
for (int k=0;k<move_len[j];k++){ | |
int x_cor = i+offset[j][k][0]; | |
int y_cor = j+offset[j][k][1]; | |
if (x_cor == 0 && y_cor == 0) | |
continue; | |
dp[i][j] = min(dp[x_cor][y_cor] + arr[i][j],dp[i][j]); | |
} | |
} | |
} | |
printf("%d. %d\n",++loop,dp[N-1][1]); | |
scanf("%d",&N); | |
} | |
} |
댓글
댓글 쓰기