(백준 알고리즘 문제풀이) 14502번 연구소
by 줌코딩
문제
어떻게 접근할 것인가
- DFS를 이용해서 벽을 3개 설치할 수 있는 경우의 수를 모두 찾아주고 각 경우에서 BFS를 진행한다.
- BFS를 진행한 후에 남은 빈 공간의 갯수를 세서 MAX보다 크면 MAX를 업데이트 해준다.
코드
#include <cstdio>
#include <vector>
#include <queue>
#define pii pair<int, int>
using namespace std;
int d[4][2] = { {1, 0}, {-1, 0}, {0, 1}, {0, -1} };
int n, m, arr[8][8], MAX;
void bfs(){
int check[8][8] = {0,};
queue<pii> q;
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++)check[i][j] = arr[i][j];
}
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
if(check[i][j] == 2){
q.push(pii(i, j));
while(!q.empty()){
int fx = q.front().first, fy = q.front().second;
q.pop();
for(int i = 0; i < 4; i++){
int nx = fx + d[i][0], ny = fy + d[i][1];
if(nx < 0 || ny < 0 || nx >= n || ny >= m)continue;
if(check[nx][ny] == 1)continue;
check[nx][ny] = 1;
q.push(pii(nx, ny));
}
}
}
}
}
int cnt = 0;
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
if(!check[i][j]) cnt++;
}
}
if(cnt > MAX)MAX = cnt;
}
void dfs(int x, int y, int cnt){
if(cnt == 3){
bfs();
return;
}
for(int j = y; j < m; j++){
if(arr[x][j] == 0) {
arr[x][j] = 1;
dfs(x, j, cnt + 1);
arr[x][j] = 0;
}
}
for(int i = x + 1; i < n; i++){
for(int j = 0 ; j < m; j++){
if(arr[i][j] == 0) {
arr[i][j] = 1;
dfs(i, j, cnt + 1);
arr[i][j] = 0;
}
}
}
}
int main(){
scanf("%d %d", &n, &m);
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++)scanf("%d", &arr[i][j]);
}
dfs(0, 0, 0);
printf("%d", MAX);
}
느낀점
- BFS와 DFS를 깔끔하게 사용해서 맞아버렸다.
- 이게 제일 효율적인 방법은 아닌 것 같지만 일단 풀었다는 사실에 만족이다!
이 포스팅은 쿠팡 파트너스 활동의 일환으로, 이에 따른 일정액의 수수료를 제공받습니다.
Subscribe via RSS