-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path프로그래머스_경주로_건설.cpp
44 lines (42 loc) · 1.43 KB
/
프로그래머스_경주로_건설.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <string>
#include <vector>
#include <iostream>
#define MAX 1000000000
using namespace std;
bool inline segCheck(int index,int jndex,int n){
return 0<=index && index<n && 0<=jndex && jndex<n;
}
string DireactionCal(int index,int jndex,int Nindex,int Njndex){
if(index==Nindex) return "-";
else return "|";
}
void dfs(vector<vector<int>> &board,vector<vector<int>> &dp,int n,int index,int jndex,string direaction="0"){
int Nindex,Njndex;
string NewDireaction="x";
for(int i=0;i<4;i++){
Nindex=index+"0121"[i]-'1';
Njndex=jndex+"1210"[i]-'1';
if(segCheck(Nindex,Njndex,n)&& board[Nindex][Njndex]==0){
NewDireaction=DireactionCal(index,jndex,Nindex,Njndex);
if(direaction==NewDireaction or direaction=="0"){
if(dp[index][jndex]+100<=dp[Nindex][Njndex]) {
dp[Nindex][Njndex]=dp[index][jndex]+100;
dfs(board,dp,n,Nindex,Njndex,NewDireaction);
}
}
else{
if(dp[index][jndex]+600<=dp[Nindex][Njndex]) {
dp[Nindex][Njndex]=dp[index][jndex]+600;
dfs(board,dp,n,Nindex,Njndex,NewDireaction);
}
}
}
}
}
int solution(vector<vector<int>> board) {
int n = board.size();
vector<vector<int>> dp(n,vector<int> (n,MAX));
dp[0][0]=0;
dfs(board,dp,n,0,0);
return dp[n-1][n-1];
}