Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 42seoul
- SRP
- 42서울
- 이노베이션아카데미
- Compiler
- pg_hba.conf
- StackArea
- LSP
- jdk
- 참조변수
- 자바컴파일러
- abstract
- classloader
- JIT
- 바이트코드
- 포함관계
- 라피신
- generics
- 제네릭스
- JVM
- 도커네트워크
- Operating System
- la-piscine
- MethodArea
- 운영체제
- HeapArea
- javac
- RDD
- 상속관계
- java
Archives
- Today
- Total
while(1) 작심삼일();
[백준 17135번] 캐슬 디펜스 본문
https://www.acmicpc.net/problem/17135
17135번: 캐슬 디펜스
첫째 줄에 격자판 행의 수 N, 열의 수 M, 궁수의 공격 거리 제한 D가 주어진다. 둘째 줄부터 N개의 줄에는 격자판의 상태가 주어진다. 0은 빈 칸, 1은 적이 있는 칸이다.
www.acmicpc.net
풀이)
1. 궁수를 놓을 수 있는 위치를 dfs를 통해 조합을 구성한다.
2. 맵이 비었으면 게임이 종료시킨다.
3. 궁수들이 세팅이 되었다면 각 궁수들마다 잡을 적들을 세팅해서 queue에 넣어준다. (여러 궁수가 한 적을 노릴 수도 있어서)
4. 큐를 비우면서 적이면 죽인다.
5. 적을 이동시킨다.
#include <iostream>
#include <queue>
using namespace std;
int N, M, D, ans = 0;
int map[16][16], map_c[16][16];
int archorPosition[16];
int dir_x[3] = {0, -1, 0};
int dir_y[3] = {-1, 0, 1};
queue <pair<int, int> > attackList;
void copyMap()
{
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
map[i][j] = map_c[i][j];
}
int enemyEmpty()
{
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
if (map[i][j])
return (0);
return (1);
}
void enemyMove()
{
for (int i = N - 2; i > -1; i--)
{
for (int j = 0; j < M; j++)
{
map[i + 1][j] = map[i][j];
if (i == 0)
map[i][j] = 0;
}
}
}
void addEnemy(int i)
{
queue <pair <pair<int, int>, int> > enemyPostion;
pair <pair<int, int>, int> start = make_pair(make_pair(N - 1, i), 1);
enemyPostion.push(start);
while (!enemyPostion.empty())
{
pair <pair<int, int>, int> front = enemyPostion.front();
enemyPostion.pop();
if (map[front.first.first][front.first.second] == 1)
{
attackList.push(make_pair(front.first.first, front.first.second));
break ;
}
if (front.second + 1 <= D)
{
for (int i = 0; i < 3; i++)
{
int nx = front.first.first + dir_x[i];
int ny = front.first.second + dir_y[i];
if (nx < 0 || ny < 0 || ny > M - 1)
continue ;
enemyPostion.push(make_pair(make_pair(nx, ny), front.second + 1));
}
}
}
while (!enemyPostion.empty())
enemyPostion.pop();
}
void gameStart()
{
int kill = 0;
while (1)
{
if (enemyEmpty())
break ;
for (int i = 0; i < M; i++)
{
if (archorPosition[i])
addEnemy(i);
}
while (!attackList.empty())
{
pair<int, int> front = attackList.front();
attackList.pop();
if (map[front.first][front.second] == 1)
{
map[front.first][front.second] = 0;
kill++;
}
}
enemyMove();
}
ans = kill > ans ? kill : ans;
}
void dfs(int idx, int cnt)
{
if (idx > M - 1)
return ;
if (cnt == 3)
{
copyMap();
gameStart();
return ;
}
archorPosition[idx + 1] = 1;
dfs(idx + 1, cnt + 1);
archorPosition[idx + 1] = 0;
dfs(idx + 1, cnt);
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> N >> M >> D;
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
cin >> map_c[i][j];
for (int i = 0; i < M - 2; i++)
{
archorPosition[i] = 1;
dfs(i, 1);
archorPosition[i] = 0;
}
cout << ans << "\n";
}
제 코드는 절대 완벽하지 않습니다
더 좋은 풀이방법 혹은 코드에 관한 질문이 있으시면 언제든 댓글주세요!
'CS > baekjoon' 카테고리의 다른 글
[백준 2573번] 빙산 (0) | 2021.04.09 |
---|---|
[백준 2458번] 키 순서 (0) | 2021.04.09 |
[백준 11657번] 타임머신 (0) | 2021.04.08 |
[백준 1939번] 중량제한 (0) | 2021.04.08 |
[백준 11404번] 플로이드 (0) | 2021.04.08 |
Comments