[BOJ] 16988 baaaaaaaaaduck2 (Easy)

Time Lapse :17min 44sec

16988.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
#include <stdio.h>
#include <memory.h>
#include <queue>
#define gc() getchar_unlocked()
using namespace std;
int map[20][20], N, M, answer = 0;
int dx[4] = {0,1,0,-1};
int dy[4] = {-1,0,1,0};
queue<pair<int,int>> q;
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 simulation(){
register int killed = 0, kill_flag, cnt;
int map_cpy[20][20];
memcpy(map_cpy,map,sizeof(map_cpy));
for(register int i = 0; i < N; ++i)
for(register int j = 0; j < M; ++j){
if(map_cpy[i][j]==2){
kill_flag = cnt = 1;
q.push(make_pair(i,j));
map_cpy[i][j] = -1;
while(!q.empty()){
int x = q.front().second;
int y = q.front().first;
q.pop();
for(int k = 0; k < 4; ++k){
int ny = y + dy[k];
int nx = x + dx[k];
if(0<=nx && nx < M && 0<=ny&&ny<N){
if(map_cpy[ny][nx]==2){
++cnt;
q.push(make_pair(ny,nx));
map_cpy[ny][nx]=-1;
}
else if(map_cpy[ny][nx]==0){
kill_flag = 0;
}
}
}
}
killed = killed + cnt*kill_flag;
}
}
answer = answer > killed ? answer : killed;
}
void DFS(int c, int s){
if(c==2){
simulation();
return;
}
register int search = 0, i, j;
for(i = 0 ; i < N; ++i)
for(j = 0; j < M; ++j){
++search;
if(!map[i][j]&&search > s){
map[i][j] = 1;
DFS(c+1,search);
map[i][j] = 0;
}
}
}
int main(void){
register int i, j;
N = fRI(); M = fRI();
for(i = 0 ; i < N; ++i)
for(j = 0; j < M; ++j)
map[i][j] = fRI();
DFS(0,0);
printf("%d",answer);
return 0; //정상종료시 반드시 0을 리턴해야 합니다.
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/07/30/PS/BOJ/16988/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.