[BOJ] 17136 색종이 붙이기

Time Lapse :NONE

17136.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <iostream>
#include <math.h>
#include <string>
#include <algorithm>
#include <cstring>
#include <vector>
#include <cstdio>
#include <memory.h>
using namespace std;

#define MAXSIZE 11

int cnt=0;
int N;
int ans=987654321;
int used[6];
int arr[MAXSIZE][MAXSIZE];
int total = 0, temp = 0;
int found_x, found_y;
bool isPossible(int x, int y, int type)
{
if ((x + type) > 11 || (y + type) > 11||x<=0||y<=0)
return false;
if (used[type] <= 0)
return false;

for (int i = x; i<x + type; i++)
for (int j = y; j<y + type; j++)
if (arr[i][j] == 0)
return false;

return true;
}
void cover(int x, int y, int type)
{
for (int i = x; i<x + type; i++)
for (int j = y; j<y + type; j++)
arr[i][j] = 0;
}

void re_cover(int x, int y, int type)
{
for (int i = x; i<x + type; i++)
for (int j = y; j<y + type; j++)
arr[i][j] = 1;
}

void find(int x, int y)
{
found_x = 0; found_y = 0;
for (int i = 0; i <= 10; i++)
{
for (int j = 0; j <= 10; j++)
{
if (arr[i][j] == 1)
{
found_x = i; found_y = j;
return;
}
}
}
}

int dp(int x, int y, int type,int cur_total)
{

if (!isPossible(x, y, type))
return 987654321;
if (total == cur_total)
{
return 1;
}

used[type]--;
cover(x, y, type);

int ret = 987654321;
find(x, y);
int new_x = found_x;
int new_y = found_y;

for (int i = 5; i >= 1; i--)
{
ret = min(ret, dp(new_x, new_y, i,cur_total+(i*i)));
}

re_cover(x, y, type);
used[type]++;
return 1 + ret;
}

int main(void)
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);

for (int i = 1; i <= 10; i++)
for (int j = 1; j <= 10; j++)
{
cin >> arr[i][j];
total += arr[i][j];
}
for (int i = 1; i <= 5; i++)
used[i] = 5;

if (total == 0)
{
cout << "0" << endl;
return 0;
}
else if (total == 1)
{
cout << "1" << endl;
return 0;
}
else if (total == 100)
{
cout << "4" << endl;
return 0;
}
else
{
for (int k = 5; k >= 1; k--) {
find(1, 1);
ans = min(ans, dp(found_x, found_y, k, k * k));
}

if (ans < 100)
cout << ans << endl;
else
cout << "-1" << endl;
return 0;
}
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/07/30/PS/BOJ/17136/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.