본문 바로가기

알고리즘/백준

boj >> 17070 파이프 옮기기1

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
#include <iostream>
using namespace std;
 
const int MAX = 17;
 
int N;
int arr[MAX][MAX];
int dp[MAX][MAX][3];//dp table, 0:가로, 1:세로, 2:대각선
 
int main(){
    scanf("%d"&N);
    //cin >> N;
    //1. map 만들어주기. 
    for(int i=0; i<N; i++)
        for(int j=0; j<N; j++)
            scanf("%d"&arr[i][j]);
 
    //2. 처음 놓여진 위치, 좌표상에서 1을 넣어주고, 위치를 계산해준다. 
    dp[0][1][0= 1;
    for(int i=0; i<N; i++){
        for(int j=0; j<N; j++){
            if(arr[i][j]) {    continue;   }
            for(int n=0; n<3; n++){
                //2-1. 지금 상태에서 가로로 움직일 경우(비어야 하는 곳이 비어야+현재가 세로(1)이면 이 경우 제외)
                if(!arr[i][j+1&& n!=1){   
                    dp[i][j+1][0+= dp[i][j][n];
                }
                //2-2. 지금 상태에서 세로로 움직일 경우(현재가 가로(0)이면 이 경우 제외)
                if(!arr[i+1][j] && n!=0){
                    dp[i+1][j][1+= dp[i][j][n];
                }
                //2-3. 지금 상태에서 대각선으로 움직일 경우 
                if(arr[i+1][j+1]+arr[i][j+1]+arr[i+1][j+1]==0){
                    dp[i+1][j+1][2+= dp[i][j][n];
                }
            }
        }
    }
 
    printf("%d\n", dp[N-1][N-1][0]+dp[N-1][N-1][1]+dp[N-1][N-1][2]);
    //int result = dp[N-1][N-1][0]+dp[N-1][N-1][1]+dp[N-1][N-1][2];
    //cout << result;
    return 0;
}
cs