-
BOJ 17141 [연구소 2]알고리즘 풀이/BFS(너비 우선 탐색) 2021. 3. 4. 23:08
BOJ 연구소 2 : www.acmicpc.net/problem/17141
17141번: 연구소 2
인체에 치명적인 바이러스를 연구하던 연구소에 승원이가 침입했고, 바이러스를 유출하려고 한다. 승원이는 연구소의 특정 위치에 바이러스 M개를 놓을 것이고, 승원이의 신호와 동시에 바이
www.acmicpc.net
옛날에 연구소는 풀어봤는데, 2탄도 있는줄 몰랐다.
나의 풀이 방법은 다음과 같다.
- 바이러스를 놓을 수 있는 구역을 먼저 체크해 vector에 담아놓는다.
- 브루트 포스를 이용해 바이러스를 놓는 모든 경우의 수를 체크한다.
- BFS로 바이러스를 확산시켜본다.
제출하니 420ms나 걸린다...
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200#include <iostream>#include <queue>#include <vector>using namespace std;struct Node{int x;int y;int depth;public:Node(int x, int y, int depth){this->x = x;this->y = y;this->depth = depth;}};struct Point{int x;int y;public:Point(int x, int y){this->x = x;this->y = y;}};int N, M;int result = 999999;int lab[50][50];int corona[50][50];bool visited[50][50];int dx[] = { -1,0,0,1 };int dy[] = { 0,-1,1,0 };queue<Node> q;vector<Point> selectedPoint;vector<Point> virusPoint;void InitializeVisit(){for (int i = 0; i < N; i++){for (int j = 0; j < N; j++){visited[i][j] = false;}}}void InputVirus(){cin >> N >> M;for (int i = 0; i < N; i++){for (int j = 0; j < N; j++){cin >> lab[i][j];if(lab[i][j] == 2){virusPoint.push_back(Point(i, j));}}}}void CopyLab(){for (int i = 0; i < N; i++){for (int j = 0; j < N; j++){switch (lab[i][j]){case 1:corona[i][j] = -1;break;case 0:case 2:corona[i][j] = 0;break;}}}}bool IsBoundary(int x, int y){return x >= 0 && x < N && y >= 0 && y < N;}int GetResult(){int time = 0;CopyLab();for (int i = 0; i < selectedPoint.size(); i++){int x = selectedPoint[i].x;int y = selectedPoint[i].y;corona[x][y] = 1;q.push(Node(x, y, 0));}while (!q.empty()){Node node = q.front();q.pop();time = node.depth;for (int j = 0; j < 4; j++){int nextX = node.x + dx[j];int nextY = node.y + dy[j];if (IsBoundary(nextX, nextY) && corona[nextX][nextY] == 0){corona[nextX][nextY]++;q.push(Node(nextX, nextY, node.depth + 1));}}}return time;}bool EmptyCheck(){for (int i = 0; i < N; i++){for (int j = 0; j < N; j++){if (corona[i][j] == 0){return false;}}}return true;}void SelectPoint(int depth, int index){if (depth == M || index == N * N){int time = GetResult();if (EmptyCheck()){if (time <= result){result = time;}}return;}int size = virusPoint.size();for (int i = index; i < size; i++){int x = virusPoint[i].x;int y = virusPoint[i].y;selectedPoint.push_back(Point(x, y));SelectPoint(depth + 1, i + 1);selectedPoint.pop_back();SelectPoint(depth, i + 1);}}int main(){ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);InputVirus();SelectPoint(0, 0);if (result == 999999){result = -1;}cout << result << endl;return 0;}cs 반응형'알고리즘 풀이 > BFS(너비 우선 탐색)' 카테고리의 다른 글
BOJ 9184 [신나는 함수 실행] (0) 2021.03.07 BOJ 17836 [공주님을 구해라!] (0) 2021.03.07 BOJ 11559 [Puyo Puyo] (4) 2018.10.04 BOJ 2146 [다리 만들기] (2) 2018.10.04 BOJ 1726 [로봇] (0) 2018.10.04