티스토리 뷰
https://programmers.co.kr/learn/courses/30/lessons/67259
코딩테스트 연습 - 경주로 건설
[[0,0,0,0,0,0,0,1],[0,0,0,0,0,0,0,0],[0,0,0,0,0,1,0,0],[0,0,0,0,1,0,0,0],[0,0,0,1,0,0,0,1],[0,0,1,0,0,0,1,0],[0,1,0,0,0,1,0,0],[1,0,0,0,0,0,0,0]] 3800 [[0,0,1,0],[0,0,0,0],[0,1,0,1],[1,0,0,0]] 2100 [[0,0,0,0,0,0],[0,1,1,1,1,0],[0,0,1,0,0,0],[1,0,0,1,0,1],[
programmers.co.kr
BFS로 격자에 4개의 진행방향마다 최소값을 갱신하여 풀이하였습니다.
C++ 소스 코드
#include <string>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
const int dx[] = {0,0,-1,1}, dy[]={-1,1,0,0};
struct path {
int x,y,z;
};
int solution(vector<vector<int>> board) {
int answer;
int bSize = (int)board.size();
int dist[26][26][4];
fill(&dist[0][0][0], &dist[25][25][2]+1, 1000000);
queue<path> q;
q.push({0,0,1});
q.push({0,0,3});
dist[0][0][1]=0;
dist[0][0][3]=0;
while(!q.empty()){
int x = q.front().x, y = q.front().y;
int z = q.front().z; q.pop();
int lastcost = dist[x][y][z];
for(int k=0;k<4;k++){
int nx = x+dx[k], ny= y+dy[k];
if (nx <0 || nx >= bSize || ny <0 || ny>=bSize || board[nx][ny]) continue;
if (k==z){ //같은 방향으로 이동할 때
if (dist[nx][ny][k]> lastcost+100){
dist[nx][ny][k] =lastcost+100;
q.push({nx,ny,k});
}
}else if (dist[nx][ny][k]> lastcost+600){ //다른 방향으로 이동할때 코너 비용 추가
dist[nx][ny][k] = lastcost+600;
q.push({nx,ny,k});
}
}
}
answer = min(dist[bSize-1][bSize-1][1],dist[bSize-1][bSize-1][3]);
return answer;
}
'Algorithm > 알고리즘 문제풀이' 카테고리의 다른 글
BOJ) 16918 - 봄버맨 (0) | 2020.07.07 |
---|---|
BOJ) 16920 - 확장 게임 (0) | 2020.07.07 |
프로그래머스) Lv2 - 수식 최대화 [2020 카카오 인턴십] (0) | 2020.07.05 |
프로그래머스) Lv1 - 키패드 누르기 [2020 카카오 인턴십] (0) | 2020.07.05 |
BOJ) 1937 - 욕심쟁이 판다 (0) | 2020.07.01 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- infallible
- C++
- Swift unowned
- cocoapods
- rxswift
- Swift
- SWEA
- Reactivex
- disposeBag
- blendshape
- 카카오인턴십
- 알고리즘
- 프로그래머스
- UIHostingController
- coreml
- GraphDB
- SwiftUI
- rxswift6
- Swift weak
- DispatchQueue
- 백준
- 백준온라인저지
- 안드로이드
- ios
- ARKit
- blendshapes
- boj
- Lottie
- 코코아팟
- Kotlin
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함