μ•Œκ³ λ¦¬μ¦˜/λ¬Έμ œν’€μ΄ Baekjoon

[λ°±μ€€] λ°±νŠΈλž˜ν‚Ή - 2210. 숫자판 점프

λ˜νš¨λ‹ˆ 2020. 3. 8. 21:55

문제

5×5 크기의 숫자판이 μžˆλ‹€. 각각의 μΉΈμ—λŠ” 숫자(digit, 0λΆ€ν„° 9κΉŒμ§€)κ°€ μ ν˜€ μžˆλ‹€. 이 숫자판의 μž„μ˜μ˜ μœ„μΉ˜μ—μ„œ μ‹œμž‘ν•΄μ„œ, 인접해 μžˆλŠ” λ„€ λ°©ν–₯으둜 λ‹€μ„― 번 μ΄λ™ν•˜λ©΄μ„œ, 각 칸에 μ ν˜€μžˆλŠ” 숫자λ₯Ό μ°¨λ‘€λ‘œ 뢙이면 6자리의 μˆ˜κ°€ λœλ‹€. 이동을 ν•  λ•Œμ—λŠ” ν•œ 번 거쳀던 칸을 λ‹€μ‹œ 거쳐도 되며, 0으둜 μ‹œμž‘ν•˜λŠ” 000123κ³Ό 같은 수둜 λ§Œλ“€ 수 μžˆλ‹€.

숫자판이 μ£Όμ–΄μ‘Œμ„ λ•Œ, λ§Œλ“€ 수 μžˆλŠ” μ„œλ‘œ λ‹€λ₯Έ μ—¬μ„― 자리의 μˆ˜λ“€μ˜ 개수λ₯Ό κ΅¬ν•˜λŠ” ν”„λ‘œκ·Έλž¨μ„ μž‘μ„±ν•˜μ‹œμ˜€.

 

μž…λ ₯

λ‹€μ„― 개의 쀄에 λ‹€μ„― 개의 μ •μˆ˜λ‘œ 숫자판이 μ£Όμ–΄μ§„λ‹€.

 

좜λ ₯

첫째 쀄에 λ§Œλ“€ 수 μžˆλŠ” μˆ˜λ“€μ˜ 개수λ₯Ό 좜λ ₯ν•œλ‹€.

 

예제 μž…λ ₯1

1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 2 1
1 1 1 1 1

 

예제 좜λ ₯1

15

 

힌트

111111, 111112, 111121, 111211, 111212, 112111, 112121, 121111, 121112, 121211, 121212, 211111, 211121, 212111, 212121 이 κ°€λŠ₯ν•œ κ²½μš°λ“€μ΄λ‹€.

 

생각

숫자 쀑볡체크λ₯Ό μ–΄λ–»κ²Œ ν•΄μ•Ό ν•˜λ‚˜ κ³ λ―Όν–ˆμ—ˆλ‹€. μ²˜μŒμž‘μ„±ν–ˆλ˜ μ½”λ“œλŠ” μ‹œκ°„μ΄ˆκ³Όκ°€ 났닀. μ˜€νƒ€λ•Œλ¬Έμ— 그런 것 같기도 ν•˜κ³  배열에 6자리숫자 κ°’ λ„£κ³  쀑볡체크 true, false ν•˜λ €ν–ˆμ—ˆλ‹€. 

κ²€μƒ‰ν•΄λ³΄λ‹ˆ setμ΄λΌλŠ” μžλ£Œν˜•μ΄ μžˆλ”λΌ. 쀑볡을 μ œκ±°ν•˜μ§€ μ•ŠλŠ” 게 νŠΉμ§•μ΄κ³ , 쀑볡을 ν—ˆμš©ν•˜λ €λ©΄ multiset을 μ‚¬μš©ν•΄μ•Όν•œλ‹€.

μ •λ‹΅λ₯ μ²˜λŸΌ μ‰¬μš΄ λ¬Έμ œλŠ” μ•„λ‹ˆμ—ˆλ˜ κ±° 같은데 흠... 

 

http://www.cplusplus.com/reference/set/set/

 

set - C++ Reference

difference_typea signed integral type, identical to: iterator_traits ::difference_type usually the same as ptrdiff_t

www.cplusplus.com

 

μž‘μ„±ν•œ μ½”λ“œ

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
#include <iostream>
#include <set>
 
using namespace std;
 
int digit[5][5];
int dx[] = {-1100};
int dy[] = {00-11};
int num;
set<int> s;
 
void DFS(int x, int y, int n){
    int temp = 0;
    if(n == 6){
        s.insert(num);
        return;
    }
    for(int i=0; i<4; i++){
        int nx = x + dx[i];
        int ny = y + dy[i];
        if (nx < 0 || ny < 0 || nx >= 5 || ny >= 5)
            continue;
        temp = num;
        num = (num * 10+ digit[nx][ny];
        DFS(nx, ny, n+1);
        num = temp;
    }
}
int main(int argc, const char * argv[]) {
    // insert code here...
    
    for(int i=0; i<5; i++){
        for(int j=0; j<5; j++){
            cin >> digit[i][j];
        }
    }
    
    for(int i=0; i<5; i++){
       for(int j=0; j<5; j++){
           DFS(i, j, 0);
       }
    }
    
    cout << s.size();
    
    return 0;
}
 
 
Colored by Color Scripter
λ°˜μ‘ν˜•