๋ฌธ์
ํ์ด
์ฝ๋
๊ฒฐ๊ณผ

๋์ด๋ : ๊ณจ๋ 4
๊ฑธ๋ฆฐ ์๊ฐ : ์ค๋ ๊ฑธ๋ฆผ
๋ฌธ์
๋ฒฝ ๋ถ์๊ณ ์ด๋ํ๊ธฐ ๋ฌธ์ ๋ณด๋ฌ๊ฐ๊ธฐ
NรM์ ํ๋ ฌ๋ก ํํ๋๋ ๋งต์ด ์๋ค. ๋งต์์ 0์ ์ด๋ํ ์ ์๋ ๊ณณ์ ๋ํ๋ด๊ณ , 1์ ์ด๋ํ ์ ์๋ ๋ฒฝ์ด ์๋ ๊ณณ์ ๋ํ๋ธ๋ค. ๋น์ ์ (1, 1)์์ (N, M)์ ์์น๊น์ง ์ด๋ํ๋ ค ํ๋๋ฐ, ์ด๋ ์ต๋จ ๊ฒฝ๋ก๋ก ์ด๋ํ๋ ค ํ๋ค. ์ต๋จ๊ฒฝ๋ก๋ ๋งต์์ ๊ฐ์ฅ ์ ์ ๊ฐ์์ ์นธ์ ์ง๋๋ ๊ฒฝ๋ก๋ฅผ ๋งํ๋๋ฐ, ์ด๋ ์์ํ๋ ์นธ๊ณผ ๋๋๋ ์นธ๋ ํฌํจํด์ ์ผ๋ค.
๋ง์ฝ์ ์ด๋ํ๋ ๋์ค์ ํ ๊ฐ์ ๋ฒฝ์ ๋ถ์๊ณ ์ด๋ํ๋ ๊ฒ์ด ์ข ๋ ๊ฒฝ๋ก๊ฐ ์งง์์ง๋ค๋ฉด, ๋ฒฝ์ ํ ๊ฐ ๊น์ง ๋ถ์๊ณ ์ด๋ํ์ฌ๋ ๋๋ค.
ํ ์นธ์์ ์ด๋ํ ์ ์๋ ์นธ์ ์ํ์ข์ฐ๋ก ์ธ์ ํ ์นธ์ด๋ค.
๋งต์ด ์ฃผ์ด์ก์ ๋, ์ต๋จ ๊ฒฝ๋ก๋ฅผ ๊ตฌํด ๋ด๋ ํ๋ก๊ทธ๋จ์ ์์ฑํ์์ค.
ํ์ด
- ๋ชจ๋ ๊ฒฝ๋ก๋ฅผ ์ดํด๋ณผ ํ์์์ด ์ต๋จ ๊ฒฝ๋ก๋ฅผ ๊ตฌํ๋ฉด ๋๋ฏ๋ก bfs๋ฅผ ์ด์ฉํ๋ค.
- ๋ฒฝ์ ๋ถ์๊ณ ์ด๋ํ ์ ์์ผ๋ฏ๋ก ๋ฒฝ์ ๋ถ์๋์ง์ ์ฌ๋ถ๊น์ง ๋ฉ๋ชจ์ด์ ์ด์ ํด์ผํ๋ค.
์ฝ๋
[dfs] => ํ๋ฆฐ ์ฝ๋
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int N, M;
int map[1001][1001];
int direction[4][2] = { {0,1},{-1,0},{0,-1},{1,0} };
int memo[1001][1001][2];
int shortestPath(bool brakeRemain, int r, int c) {
if (r == N - 1 && c == M - 1)
return 1;
int minPath = 1e8;
for (int i = 0; i < 4; i++) {
int rp = r + direction[i][0];
int cp = c + direction[i][1];
int type = 0;
if (rp < 0 || cp < 0 || rp >= N || cp >= M) // ๋งต ์์ ์์ผ๋ฉด
continue;
if (brakeRemain)
type = 1;
int res;
if (map[rp][cp] == 0) {
if (memo[rp][cp][type] != -1)
res = memo[rp][cp][type];
else {
memo[rp][cp][type] = 1e8;
res = shortestPath(brakeRemain, rp, cp) + 1;
memo[rp][cp][type] = res;
}
if (minPath > res)
minPath = res;
}
else {
// ๋ฒฝ์ด๋ฉด
if (!brakeRemain) // ๋ถ์ ์ ์์ผ๋ฉด ๊ฑด๋๋ฐ๊ธฐ
continue;
if (memo[rp][cp][0] != -1) // ๋ฉ๋ชจ์ด์ ์ด์
res = memo[rp][cp][0];
else {
memo[rp][cp][0] = 1e8; // ๋ค์ ๋๋์์ค์ง ๋ชปํ๊ฒ
res = shortestPath(!brakeRemain, rp, cp) + 1;
memo[rp][cp][0] = res;
}
if (minPath > res)
minPath = res;
}
}
return minPath;
}
int main() {
cin >> N >> M;
for (int i = 0; i < N; i++) {
string str;
cin >> str;
for (int j = 0; j < str.size(); j++) {
map[i][j] = str[j]-'0';
}
}
memset(memo, -1, sizeof(memo));
int res = shortestPath(true, 0, 0);
if (res >= 1e8)
res = -1;
cout << res;
}
[bfs] => ์ฑ๊ณต ์ฝ๋
#include <iostream>
#include <string>
#include <cstring>
#include <queue>
using namespace std;
int N, M;
int map[1001][1001];
int direction[4][2] = { {0,1},{-1,0},{0,-1},{1,0} };
int memo[1001][1001][2];
struct Node {
int r;
int c;
bool breakRemain = true;
int p;
Node(int row, int col, bool b) {
r = row; c = col; breakRemain = b; p = 0;
}
Node(int row, int col, bool b, int path) {
r = row; c = col; breakRemain= b; p = path;
}
};
int main() {
// 11์ 40๋ถ ์์
// ๋ฒฝ ํ๋๋ ๋ถ์ด๋ ๋จ
// ์ํ์ข์ฐ ์ด๋
// ์ต๋จ ๊ฒฝ๋ก
// n,m ์ 1000์ดํ
// ๋ถ๊ฐ๋ฅ ์ -1
// ์์ ๋ฌธ์ : ํด๋น ์นธ์์ ๋ง์ง๋ง๊น์ง์ ์ต๋จ ๊ฒฝ๋ก
// ์
๋ ฅ : ์ง๊ธ๊น์ง ๋ฒฝ์ ๋ถ์์ ์ด ์๋์ง, ํ์ฌ ์นธ
// ์ถ๋ ฅ : ์ต๋จ ๊ฒฝ๋ก
// ๋ฉ๋ชจ์ด์ ์ด์
: ํด๋น ์นธ, ๋ฒฝ ๋ถ์จ ์ฌ๋ถ
// ์ข
๋ฃ : ๋ง์ง๋ง ์นธ์ ๋๋ฌ, ๋ ๊ฐ๊ณณ์ด ์๊ณ ๋ฒฝ๋ถ์จ๋ ์์
cin >> N >> M;
for (int i = 0; i < N; i++) {
string str;
cin >> str;
for (int j = 0; j < str.size(); j++) {
map[i][j] = str[j]-'0';
}
}
memset(memo, -1, sizeof(memo));
queue<Node*> que;
que.push(new Node(0, 0, true));
memo[0][0][0] = 0;
memo[0][0][1] = 0;
int flag = 1;
while (!que.empty()) {
Node* node = que.front(); que.pop();
int r = node->r; int c = node->c; bool b = node->breakRemain;
int path = node->p +1;
if (r == N - 1 && c == M - 1) {
// ๋ง์ง๋ง ๋์ฐฉ
flag = 0;
cout << path;
break;
}
for (int i = 0; i < 4; i++) {
int rp = r + direction[i][0];
int cp = c + direction[i][1];
if (rp < 0 || cp < 0 || rp >= N || cp >= M) // ๋งต ์์ ์์ผ๋ฉด
continue;
if (memo[rp][cp][b ? 1 : 0] != -1) // ์ด๋ฏธ ํ๋ ๊ณณ์ด๋ฉด
continue;
if (map[rp][cp] == 0) {
// ๋ฒฝ์ด ์๋๋ฉด
que.push(new Node(rp, cp, b, path));
memo[rp][cp][b ? 1 : 0] = path;
}
else {
// ๋ฒฝ์ด๋ฉด
if (!b) // ๋ฒฝ๋ถ์๊ธฐ๊ฐ ์์ผ๋ฉด
continue;
que.push(new Node(rp, cp, false, path));
memo[rp][cp][b ? 1 : 0] = path;
}
}
}
if (flag) {
cout << -1;
}
}
๊ฒฐ๊ณผ

'Algorithm ๋ฌธ์ > BOJ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[c++] BOJ 2589 :: ๋ณด๋ฌผ์ฌ (0) | 2021.05.18 |
---|---|
[c++] BOJ 1707 :: ์ด๋ถ ๊ทธ๋ํ (0) | 2021.05.12 |
[c++] BOJ 16236 :: ์๊ธฐ ์์ด (0) | 2021.05.03 |
[c++] BOJ 1987 :: ์ํ๋ฒณ (0) | 2021.04.28 |
[c++] BOJ 10026 :: ์ ๋ก์์ฝ (0) | 2021.04.23 |
Comment