๋ฌธ์ ๋งํฌ
https://school.programmers.co.kr/learn/courses/30/lessons/67259
์ฃ ๋ฅด๋๊ฐ ๊ฒฝ์ฃผ๋ก๋ฅผ ๊ฑด์คํ๋ ๋ฐ ํ์ํ ์ต์ ๋น์ฉ์ ๊ณ์ฐํ ์ ์๋๋ก ๋์์ฃผ์.
ํ์ด
์ธ์ ํ ์ํ์ข์ฐ๋ก ์์ง์ผ ์ ์์ผ๋ฉฐ ์ง์ ๋๋ก๋ฅผ ๊ฑด์คํ๋ ๋ฐ๋ 100์, ์ฝ๋๋ฅผ ๊ฑด์คํ๋ ๋ฐ๋ 500์์ด ์์๋๋ค.
์ฆ, ๊ฐ์ ๋ฐฉํฅ์ผ๋ก ์ด๋ํ ๋๋ ๋๋ก๋ง ๊ฑด์คํ๋ฉด ๋๋๊น 100์
๋ค๋ฅธ ๋ฐฉํฅ์ผ๋ก ์ด๋ํ ๋๋ ๋๋ก + ์ฝ๋๋ฅผ ๊ฑด์คํด์ผํ๋ฏ๋ก 600์์ ๋น์ฉ์ด ๋ฐ์ํ๋ค.
"๋ฐฉํฅ"์ด ์ค์ํ ์์์์ ์ ์ ์๋ค.
์ต์ ๋น์ฉ์ ์ถ๋ ฅํด์ผ ํ๋ฏ๋ก "์ต๋จ๊ฑฐ๋ฆฌ" ์๊ณ ๋ฆฌ์ฆ์ ์ฌ์ฉํด์ผ ํ๋ค.
๊ทธ ์ค์์ ๋๋ ๋ค์ต์คํธ๋ผ ์๊ณ ๋ฆฌ์ฆ์ ์ฌ์ฉํ๋ค.
๊ธฐ์กด์ ๋ค์ต์คํธ๋ผ ์๊ณ ๋ฆฌ์ฆ์ ๊ฐ ์นธ๋ง๋ค ์ต๋จ๊ฑฐ๋ฆฌ๋ฅผ ์ ์ฅํ๋ค.
ํ์ง๋ง ์ด ๋ฌธ์ ์์๋ ์์์ ์นธ์ ๋์ฐฉํ์ ๋, ์ฐจ๊ฐ ์ด๋ค ๋ฐฉํฅ์ ๋ฐ๋ผ๋ณด๊ณ ์๋ ์ง๋ ์ค์ํ๋ค.
๊ทธ๋์ ์ขํ + ์ฐจ๊ฐ ๋ฐ๋ผ๋ณด๊ณ ์๋ ๋ฐฉํฅ๋ง๋ค ์ต๋จ๊ฑฐ๋ฆฌ๋ฅผ ๋ฐ๋ก ๊ณ์ฐํ๋ค.
์ฐ์ ์์ ํ์ {๋น์ฉ, ๋ฐฉํฅ}, {๋ฐฉ๋ฌธํ ์นธ์ ์ขํ} ๋ฅผ ์ ์ฅํ๋ค.
๊ฑฐ๋ฆฌ๋ฅผ ์ ์ฅํ๋ Dist ๋ฐฐ์ด์ Dist[4][25][25] ์นธ์ผ๋ก ๋ง๋ค์ด ์ฒซ๋ฒ์งธ ์ธ๋ฑ์ค๋ ๋ฐฉํฅ์ผ๋ก ์ค์ ํ๋ค.
๋๋ 0 = ์, 1 = ํ, 2 = ์ข, 3 = ์ฐ. ์ด๋ ๊ฒ ์๊ฐํ๋ค.
์ต์ข ์ ์ผ๋ก Dist[0 ~ 3][n-1][n-2] ๋ค ๊ฐ์ ๊ฐ ์ค ์ต์๊ฐ์ด ์ ๋ต์ด ๋๋ค.
์ฝ๋ ์์ฑ
|
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 25 + 5;
const int INF = INT_MAX;
int dx[] = {0,0,-1,1};
int dy[] = {-1,1,0,0};
int Dist[4][MAX_N][MAX_N];
//bool visited[MAX_N][MAX_N];
bool is_ok(pair<int,int> pos, vector<vector<int>> board){
int n = board.size();
if(pos.first < 0 || pos.first >= n || pos.second < 0 || pos.second >= n
|| board[pos.first][pos.second])
return false;
return true;
}
void BFS(vector<vector<int>> board){
priority_queue<pair<pair<int,int>, pair<int,int>>> pq;
int board_size = board.size();
pq.push({{0,1},{0,0}});
pq.push({{0,3},{0,0}});
while(!pq.empty()){
pair<int,int> now = pq.top().second;
int cost = -pq.top().first.first;
int direction = pq.top().first.second;
pq.pop();
if(Dist[direction][now.first][now.second] != INF)
continue;
Dist[direction][now.first][now.second] = cost;
if(now.first == board_size - 1 && now.second == board.size() -1)
continue;
for(int i = 0; i<4; i++){
pair<int,int> next = {now.first + dy[i], now.second + dx[i]};
int acost;
if(direction == i)
acost = cost + 100;
else if(direction != i)
acost = cost + 600;
if(is_ok(next, board) && Dist[i][next.first][next.second] == INF)
pq.push({{-acost, i}, next});
}
}
}
int solution(vector<vector<int>> board) {
int answer = INF;
int n = board.size();
for(int i = 0; i<=board.size(); i++)
for(int j = 0; j<=board.size(); j++)
for(int d = 0; d<4; d++)
Dist[d][i][j] = INF;
BFS(board);
for(int i = 0; i<4; i++)
answer = min(answer, Dist[i][n-1][n-1]);
return answer;
}
|
cs |
'ํ๋ก๊ทธ๋๋จธ์ค > 3๋จ๊ณ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค Lv3] : ์ง๊ฒ๋ค๋ฆฌ ๊ฑด๋๊ธฐ (0) | 2022.07.20 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค Lv3] : ๊ธฐ๋ฅ๊ณผ ๋ณด ์ค์น (0) | 2022.07.19 |
[ํ๋ก๊ทธ๋๋จธ์ค Lv3] : ํฉ์น ํ์ ์๊ธ (0) | 2022.07.13 |
[ํ๋ก๊ทธ๋๋จธ์ค Lv3] : ๊ธ๊ณผ ์ ์ด๋ฐํ๊ธฐ (0) | 2022.07.11 |
[ํ๋ก๊ทธ๋๋จธ์ค Lv3] : GPS (0) | 2022.07.08 |