[BOJ] 18808 스티커 붙이기

Time Lapse :43min 23sec

18808.c

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 <stdio.h>
#include <memory.h>
#define SWAP(x, y) {(x)^=(y)^=(x)^=(y);}
#define gc() getchar_unlocked()
short laptop[40][40];
short sticker[10][10];
short R, C;
int fRI() {
int N = gc(), ret = 0;
for (; N<48 || N>57; N = gc());
do {
ret = (ret << 3) + (ret << 1) + (N & 0b1111); N = gc();
} while (0x30 <= N);
return ret;
}
void Rotate90() {
short Rsticker[10][10] = { 0, };
for (short i = 0; i < R; ++i)
for (short j = 0; j < C; ++j)
Rsticker[j][R - 1 - i] = sticker[i][j];
SWAP(R, C);
memcpy(sticker, Rsticker, sizeof(sticker));
}
int main() {
register short N = fRI(), M = fRI(), K = fRI(), answer, i, j, sti, stj, put_flag, flag, rotate, stk;
for (stk = 0; stk < K; ++stk) {
R = fRI(); C = fRI();
for (i = 0; i < R; ++i)
for (j = 0; j < C; ++j)
sticker[i][j] = fRI();
put_flag = 0;
for(rotate = 0; rotate < 4 && !put_flag; ++rotate) {
for (i = 0; i <= N - R && !put_flag; ++i)
for (j = 0; j <= M - C && !put_flag; ++j) {
flag = 0;
for (sti = 0; sti < R && !flag; ++sti)
for (stj = 0; stj < C && !flag; ++stj)
if (sticker[sti][stj] && laptop[i + sti][j + stj]) flag = 1;
if (flag) continue;
put_flag = 1;
for (sti = 0; sti < R; ++sti)
for (stj = 0; stj < C; ++stj)
if (sticker[sti][stj]) laptop[i + sti][j + stj] = 1, ++answer;
}
if(!put_flag) Rotate90();
}
}
printf("%d", answer);
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/07/30/PS/BOJ/18808/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.