λμ΄λ : 골λ 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