[λ°±μ€(BOJ)] 1941_μλ¬Έλ μΉ κ³΅μ£Ό C++
λ¬Έμ : BOJ_1941λ² μλ¬Έλ μΉ κ³΅μ£Ό
λ¬Έμ μ€λͺ
5 * 5μ mapμ΄ μ£Όμ΄μ§κ³ , κ° μ리λ 'Y' νΉμ 'S'λ‘ ννλμ΄ μμ΅λλ€.
μ, μλ, μ’, μ°μ μλ‘ μΈμ ν 7κ°μ μ리μμ 'S' κ° μ΅μ 4κ° μ΄μ μλ 7 μ리μ μλ₯Ό μ°Ύλ λ¬Έμ μ
λλ€.
Solution
μμ νμ, bfs
κ° μ리μ λν μ 보λ₯Ό 벑ν°μ μ μ₯ν΄ μ€λλ€.
μ μ₯ν μ 보λ κ° μ리μ μμΉ, 'S'μΈμ§μ μ¬λΆμ
λλ€.
μ μ₯λ μνμμ 7κ°μ μ리λ₯Ό check ν΄μ£Όκ³ μ΄ 7κ°μ μ리μμ 'S'κ° 4κ° μ΄μμΈ κ²½μ° bfsλ₯Ό λλ €μ€λλ€.
bfsμμ λ€μμ νμΈνλ©° μνν΄ μ€λλ€.
- μ£Όμ΄μ§ ν¬κΈ° μμ μλ mapμ μ리μΈμ§
- μ’, μ°, μ, νλ‘ μΈμ λΌμλμ§
- μμ μμ νμμμ check λμ΄ μλ μ리μΈμ§
μ΄ μ‘°κ±΄μ λ§μ‘±νλ©° checkλ 7κ°μ μ리λ₯Ό λͺ¨λ μνν κ²½μ°μ λ΅μ μΆκ°ν΄ μ€λλ€.
Description
- 25C7 = 480,700μ΄κ³ bfsλ‘ 7κ°λ₯Ό push νκΈ° λλ¬Έμ μκ° λ³΅μ‘λκ° ν¬μ§ μμ΅λλ€.
- check νλ κ° μμλ
lot
μ΄λΌλ pair λ°°μ΄μ μ μΈνμ¬ μμΉ μ’νλ₯Ό μ μ₯νμ΅λλ€.
#include <iostream>
#include <queue>
#include <algorithm>
#include <vector>
#include <cstring>
using namespace std;
char map[6][6];
int s_chk[26];
int dx[] = { 0,0,1,-1 };
int dy[] = { 1,-1,0,0 };
int cnt_m = 1;
void go(int idx, int cnt);
int chk[26];
int res,swc;
void go_res();
int lot[26][26];
vector<pair<int, int>> v;
int main(void) {
v.resize(26);
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 5; j++) {
scanf(" %c", &map[i][j]);
if (map[i][j] == 'S') s_chk[cnt_m] = 1;
lot[i][j] = cnt_m;
v[cnt_m] = { i,j };
cnt_m++;
}
}
go(1, 1);
printf("%d\n", res);
}
void go(int idx, int cnt) {
if (cnt == 8) {
int das = 0;
swc = 0;
for (int i = 1; i <= 25; i++) {
if (chk[i]) {
if (s_chk[i]) {
das++;
if (!swc) swc = i;
}
}
}
if (das >= 4) go_res();
return;
}
for (int i = idx; i <= 25; i++) {
if (chk[i]) continue;
chk[i] = 1;
go(i, cnt + 1);
chk[i] = 0;
}
}
void go_res(void) {
int go = 0;
int vis[26][26];
memset(vis, 0, sizeof(vis));
queue<pair<int,int>> q;
q.push({ v[swc].first, v[swc].second });
vis[v[swc].first][v[swc].second] = 1;
while (!q.empty()) {
go++;
int x = q.front().first;
int y = q.front().second;
q.pop();
for (int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx < 1 || ny < 1 || nx>5 || ny>5) continue;
if (vis[nx][ny]) continue;
if (chk[lot[nx][ny]]==1) {
vis[nx][ny] = 1;
q.push({ nx,ny });
}
}
}
if (go == 7) {
res++;
}
}
'Algorithm > BOJ' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[BOJ] λ±(3190) (0) | 2022.03.15 |
---|---|
[BOJ] μμ μ΄λ 2μ κ±°λ μ κ³±μ μ’μν΄! μμ μ΄λ 2μ κ±°λ μ κ³±μ μ’μν΄!(20153) (0) | 2022.03.15 |
[BOJ] μ¬μ΄ν΄ κ²μ(20040) (0) | 2022.03.14 |
[BOJ] νλ Έμ΄ ν μ΄λ μμ(11729) (0) | 2022.03.13 |
[BOJ] κ°μ₯ κΈ΄ λ°μ΄ν λ λΆλΆ μμ΄(11054) (0) | 2022.03.13 |