π¨ λ¬Έμ
λ¬Έμ λ§ν¬: https://www.acmicpc.net/problem/2210
- μκ³ λ¦¬μ¦ λΆλ₯: κΉμ΄ μ°μ νμ (DFS), κ·Έλν νμ, λΈλ£¨νΈν¬μ€
- λμ΄λ: Silver 2
π¬ νμ΄
1. arr[5][5]μ μ«μνμ μ λ ₯λ°κ³ ,
2. λͺ¨λ μ’νμ λν΄ DFS κ²½λ‘ νμ
- μ€λ³΅μ νμ©νμ§ μλ setμ μ΄μ©νλ€.
π©π» μ½λ
C++
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <set>
#define N 5
using namespace std;
int arr[N][N];
int dx[4] = { 1, 0, -1, 0 };
int dy[4] = { 0, 1, 0, -1 };
set<int> s;
void DFS(int y, int x, int n, int len) {
if (len == 6) {
s.insert(n);
return;
}
for (int i = 0; i < 4; i++) {
int ny = y + dy[i];
int nx = x + dx[i];
if (ny >= 0 && ny < N && nx >= 0 && nx < N) {
DFS(ny, nx, n * 10 + arr[ny][nx], len + 1);
}
}
}
int main(void) {
ios::sync_with_stdio(false);
cin.tie(NULL);
//freopen("input.txt", "rt", stdin);
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
cin >> arr[i][j];
}
} // μ
λ ₯
/* λͺ¨λ μ’νμ DFS κ²½λ‘ νμ */
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
DFS(i, j, arr[i][j], 1);
}
}
cout << s.size();
return 0;
}
'Coding Test > λ°±μ€(BOJ)' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[BOJ] λ°±μ€ #1026. 보물 (C++) (0) | 2022.02.16 |
---|---|
[BOJ] λ°±μ€ #1309. λλ¬Όμ (C++) (0) | 2022.02.13 |
[BOJ] λ°±μ€ #11047. λμ 0 (C++) (0) | 2022.02.13 |
[BOJ] λ°±μ€ #1251. λ¨μ΄ λλκΈ° (C++) (0) | 2022.02.13 |
[BOJ] λ°±μ€ #1010. λ€λ¦¬ λκΈ° (C++) (0) | 2022.02.12 |