본문 바로가기

BOJ

백준 14502 / 연구소



BFS+재귀 연습문제


맵이 최대 8X8이기 때문에


맵에 벽 3개를 세운 모든 경우를 따져보면 쉽게 풀 수 있다.


이때 삼중for문보다 재귀를 사용해서 풀면 뭔가 있어보이지ㅎ



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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <stdio.h>
#include <iostream>
#include <vector>
#include <queue>
 
using namespace std;
 
int n, m;
int orimap[9][9];
int map[9][9];
int map2[9][9];
bool check[9][9];
 
vector<pair<intint>>zero;
 
vector<int>ans;
 
queue<pair<int,int>>q;
 
int dx[] = { 0,0,1,-1 };
int dy[] = { 1,-1,0,0 };
 
 
int checking() {
 
    
 
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            map2[i][j] = map[i][j];
 
            if (map2[i][j] == 2) {
                q.push(make_pair(i, j));
            }
        }
    }
    
 
 
 
    while (!q.empty()) {
        int x = q.front().first;
        int y = q.front().second;
        q.pop();
 
        for (int i = 0; i < 4; i++) {
            int nx = x + dx[i];
            int ny = y + dy[i];
 
            if (nx >= 0 && nx < n && ny >= 0 && ny < m) {
 
                if (check[nx][ny] == false && map2[nx][ny] == 0) {
                    map2[nx][ny] = 2;
                    check[nx][ny] = true;
                    q.push(make_pair(nx, ny));
                }
 
            }
 
 
        }
        
 
    }
 
    int safety = 0;
 
    
 
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            check[i][j] = false;
            if (map2[i][j] == 0) {
                safety++;
            }
        }
    }
 
    return safety;
 
}
 
 
 
void dfs(int index, int count) {
    if (count == 3) {
        ans.push_back(checking());
        return;
    }
    if (index >= zero.size())return;
 
    int x = zero[index].first;
    int y = zero[index].second;
 
    map[x][y] = 1;
    dfs(index + 1, count + 1);
    map[x][y] = 0;
    dfs(index+1, count);
 
}
 
 
 
 
 
 
int main() {
 
    //freopen("Text.txt", "r", stdin);
    cin >> n >> m;
 
    
 
 
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            cin >> orimap[i][j];
            map[i][j] = orimap[i][j];
            check[i][j] = false;
        }
    }
 
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if (orimap[i][j] == 0) {
                zero.push_back(make_pair(i, j));
            }
        }
    }
 
 
    dfs(00);
 
 
    int answer = 0;
 
    for (int i = 0; i < ans.size(); i++) {
        if (answer < ans[i]) {
            answer = ans[i];
        }
    }
 
 
    cout << answer;
 
}
cs





'BOJ' 카테고리의 다른 글

백준 11053 / 가장 긴 증가하는 부분 수열  (0) 2018.12.26
백준 15990 / 1,2,3 더하기 5  (0) 2018.12.26
백준 1463 / 1로 만들기  (0) 2018.12.20
백준 3055 / 탈출  (0) 2018.12.20
백준 2206 / 벽 부수고 이동하기  (0) 2018.12.19