[BOJ] 17070 파이프 옮기기 1

Time Lapse :NONE

17070.cpp

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <iostream>
#include <math.h>
#include <string>
#include <algorithm>
#include <cstring>
#include <vector>
#include <cstdio>
#include <queue>

using namespace std;

#define MAXSIZE 17
int map[MAXSIZE][MAXSIZE];
int cache[MAXSIZE][MAXSIZE][3];
int N;
int ans;

int dp(int y, int x, int type)
{
if(x>N||y>N)
return 0;
int &ret = cache[y][x][type];
if(ret!=-1)
return ret;

ret = 0;
switch (type) {
case 0:
{
if(!map[y][x])
{
if(x==N&&y==N)
{
ret = 1;
return 1;
}
else
ret = dp(y,x+1,0) + dp(y+1,x+1,2);
}
else
return 0;
break;
}
case 1:
{
if(!map[y][x])
{
if(x==N&&y==N)
{
ret = 1;
return 1;
}
else
ret = dp(y+1,x,1) + dp(y+1,x+1,2);
}
else
return 0;
break;
}
case 2:
{
if(!map[y][x]&&!map[y-1][x]&&!map[y][x-1])
{
if(x==N&&y==N)
{
ret = 1;
return 1;
}
else
ret = dp(y,x+1,0) + dp(y+1,x,1) + dp(y+1,x+1,2);
}
else
return 0;
break;
}
default:
return 0;
break;
}

return ret;
}

int main(void)
{
cin>>N;
for(int i=1;i<=N;i++)
for(int j=1;j<=N;j++)
cin>>map[i][j];

memset(cache,-1,sizeof(cache));
ans = dp(1,2,0);
cout<<ans<<endl;
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/07/30/PS/BOJ/17070/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.