-
백준 14502번 : 연구소 C언어c c++ 언어 공부 2023. 3. 7. 22:42
https://www.acmicpc.net/problem/14502
14502번: 연구소
인체에 치명적인 바이러스를 연구하던 연구소에서 바이러스가 유출되었다. 다행히 바이러스는 아직 퍼지지 않았고, 바이러스의 확산을 막기 위해서 연구소에 벽을 세우려고 한다. 연구소는 크
www.acmicpc.net
Code:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125#include <stdio.h>#include <string.h>int n, m;int arr[9][9] = { 0, };int temp[9][9] = { 0, };int temp2[9][9] = { 0, };int visit[9][9] = { 0, };int max = 0;int dx[] = { -1,1,0,0 };int dy[] = { 0,0,-1,1 };void copy(){for (int i = 0; i < n; i++){for (int j = 0; j < m; j++){temp[i][j] = arr[i][j];}}}void copy2(){for (int i = 0; i < n; i++){for (int j = 0; j < m; j++){temp2[i][j] = temp[i][j];}}}void dfs(int a,int b){visit[a][b] == 1;for (int k = 0; k < 4; k++){int x = b + dx[k];int y = a + dy[k];if (x >= 0 && x < m && y >= 0 && y < n && temp2[y][x] == 0 && visit[y][x]==0){temp2[y][x] = 2;dfs(y, x);}}}void wall(int cnt){if (cnt == 3){memset(visit, 0, sizeof(visit));copy2();for (int i = 0; i < n; i++){for (int j = 0; j < m; j++){if (temp2[i][j] == 2 && visit[i][j]==0){dfs(i, j);}}}int count = 0;for (int i = 0; i < n; i++){for (int j = 0; j < m; j++){if(temp2[i][j] == 0){count++;}}}if (max < count){max = count;}return;}else{for (int i = 0; i < n; i++){for (int j = 0; j < m; j++){if (temp[i][j] == 0){temp[i][j] = 1;wall(cnt+1);temp[i][j] = 0;}}}}}int main(){scanf("%d %d", &n, &m);int i, j = 0;for (i = 0; i < n; i++){for (j = 0; j < m; j++){scanf("%d", &arr[i][j]);}}for (i = 0; i < n; i++){for (j = 0; j < m; j++){if (arr[i][j] == 0){copy();temp[i][j] = 1;wall(1);temp[i][j] = 0;}}}printf("%d", max);}cs 이 문제는 dfs나 bfs로 풀면 풀리는 문제이다. 많은 사람들이 bfs로 푼사람이 많아서 dfs로 해결하였다.
1. 3개의 벽의 세워야 하므로 브루트포스 방식으로 3개를 세워주었다.
2. memset으로 visit 행렬을 초기화 시키며 dfs로 바이러스의 퍼짐을 확인하였다.
3. 0의 개수를 확인하며 max값을 만들어줬다.
'c c++ 언어 공부' 카테고리의 다른 글
백준 2559번 : 수열 C언어 (0) 2023.03.08 백준 10812번 : 바구니 순서 바꾸기 C언어 (0) 2023.03.08 백준 2960번 : 에라토스테네스의 체 C언어 (0) 2023.03.07 백준 2824번 : 최대공약수 C언어 (0) 2023.03.07 백준 13241번 : 최소공배수 C언어 (0) 2023.03.06