반응형

https://www.acmicpc.net/problem/5069

 

5069번: 미로에 갇힌 상근

상근이는 오른쪽 그림과 같은 미로에 갇혀있다. 미로는 육각형 모양의 방이 계속해서 붙어있는 모양이고, 이러한 방은 무수히 많이 있다. 또, 인접한 방은 문으로 연결되어 있어서, 한 방에서 다

www.acmicpc.net

 

 

[ 문제풀이 ]

 

1. dp[ 30 ][ 30 ][ 15 ]를 선언하고 -1로 초기화해 줍니다.

 

2. 6방향으로 이동하면서 이동 가능 거리가 0이 남았을 때 출발한 위치라면 1을 return 합니다.

 

3. dp[ 10 ][ 10 ][ n ]을 출력합니다.

 

[ 소스코드 ]

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>
 
using namespace std;
 
int dp[30][30][15];
int dy[] = { -1,0,1,1,0,-1 };
int dx[] = { 0,1,1,0,-1,-1 };
int T, n;
 
int dfs(int y, int x, int cnt)
{
    if (dp[y][x][cnt] != -1return dp[y][x][cnt];
    if (cnt == 0return y == 10 && x == 10;
    int& ret = dp[y][x][cnt];
    ret = 0;
 
    for (int i = 0; i < 6; i++) {
        const int yy = y + dy[i];
        const int xx = x + dx[i];
        if (yy >= 0 && yy < 30 && xx >= 0 && xx < 30) {
            ret += dfs(yy, xx, cnt - 1);
        }
    }
 
    return ret;
}
 
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
 
    cin >> T;
 
    for (int i = 0; i < 30; i++) {
        for (int j = 0; j < 30; j++) {
            for (int k = 0; k <= 14; k++) {
                dp[i][j][k] = -1;
            }
        }
    }
 
    while (T--) {
        cin >> n;
 
        cout << dfs(1010, n) << '\n';
    }
}
cs
반응형

+ Recent posts