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
- classloader
- 포함관계
- LSP
- 제네릭스
- Operating System
- generics
- 도커네트워크
- 42서울
- 운영체제
- SRP
- 바이트코드
- javac
- pg_hba.conf
- 라피신
- RDD
- java
- 참조변수
- 자바컴파일러
- 42seoul
- JIT
- 상속관계
- JVM
- StackArea
- 이노베이션아카데미
- Compiler
- MethodArea
- la-piscine
- HeapArea
- jdk
- abstract
Archives
- Today
- Total
while(1) 작심삼일();
[백준 17142번] 연구소 3 본문
https://www.acmicpc.net/problem/17142
17142번: 연구소 3
인체에 치명적인 바이러스를 연구하던 연구소에 승원이가 침입했고, 바이러스를 유출하려고 한다. 바이러스는 활성 상태와 비활성 상태가 있다. 가장 처음에 모든 바이러스는 비활성 상태이고
www.acmicpc.net
풀이)
1. 바이러스 시작점을 dfs를 통해 조합을 구성한다.
2. 이후 퍼트리는 지역이 0거나 2이면서 게임이 끝나지 않은 상황이면 이면 다음 값들을 큐에 넣고 bfs를 진행한다.
3. 끝난시점에서 전부 퍼트려진 상태가 됐을 때 중에서 가장 적은 시간을 출력한다.
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
vector <pair<int, int> > virus;
int map[51][51], map_c[51][51];
int N, M, ans = 2e9, test = 0;
int dir_x[4] = {-1, 1, 0, 0};
int dir_y[4] = {0, 0, 1, -1};
int selected[11];
void copy()
{
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
map[i][j] = map_c[i][j];
}
int endCheck()
{
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
if (map[i][j] == 0)
return (0);
return (1);
}
void spreadVirus()
{
int total = 0;
queue <pair<pair<int, int>, int> > q;
for (int i = 0; i < virus.size(); i++)
{
if (selected[i])
{
q.push(make_pair(make_pair(virus.at(i).first, virus.at(i).second), 1));
map[virus[i].first][virus[i].second] = 3;
}
}
while (!q.empty())
{
int x = q.front().first.first;
int y = q.front().first.second;
int second = q.front().second;
q.pop();
for (int i = 0; i < 4; i++)
{
int nx = x + dir_x[i];
int ny = y + dir_y[i];
if (nx < 0 || nx > N - 1 || ny < 0 || ny > N - 1)
continue ;
if (map[nx][ny] == 0 || (map[nx][ny] == 2 && !endCheck()))
{
map[nx][ny] = second;
q.push(make_pair(make_pair(nx, ny), second + 1));
total = second > total ? second : total;
}
}
}
if (endCheck())
ans = total < ans ? total : ans;
}
void permutation(int size, int cnt)
{
test++;
if (size > virus.size() - 1)
return ;
if (cnt == M)
{
copy();
spreadVirus();
return ;
}
selected[size + 1] = 1;
permutation(size + 1, cnt + 1);
selected[size + 1] = 0;
permutation(size + 1, cnt);
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> N >> M;
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
{
cin >> map[i][j];
if (map[i][j] == 2)
virus.push_back(make_pair(i, j));
if (map[i][j] == 1)
map[i][j] = -1;
map_c[i][j] = map[i][j];
}
for (int i = 0; i < virus.size() - M + 1; i++)
{
selected[i] = 1;
permutation(i, 1);
selected[i] = 0;
}
if (ans == 2e9)
cout << "-1\n";
else
cout << ans << "\n";
return (0);
}
제 코드는 절대 완벽하지 않습니다
더 좋은 풀이방법 혹은 코드에 관한 질문이 있으시면 언제든 댓글주세요!
'CS > baekjoon' 카테고리의 다른 글
[백준 1238번] 파티 (0) | 2021.04.13 |
---|---|
[백준 17406번] 배열 돌리기 4 (0) | 2021.04.12 |
[백준 2580번] 스도쿠 (0) | 2021.04.09 |
[백준 2573번] 빙산 (0) | 2021.04.09 |
[백준 2458번] 키 순서 (0) | 2021.04.09 |
Comments