https://school.programmers.co.kr/learn/courses/30/lessons/67259
๋ด ํด๋ต
#include <string.h>
#include <vector>
#include <queue>
#include <iostream>
using namespace std;
enum dir
{
start = -1,
right,
down,
left,
up
};
struct road
{
int x;
int y;
int coast;
dir d;
road()
{
x = 0;
y = 0;
coast = 0;
d = dir::start;
};
road(int _x, int _y, int _coast, dir _d)
{
x = _x;
y = _y;
coast = _coast;
d = _d;
};
};
int solution(vector<vector<int>> board) {
int answer = 1'000'000;
int coast[25][25][4]; // ๋๋ก ๊ฑด์ค ๋น์ฉ ๋ฐฉํฅ๋ณ๋ก ์ ์ฅ
int dir_x[4] = { 1, 0 , -1, 0 }; // ์ฐ, ํ, ์ข, ์
int dir_y[4] = {0, 1, 0, -1};
// ๋น์ฉ์ ์ต๋ ๊ฐ์ผ๋ก ์ด๊ธฐํ ํ๋ค.
for (int i = 0; i < 25; ++i)
for (int j = 0; j < 25; ++j)
memset(coast[i][j], 1'000'000, sizeof(int) * 4);
// ์์์ Q์ ๋ฃ๊ธฐ
queue<road> q;
for(int i= 0; i < 4; ++i)
coast[0][0][i] = 0;
q.push(road());
// Q๊ฐ ๋น ๋๊น์ง ๊ฒฝ๋ก ํ์
while (!q.empty())
{
road cur = q.front();
q.pop();
// ๋์ฐฉ์ง์ ์ ๋์ฐฉํ๋ฉด ์ต์ ๋น์ฉ ์ ์ฅ
if (cur.y == board.size() - 1 && cur.x == board.size() - 1)
{
answer = answer < cur.coast ? answer : cur.coast;
continue;;
}
// 4๋ฐฉํฅ ํ์
for (int i = 0; i < 4; ++i)
{
int nx = cur.x + dir_x[i];
int ny = cur.y + dir_y[i];
// ๋ค์ ์ง์ ์ด ๋๋ก ๋ฒ์ ๋ฐ์ด๋ฉด continue
if (nx < 0 || nx >= board.size() || ny < 0 || ny >= board.size())
continue;
// ๋ค์ ์ง์ ์ด ๋ฒฝ์ ๋งํ์๋ค๋ฉด continue
if (board[ny][nx] == 1)
continue;
// ๋ค์ ์ง์ ๊ฑด์ค์ ๋๋ ๋น์ฉ ๊ณ์ฐ
int c = cur.coast + 100;
// ์ฝ๋๋ผ๋ฉด 500์ ์ถ๊ฐ
if (cur.d != dir::start)
if (cur.d % 2 != i % 2)
c += 500;
// ํ์ฌ ๋ฐฉํฅ์์ ๋๋ก ๊ฑด์ค ๋น์ฉ์ด ์ต์ ๊ฐ์ด๋ผ๋ฉด Q์ ์ถ๊ฐ
if (coast[ny][nx][i] >= c)
{
coast[ny][nx][i] = c;
q.push(road(nx, ny, c, static_cast<dir>(i)));
}
}
}
return answer;
}
๋ฐฉํฅ๋ง๋ค ๊ฑด์ค ๋น์ฉ์ ๋ฐ๋ก ์ ์ฅํด์ ๋น๊ตํด์ผ ํ๋ ๋ถ๋ถ์ด ๊น๋ค๋ก์ด ๋ฌธ์ ์๋ค.
'๐ฅ๏ธ Study Note > Coding Test' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค] level.2 - ๋ฌด์ธ๋ ์ฌํ(C++) (0) | 2023.05.23 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค] level.2 - ๋ ๋ฐ๋จน๊ธฐ(C++) (0) | 2023.05.22 |
[ํ๋ก๊ทธ๋๋จธ์ค] level.3 - ์ ์ ์ ์ถ ์ค์ผ์ค๋ง (C++) (0) | 2023.05.18 |
[ํ๋ก๊ทธ๋๋จธ์ค] level.3 - ํ๊ดด๋์ง ์์ ๊ฑด๋ฌผ(C++) (0) | 2023.05.16 |
[ํ๋ก๊ทธ๋๋จธ์ค] level.3 - ๊ฐ์ฅ ๋จผ ๋ ธ๋(C++) (0) | 2023.05.15 |