반응형

문제 출처 :


https://www.acmicpc.net/problem/17135



알고리즘 분석 :


문제 해결에 필요한 사항

1. 구현


문제 해결을 위해 지문을 읽고 그대로 따라 내려가면 된다.


자세한 내용은 아래 주석을 통해 달아두었다.





소스 코드 : 


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
#include <iostream>
#include <cstdio>
#include <queue>
#include <algorithm>
#include <vector>
#include <cmath>
 
using namespace std;
 
typedef pair<intint> pii;
 
int archer[3];
int arr[15][15];
int n, m, d;
 
vector<pii> enemy, etmp;
 
int y, x;
 
// 궁수와 가장 가까운 순서대로, 같다면 가장 왼쪽에 있는 순서대로 정렬
bool comp(const pii &a, const pii &b) {
    int d1 = abs(a.first - y) + abs(a.second - x);
    int d2 = abs(b.first - y) + abs(b.second - x);
    
    if (d1 == d2)
        return a.second < b.second;
 
    return d1 < d2;
}
pii solve(int here) {
    // y,x :: 궁수의 좌표
    y = n;
    x = archer[here];
 
    // 궁수 좌표를 기준으로 정렬
    sort(enemy.begin(), enemy.end(), comp);
 
    // 만약 가장 가까운 적이 d 이하이면 첫번째 적 리턴
    if (abs(enemy[0].first - y) + abs(enemy[0].second - x) <= d)
        return enemy[0];
 
    // 해당하는 적이 없음
    return { -1,-};
}
int main() {
    scanf("%d %d %d"&n, &m, &d);
 
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
        {
            scanf("%d"&arr[i][j]);
            // 적이면 etmp에 좌표 push
            if (arr[i][j])
                etmp.push_back({ i,j });
        }
 
    int ans = 0;
    // 3중 포문으로 궁수 위치 지정
    for (int i = 0; i < m - 2; i++) {
        for (int j = i + 1; j < m - 1; j++) {
            for (int k = j + 1; k < m; k++) {
                archer[0= i;
                archer[1= j;
                archer[2= k;
                int killCnt = 0;
 
                enemy = etmp;
                while (!enemy.empty()) {
                    // 이번 턴에 궁수가 죽이는 적을 kill 배열에 저장
                    pii kill[3];
                    for (int t = 0; t < 3; t++)
                        kill[t] = solve(t);
 
                    // 궁수가 죽인 적을 제외하고 tmp에 남은 적 push
                    // 이때 적의 좌표 + 1(아래로 이동)이 n 미만인것들만 push
                    vector<pii> tmp;
                    for (int a = 0; a < enemy.size(); a++) {
                        bool isKill = false;
                        for (int b = 0; b < 3; b++) {
                            if (enemy[a] == kill[b]) {
                                isKill = true;
                                break;
                            }
                        }
                        if (isKill) {
                            killCnt++;
                            continue;
                        }
                        if (enemy[a].first + < n)
                            tmp.push_back({ enemy[a].first + 1, enemy[a].second });
                    }
 
                    ans = max(ans, killCnt);
 
                    enemy = tmp;
                }
            }
        }
    }
 
    printf("%d\n", ans);
 
    return 0;
}
 
cs

반응형

'Applied > 알고리즘 문제풀이' 카테고리의 다른 글

[1257번] K번째 문자열  (0) 2019.04.21
[17136번] 색종이 붙이기  (0) 2019.04.11
[14923번] 미로 탈출  (0) 2019.04.09
[SwExpertAcademy] 비밀  (0) 2019.04.04
[17069번] 파이프 옮기기 2  (0) 2019.04.02