Notice
Recent Posts
Recent Comments
-
[DFS] 백준 2210번 - 숫자판 점프 본문
https://www.acmicpc.net/problem/2210
백준 DFS 문제다.
set 컨테이너를 활용해 풀면 쉽게 풀 수 있다.
갈 수 있는 방향으로 중복허용해 5번 이동하여 만들 수 있는 문자열의 개수를 출력하는 문제다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
#include <set>
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
int max_answer = -1;
vector<vector<string> > map;
set<string> s;
typedef struct dir{
int dr, dc;
}dir;
dir direction[4] = { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } };
void dfs(int jumpcnt, string val, int r, int c){
if (jumpcnt == 5){
s.insert(val);
return;
}
for (int i = 0; i < 4; i++){
int nr = r + direction[i].dr;
int nc = c + direction[i].dc;
if (0 <= nr && nr < 5 && 0 <= nc && nc < 5){
dfs(jumpcnt + 1, val + map[nr][nc], nr, nc);
}
}
}
int main(){
for (int i = 0; i < 5; i++){
for (int j = 0; j < 5; j++){
int ele;
cin >> ele;
map[i][j] = map[i][j] + to_string(ele);
}
}
for (int i = 0; i < 5; i++){
for (int j = 0; j < 5; j++){
dfs(0, map[i][j], i, j);
}
}
cout << s.size() << '\n';
return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs |
'3. DFS & 백트래킹' 카테고리의 다른 글
[DFS] 백준 2573번 - 빙산 (0) | 2020.02.07 |
---|---|
[백준] 2668번 - 숫자 고르기 (0) | 2020.02.07 |
[DFS/백트래킹] 백준 17136번 - 색종이 붙이기 (0) | 2020.01.28 |
[DFS] 백준 2468번 - 안전영역 (정답률 33%) (0) | 2020.01.13 |
[DFS] 백준 2583번 - 영역 구하기 (정답률 56%) (0) | 2020.01.13 |
Comments