Skip to content

Commit 4285d41

Browse files
committed
[BOJ] 2234 성곽 (G3)
1 parent 58d7468 commit 4285d41

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed

박예진/4주차/260121.cpp

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
//https://www.acmicpc.net/problem/2234
2+
3+
#include <iostream>
4+
#include <algorithm>
5+
#include <vector>
6+
#include <queue>
7+
using namespace std;
8+
9+
struct Node {
10+
int x, y;
11+
};
12+
13+
int N, M, arr[51][51];
14+
bool visited[51][51];
15+
int roomIdx[51][51];
16+
// 서, 북, 동, 남
17+
const int dx[4] = {0, -1, 0, 1};
18+
const int dy[4] = {-1, 0, 1, 0};
19+
20+
bool OOB(int x, int y){
21+
if (x < 0 || x >= N || y < 0 || y >= M) return true;
22+
return false;
23+
}
24+
25+
int bfs(int x, int y, int idx) {
26+
queue<Node> q;
27+
q.push({x, y});
28+
visited[x][y] = true;
29+
int cnt = 0;
30+
31+
while(!q.empty()) {
32+
Node now = q.front(); q.pop();
33+
roomIdx[now.x][now.y] = idx;
34+
cnt++;
35+
36+
for(int dir = 0; dir < 4; dir++){
37+
int nx = now.x + dx[dir];
38+
int ny = now.y + dy[dir];
39+
40+
if (OOB(nx, ny) || visited[nx][ny]) continue;
41+
if (arr[now.x][now.y] & (1 << dir)) continue; // 벽이 있다면
42+
q.push({nx, ny});
43+
visited[nx][ny] = true;
44+
}
45+
}
46+
return cnt;
47+
}
48+
49+
int main(){
50+
cin >> M >> N;
51+
for(int i = 0; i < N; i++){
52+
for(int j = 0; j < M; j++){
53+
cin >> arr[i][j];
54+
}
55+
}
56+
57+
// bfs 돌려서 방의 개수, 넓이 구하기
58+
int roomCnt = 0;
59+
int roomSize = 0;
60+
int res = 0;
61+
vector<int> rooms;
62+
63+
for(int i = 0; i < N; i++){
64+
for(int j = 0; j < M; j++){
65+
if (!visited[i][j]) {
66+
int num = bfs(i, j, roomCnt);
67+
roomCnt++;
68+
roomSize = max(roomSize, num);
69+
rooms.push_back(num);
70+
}
71+
}
72+
}
73+
74+
// 각 연결되어 있는 방 검색
75+
for(int i = 0; i < N; i++){
76+
for(int j = 0; j < M; j++){
77+
for(int dir = 0; dir < 4; dir++){
78+
int nx = i + dx[dir];
79+
int ny = j + dy[dir];
80+
81+
if (OOB(nx, ny)) continue;
82+
if (roomIdx[i][j] != roomIdx[nx][ny]) {
83+
res = max(res, rooms[roomIdx[i][j]] + rooms[roomIdx[nx][ny]]);
84+
}
85+
}
86+
}
87+
}
88+
89+
cout << roomCnt << "\n" << roomSize << "\n" << res;
90+
}

0 commit comments

Comments
 (0)