|
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
|
#include <iostream>
#include <vector>
#include <queue>
#include<string>
#include <algorithm>
#include <cstring> //memset
#include<tuple>
using namespace std;
typedef long long ll;
typedef pair<int, int > pii;
const ll MAX = 1000000000;
const int INF = 87654321;
int N, M, D;
int arr[20][20];
int carr[20][20];
int dy[3] = { 0,-1,0 };
int dx[3] = { -1,0,1 };
int visited[20][20];
int check[20][20];
int ans = 0;
vector<int> arc;
void copy() {
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
carr[i][j] = arr[i][j];
}
void reset() {
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
visited[i][j] = 0;
}
void resetChk() {
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
check[i][j] = 0;
}
//궁수 화살쏴서 적 몇명잡는지 체크
void BFS() {
copy();
resetChk();
int pos = N;
while (pos >= 1) {
for (int i = 0; i < arc.size(); i++) {
reset();
queue< pii> q;
q.push({ pos - 1,arc[i] });
while (!q.empty()) {
int y = q.front().first;
int x = q.front().second;
q.pop();
visited[y][x] = 1;
//만약 사격거리안에서 적이발견되면 쏜다.
if (carr[y][x] == 1) {
check[y][x] = 1;
break;
}
for (int j = 0; j < 3; j++) {
int ny = y + dy[j];
int nx = x + dx[j];
if (0 <= ny && ny < N && 0 <= nx && nx < M && abs(arc[i] - nx) + abs(pos - ny) <= D && !visited[ny][nx]) {
q.push({ ny,nx });
}
}
}
}
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
if (check[i][j]) {
carr[i][j] = 0;
}
pos--;
}
int ret = 0;
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
if (check[i][j])
ret++;
ans = max(ans, ret);
}
//궁수 위치 3개 고르기
void DFS(int pos, int cnt) {
if (cnt > 3) return;
if (cnt == 3) {
BFS();
return;
}
if (pos >= M) return;
arc.push_back(pos);
DFS(pos + 1, cnt + 1);
arc.pop_back();
DFS(pos + 1, cnt);
}
int main(void) {
cin >> N >> M >> D;
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
cin >> arr[i][j];
}
}
DFS(0, 0);
cout << ans;
}
|
cs |
처음문제 풀때 접근을 병사들이 공격할때 좌표를 내리는 식으로 구현했었는데,
궁수들이 올라가는방식이 구현하기 훨씬 쉽네요.
동시에 공격하는 것도 적을 죽이면 좌표에서 바로 지우지 않고 check만 해둔뒤 3명의 궁수가 공격이 끝나면
그제서야 적을 지우는 것도 놓쳤습니다.
'알고리즘 > BAEKJOON' 카테고리의 다른 글
| 백준 17143번 낚시왕 (0) | 2021.05.23 |
|---|---|
| 백준 5014번 스타트 링크 시간초과 (0) | 2021.05.15 |
| 백준 14502번 연구소 (0) | 2019.09.19 |
| 백준 2805번 나무자르기 (0) | 2019.09.15 |
| 백준 11659번 구간합 구하기4 (0) | 2019.09.15 |