๋ฌธ์ ๋งํฌ
https://school.programmers.co.kr/learn/courses/30/lessons/60063
๋ก๋ด์ด ์ง๋์ ๊ฐ์ฅ ๋์ ์ธ (N, N)๊น์ง ์ด๋ํ๋ ๋ฐ ํ์ํ ์ต์ ์๊ฐ์ ๋ฆฌํดํด์ฃผ์.
ํ์ด
๋ฌด์ง์ ๋ก๋ด์ 2์นธ์ ์ฐจ์งํ๋ ๋ก๋ด์ด๋ค.
์,ํ,์ข,์ฐ๋ก ์ด๋ํ๊ฑฐ๋ 90๋ ํ์ ์ด ๊ฐ๋ฅํ๋ค.
๋ฌธ์ ๋ฅผ ์ ๋๋ก ์ ์ฝ๊ณ ๋์ฌ์ ธ์๋ ๋ฐฉํฅ์ผ๋ก๋ง ์ด๋์ด ๊ฐ๋ฅํ ์ค ์๊ณ ๋ฌธ์ ๋ฅผ ํ์ด์ ์๊ฐ์ ๋ญ๋นํ๋ค..
ํ์ ๋ฐฉ๋ฒ์ BFS๋ฅผ ์ด์ฉํ๊ณ , ๋ฐฉ๋ฌธ์ฒดํฌ๊ฐ ์ค์ํ๋ค.
๋ก๋ด์ด ๋ ์นธ์ ์ฐจ์งํ๊ธฐ ๋๋ฌธ์ 1์ฐจ์ ๋ฐฐ์ด๋ก ๋ฐฉ๋ฌธ์ ์ฒดํฌํ๊ฒ ๋๋ฉด, ์ธ๋ก๋ก ์ ๊ทผํ์ ๋์ ๊ฐ๋ก๋ก ์ ๊ทผํ์ ๋๋ฅผ ๊ตฌ๋ถํ์ง ๋ชปํ๋ค.
visited๋ฐฐ์ด์ 4์ฐจ์์ผ๋ก ๋ง๋ค์ด์ ๋ก๋ด์ด ์ฐจ์งํ๊ณ ์๋ ๋ ์นธ์ ์ขํ๋ฅผ pos1, pos2๋ผ๊ณ ํ์ ๋,
visited[pos1_y][pos1_x][pos2_y][pos2_x] ์ด๋ ๊ฒ ํ์๋ฅผ ํ ์ ์๋๋ก ํ๋ค.
์๊ด์ ์์ง๋ง
์ข์ฐ๋ก ๋์ฌ์์ ๋ ํญ์ ์ผ์ชฝ์ ์๋ ์นธ์ pos1,
์ํ๋ก ๋์ฌ์์ ๋ ํญ์ ์์ชฝ์ ์๋ ์นธ์ pos1 ์ด๋ผ๊ณ ์ ํ๋ค.
์ด์ ๋ก๋ด์ ์ด๋์ ํต์ ํด์ฃผ์.
์์ ๊ฐ์ด ๋ก๋ด์ด ๊ฐ๋ก๋ก ๋์ฌ์ ธ ์๋ค๊ณ ํ์.
๋ฒ์๋ฅผ ๋ฒ์ด๋์ง ์๋ ์ ์์, ์ํ์ข์ฐ๋ก์ ์ด๋๊ณผ 4๋ฐฉํฅ ํ์ ์ ๋ชจ๋ ๊ณ ๋ คํด์ผํ๋ค.
๋จผ์ ์ํ์ข์ฐ๋ก์ ์ด๋์ ์ดํด๋ณด๋ฉด
์ : ์๋ก ์ด๋ํ๊ธฐ ์ํด์ โ ๋ฒ ๋ ์นธ์ ๋ฒฝ์ด ์์ด์ผ ํ๋ค.
ํ : ์๋๋ก ์ด๋ํ๊ธฐ ์ํด์ โก๋ฒ ๋ ์นธ์ ๋ฒฝ์ด ์์ด์ผ ํ๋ค.
์ฐ : ์ค๋ฅธ์ชฝ์ผ๋ก ์ด๋ํ๊ธฐ ์ํด์ โข๋ฒ์ ๋ฒฝ์ด ์์ด์ผ ํ๋ค.
์ข : ์ผ์ชฝ์ผ๋ก ์ด๋ํ๊ธฐ ์ํด์ โฃ๋ฒ์ ๋ฒฝ์ด ์์ด์ผ ํ๋ค.
๋ค์์ผ๋ก ํ์ ์ ์ดํด๋ณด๋ฉด
โ : โ ๋ฒ ๋ฐฉํฅ์ผ๋ก ํ์ ํ๊ธฐ ์ํด์ โ ๋ฒ ๋ ์นธ์ ๋ชจ๋ ๋ฒฝ์ด ์์ด์ผ ํ๋ค.
โก : โก๋ฒ ๋ฐฉํฅ์ผ๋ก ํ์ ํ๊ธฐ ์ํด์ โก๋ฒ ๋ ์นธ์ ๋ชจ๋ ๋ฒฝ์ด ์์ด์ผ ํ๋ค.
๋ก๋ด์ด ์ธ๋ก๋ก ๋์ฌ์์ ๋๋ ๊ฐ์ ๋งฅ๋ฝ์ผ๋ก ์ํฉ์ ๊ณ ๋ คํด์ฃผ๋ฉด ๋๋ค.
์ฝ๋ ์์ฑ
|
#include <bits/stdc++.h>
using namespace std;
bool visited[105][105][105][105];
int ans = 987654321;
bool is_ok(pair<int,int> pos, int n, vector<vector<int>>& board){
if(pos.first < 0 || pos.first >= n || pos.second < 0 || pos.second >= n || board[pos.first][pos.second])
return false;
return true;
}
int solution(vector<vector<int>> board) {
int answer = 0;
int n = board.size();
//Priority_queue pq;
queue<pair<pair<int,pair<int,int>>, pair<int,int>>> q;
q.push({{0,{0,0}},{0,1}});
while(!q.empty()){
int T = q.front().first.first;
pair<int,int> pos1 = q.front().first.second;
pair<int,int> pos2 = q.front().second;
q.pop();
if(visited[pos1.first][pos1.second][pos2.first][pos2.second])
continue;
visited[pos1.first][pos1.second][pos2.first][pos2.second] = true;
if(pos2.first == n - 1 && pos2.second == n - 1){
answer = T;
break;
}
if(pos1.first == pos2.first){ //ํ์ฌ ๊ฐ๋ก๋ฐฉํฅ
pair<int,int> next = {pos2.first, pos2.second + 1}; //์ค๋ฅธ์ชฝ์ผ๋ก ์ด๋
if(is_ok(next, n, board) && !visited[pos2.first][pos2.second][next.first][next.second])
q.push({{T + 1, pos2},next});
next = {pos1.first, pos1.second - 1}; // ์ผ์ชฝ์ผ๋ก
if(is_ok(next, n, board) && !visited[next.first][next.second][pos1.first][pos1.second])
q.push({{T + 1, next},pos1});
next = {pos1.first + 1, pos1.second}; // ์ผ์ชฝ ํ์
if(is_ok(next, n, board) && is_ok({pos2.first + 1, pos2.second}, n, board)
&& !visited[next.first][next.second][pos2.first + 1][pos2.second])
q.push({{T + 1, next}, {pos2.first + 1, pos2.second}});
if(is_ok(next, n, board) && !visited[pos1.first][pos1.second][next.first][next.second]
&& !board[pos2.first + 1][pos2.second])
q.push({{T + 1, pos1},next});
next = {pos2.first + 1, pos2.second}; // ์ค๋ฅธ์ชฝ ํ์
if(is_ok(next, n, board) && !visited[pos2.first][pos2.second][next.first][next.second]
&& !board[pos1.first + 1][pos1.second])
q.push({{T + 1, pos2},next});
next = {pos1.first - 1, pos1.second};
if(is_ok(next, n, board) && is_ok({pos2.first - 1, pos2.second}, n, board)
&& !visited[next.first][next.second][pos2.first - 1][pos2.second])
q.push({{T + 1, next}, {pos2.first - 1, pos2.second}});
if(is_ok(next, n, board) && !visited[next.first][next.second][pos1.first][pos1.second]
&& !board[pos2.first - 1][pos2.second])
q.push({{T + 1, next},pos1});
next = {pos2.first - 1, pos2.second};
if(is_ok(next, n, board) && !visited[next.first][next.second][pos2.first][pos2.second]
&& !board[pos1.first - 1][pos1.second])
q.push({{T + 1, next},pos2});
}
else if(pos1.second == pos2.second){ //์ธ๋ก ๋ฐฉํฅ
pair<int,int> next = {pos2.first + 1, pos2.second}; //์๋๋ก
if(is_ok(next, n, board) && !visited[pos2.first][pos2.second][next.first][next.second])
q.push({{T + 1, pos2},next});
next = {pos1.first - 1, pos1.second}; //์๋ก
if(is_ok(next, n, board) && !visited[next.first][next.second][pos1.first][pos1.second])
q.push({{T + 1, next},pos1});
next = {pos1.first, pos1.second + 1};
if(is_ok(next, n, board) && is_ok({pos2.first, pos2.second + 1}, n, board)
&& !visited[next.first][next.second][pos2.first][pos2.second + 1])
q.push({{T + 1, next}, {pos2.first, pos2.second + 1}});
if(is_ok(next, n , board) && !visited[pos1.first][pos1.second][next.first][next.second]
&& !board[pos2.first][pos2.second + 1])
q.push({{T + 1, pos1}, next});
next = {pos2.first, pos2.second + 1};
if(is_ok(next, n , board) && !visited[pos2.first][pos2.second][next.first][next.second]
&& !board[pos1.first][pos1.second + 1])
q.push({{T + 1, pos2}, next});
next = {pos1.first, pos1.second - 1};
if(is_ok(next,n, board) && is_ok({pos2.first, pos2.second - 1}, n, board)
&& !visited[next.first][next.second][pos2.first][pos2.second - 1])
q.push({{T + 1, next}, {pos2.first, pos2.second - 1}});
if(is_ok(next, n , board) && !visited[next.first][next.second][pos1.first][pos1.second]
&& !board[pos2.first][pos2.second - 1])
q.push({{T + 1, next},pos1});
next = {pos2.first, pos2.second - 1};
if(is_ok(next, n , board) && !visited[next.first][next.second][pos2.first][pos2.second]
&& !board[pos1.first][pos1.second - 1])
q.push({{T + 1, next},pos2});
}
}
return answer;
}
|
cs |
์ฒ์์ ์ด๋ ๋ฐฉํฅ์ ์๋ชป ์ ์ฑ ์ฝ๋๋ฅผ ์์ฑํ ํ ๋์ค์ ์ฝ๋๋ฅผ ์ถ๊ฐํด์ ์ฝ๋๊ฐ ์๋ง์ด ๋๋ค.
'ํ๋ก๊ทธ๋๋จธ์ค > 3๋จ๊ณ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค Lv3] : ์นด๋ ์ง ๋ง์ถ๊ธฐ (0) | 2022.07.25 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค Lv3] : ์ง๊ฒ๋ค๋ฆฌ ๊ฑด๋๊ธฐ (0) | 2022.07.20 |
[ํ๋ก๊ทธ๋๋จธ์ค Lv3] : ๊ธฐ๋ฅ๊ณผ ๋ณด ์ค์น (0) | 2022.07.19 |
[ํ๋ก๊ทธ๋๋จธ์ค Lv3] : ๊ฒฝ์ฃผ๋ก ๊ฑด์ค (0) | 2022.07.14 |
[ํ๋ก๊ทธ๋๋จธ์ค Lv3] : ํฉ์น ํ์ ์๊ธ (0) | 2022.07.13 |