#include <iostream>
#include <vector>
#include <memory.h>
#include <algorithm>
using namespace std;
int N, M, D;
int board[15][15];
bool visited[15];
vector<int> archers;
vector<pair<int, int>> targets;
int ans = 0;
int numEnemy = 0;
bool cmp(pair<int, int> p1, pair<int, int> p2) {
return p1.second < p2.second; // 가장 왼쪽에 있는 순서대로
}
void setTarget() {
targets.clear();
for (int i = 0; i < archers.size(); i++) {
vector<pair<int, int>> cand;
for (int dist = 1; dist <= D; dist++) {
for(int r=N-1; r>=0; r--){
for (int c = 0; c<M; c++) {
if (board[r][c] == 0) continue;
int tdist = abs(N - r) + abs(archers[i] - c);
if (dist == tdist) {
cand.push_back(make_pair(r, c));
}
}
}
if (cand.size() > 0) break;
}
if (cand.size() > 0) {
sort(cand.begin(), cand.end(), cmp);
targets.push_back(cand[0]);
}
}
}
int removeEnemy() {
int cnt = 0;
for (int i = 0; i < targets.size(); i++) {
int tx = targets[i].first;
int ty = targets[i].second;
if (board[tx][ty] == 0) continue;
numEnemy--;
board[tx][ty] = 0;
cnt++;
}
return cnt;
}
void moveEnemy() {
for (int r = N-1; r >= 0; r--) {
for (int c = 0; c < M; c++) {
if (board[r][c] == 1) {
if (r + 1 < N) {
board[r + 1][c] = 1;
}
else {
numEnemy--;
}
board[r][c] = 0;
}
}
}
}
int protectCattle() {
int ret = 0;
int cboard[15][15];
int cNumEnemy = numEnemy;
memcpy(cboard, board, sizeof(board));
while (numEnemy > 0) {
// 궁수의 목표물 설정
setTarget();
// 공격받은 적 게임에서 제외
ret += removeEnemy();
// 적 아래로 한칸 이동
moveEnemy();
}
memcpy(board, cboard, sizeof(board));
numEnemy = cNumEnemy;
return ret;
}
void getArchers(int v) {
if (archers.size() == 3) {
ans = max(ans, protectCattle());
return;
}
for (int i = v; i < M; i++) {
if (visited[i]) continue;
visited[i] = true;
archers.push_back(i);
getArchers(i + 1);
archers.pop_back();
visited[i] = false;
}
}
int main()
{
cin.tie(NULL);
ios_base::sync_with_stdio(false);
scanf("%d %d %d", &N, &M, &D);
for (int r = 0; r < N; r++) {
for (int c = 0; c < M; c++) {
scanf("%d", &board[r][c]);
if (board[r][c] == 1) numEnemy++;
}
}
memset(visited, false, sizeof(visited));
getArchers(0);
printf("%d\n", ans);
return 0;
}
Leave a comment