문제 링크 :https://www.acmicpc.net/problem/2210

 

2210번: 숫자판 점프

111111, 111112, 111121, 111211, 111212, 112111, 112121, 121111, 121112, 121211, 121212, 211111, 211121, 212111, 212121 이 가능한 경우들이다.

www.acmicpc.net

 

 

 문제 설명 

 

처음에 5 x 5 크기의 숫자판이 주어지고 숫자판에서는 상하 좌우 네방향으로 움직일수 있다.

총 6번을 움직일때 얻을수있는 서로 다른 수열 의 갯수 를 출력.

똑같은곳을 여러번 방문 해도된다 .

 

 알고리즘 

 

재귀로 구현.

1. 배열의 (0,0) 부터 (4,4) 까지 다 탐색.

2. dfs 깊이 6일때 check() 함수 

3. check함수는 ans 벡터를 만들고 ans 벡터안에 똑같은 수열이 있는지 판별후 없으면 push_back .

4. ans의 크기 출력.

 

 

 코드 

 

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
49
50
51
52
#include<iostream>
#include<vector>
using namespace std;
 
 
 
int dx[] = { 1,-1,0,0 };
int dy[] = { 0,0,1,-1 };
int arr[5][5];
vector<vector<int>> ans;
 
bool check(vector<int> v) {
    int Size = ans.size();
    for(int i = 0; i < Size; i++) {
        if (ans[i] == v)return false;
    }
    return true;
}
 
 
void func(int y,int x,vector<int> v) {
    v.push_back(arr[y][x]);
    if (v.size() == 6) {
        if (check(v))
            ans.push_back(v);
        return;
    }
    for (int i = 0; i < 4; i++) {
        int next_y = y + dy[i];
        int next_x = x + dx[i];
        if (next_x >= 0 && next_x < 5 && next_y >= 0 && next_y < 5) {
            func(next_y, next_x, v);
        }
    }
 
}
 
int main() {
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 5; j++) {
            cin >> arr[i][j];
        }
    }
    vector<int> a;
    
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 5; j++) {
            func(i, j, a);
        }
    }
    cout << ans.size();
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
 

'c++ > 백준' 카테고리의 다른 글

백준 17203 번 : ∑|ΔEasyMAX|  (0) 2020.04.20
백준 10597번 : 순열장난  (0) 2020.04.17
백준 2661번 : 좋은수열  (0) 2020.04.17
백준 9663번 : N-Queen  (0) 2020.04.17
백준 1920번 : 수 찾기  (0) 2020.04.17
ariz1623