[BOJ] 2339 석판 자르기

Time Lapse :NONE

2339.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <iostream>
#include <math.h>
#include <algorithm>
#include <cstring>

using namespace std;

#define MAXSIZE 20

int arr[MAXSIZE][MAXSIZE];
int n;
int answer = 0;

bool cut(int index, int arg1, int arg2, bool up)
{
if (up)
{
for (int i = arg1; i <= arg2; i++)
if (arr[index][i] == 2)
return false;

return true;
}
else
{
for (int i = arg1; i <= arg2; i++)
if (arr[i][index] == 2)
return false;

return true;
}
}

int answer_func(int x1, int x2, int y1, int y2, bool up)
{
int total = 0;
int ans = 0;
bool bul = true;

if (x2 == n || y2 == n)
return 1;

for (int i = x1; i <= x2; i++)
{
for (int j = y1; j <= y2; j++)
{
total += arr[i][j];
if (arr[i][j] == 1)
bul = false;
}
}

if (total < 2)
return 0;
if (total == 2)
{
return 1;
}
if (bul)
return 0;

for (int i = x1; i <= x2; i++)
{
for (int j = y1; j <= y2; j++)
{
if (arr[i][j] == 1 && up && cut(i,y1,y2,up))
{
ans = ans + answer_func(x1, i - 1, y1, y2, !up)*answer_func(i + 1, x2, y1, y2, !up);
}
else if (arr[i][j] == 1 && !up && cut(j, x1,x2, up))
{
ans = ans + answer_func(x1, x2, y1, j - 1, !up)*answer_func(x1, x2, j + 1, y2, !up);
}
}
}

return ans;
}

int main(void)
{

cin >> n;

for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cin >> arr[i][j];
}
}

for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (arr[i][j] == 1)
{
answer = answer + answer_func(0, n - 1, 0, j - 1, true)*answer_func(0, n - 1, j + 1, n - 1, true);
answer = answer + answer_func(0, i - 1, 0, n - 1, false)*answer_func(i + 1, n - 1, 0, n - 1, false);
}
}
}

if (answer == 0)
cout << "-1" << endl;
else
cout << answer << endl;
}

Author: Song Hayoung
Link: https://songhayoung.github.io/2020/07/30/PS/BOJ/2339/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.