[BOJ] 17135 캐슬 디펜스

Time Lapse :NONE

17135.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
#include <iostream>
#include <math.h>
#include <algorithm>
#include <memory.h>
#include <vector>
using namespace std;


#define MAXSIZE 16
int map[MAXSIZE][MAXSIZE];
int map_cpy[MAXSIZE][MAXSIZE];
bool status[MAXSIZE][MAXSIZE];
bool status_cpy[MAXSIZE][MAXSIZE];
int N,M,D;
int line_start;
int ans=0;
vector<int> arrow;

void play() {
vector<pair<int, int>> p;
p.push_back(make_pair(N+1,arrow[0]));
p.push_back(make_pair(N+1,arrow[1]));
p.push_back(make_pair(N+1,arrow[2]));
int cnt = 0;
bool find;
while(p[2].first>1)
{
vector<pair<int, int>> killed;
for(int k=0;k<3;k++) {
find = false;
for (int i = 1; i <= D; i++) {
int dx = -1;
int dy = (i - 1) * (-1);
for (int j = 1; j < i * 2; j++) {
int nx = p[k].first+dx;
int ny = p[k].second+dy;

dx = (j<i)?dx-1:dx+1;
dy++;

if(nx<=0||ny<=0||ny>M)
continue;
if(map_cpy[nx][ny]==1)
{
if(status_cpy[nx][ny])
{
cnt++;
status_cpy[nx][ny]=false;
killed.push_back(make_pair(nx,ny));
}
find=true;
break;
}
}
if(find)
break;
}
p[k].first--;
}
while(killed.size()>0)
{
int nx = killed[0].first;
int ny = killed[0].second;
map_cpy[nx][ny]=0;
killed.erase(killed.begin());
}
}
ans = max(ans, cnt);
return;
}

void DFS(int idx) {
if (arrow.size() == 3) {
memcpy(status_cpy,status,sizeof(status_cpy));
memcpy(map_cpy,map,sizeof(map_cpy));
play();
return ;
}
for (int i = idx; i <= M-2+arrow.size(); i++)
{
arrow.push_back(i);
DFS(i+1);
arrow.pop_back();
}
}

int main(void)
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin>>N>>M>>D;
memset(status,false,sizeof(status));
for(int i=1;i<=N;i++)
for(int j=1;j<=M;j++)
{
cin>>map[i][j];
if(map[i][j]==1)
status[i][j]=true;
}

DFS(1);
cout<<ans<<endl;
}

17135.java

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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import java.io.*;
import java.util.*;

import java.io.*;
import java.util.*;

public class Main {
static void play() {
int[][] p = new int[3][2];
p[0][0] = p[1][0] = p[2][0] = n + 1;
p[0][1] = arrow[0]; p[1][1] = arrow[1]; p[2][1] = arrow[2];
int cnt = 0;
boolean find;
while(p[2][0] > 1) {
Queue<pair> killed = new LinkedList<>();
for(int k=0;k<3;k++) {
find = false;
for (int i = 1; i <= d; i++) {
int dx = -1;
int dy = 1 - i;
for (int j = 1; j < i * 2; j++) {
int nx = p[k][0]+dx;
int ny = p[k][1]+dy;

dx = (j<i)?dx-1:dx+1;
dy++;
if(nx<=0||ny<=0||ny>m)
continue;
if(map_cpy[nx][ny]==1){
if(status_cpy[nx][ny]){
cnt++;
status_cpy[nx][ny]=false;
killed.add(new pair(nx,ny));
}
find=true;
break;
}
}
if(find)
break;
}
p[k][0]--;
}
while(!killed.isEmpty()){
pair pa = killed.remove();
int nx = pa.f;
int ny = pa.s;
map_cpy[nx][ny]=0;
}
}
ans = Math.max(ans, cnt);
return;
}

static void DFS(int val, int idx) {
if (idx == 3) {
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++) {
map_cpy[i][j] = map[i][j];
status_cpy[i][j] = status[i][j];
}
play();
return ;
}
for (int i = val; i <= m; i++) {
arrow[idx] = i;
DFS(i + 1, idx + 1);
}
}

static int[][] map = new int[16][16];
static int[][] map_cpy = new int[16][16];
static boolean[][] status = new boolean[16][16];
static boolean[][] status_cpy = new boolean[16][16];
static int n, m, ans, d;
static int[] arrow = new int[3];

static Reader r = new Reader();
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
public static void main(String[] args) throws IOException {
n = r.nextInt();
m = r.nextInt();
d = r.nextInt();
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++) {
map[i][j] = r.nextInt();
if(map[i][j]==1)
status[i][j]=true;
}

DFS(1, 0);
System.out.println(ans);

}

static class pair {
public int f, s;
public pair(int f, int s) {
this.f = f;
this.s = s;
}
}

static class Reader {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;

public Reader() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}

public int nextInt() throws IOException {
int ret = 0, flag = 1;
byte c = read();
if(c == '-') {
flag = -1;
c = read();
}
do {
ret = (ret<<3) + (ret<<1) + (c & 0b1111);
} while ((c = read()) >= '0' && c <= '9');

return ret * flag;
}
public String readLine() throws IOException {
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1)
{
if (c == '\n')
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}


private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}

private byte read() throws IOException {
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
}
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/07/30/PS/BOJ/17135/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.