#include <stdio.h> #include <iostream> #include <set> #include <queue> #include <vector> #include <string> #include <algorithm> using namespace std; int map[55][55]; int mapCopy[55][55]; int row, col; int color; int roomCnt; int roomMax = 1; int crashMax; int roomArea[2510]; queue<pair<int, int>> q; int visited[55][55]; int dx[4] = {0, -1, 0, 1}; // 서 북 동 남 int dy[4] = {-1, 0, 1, 0}; // 서 북 동 남 int safe(int x, int y) { if(x >= 0 && x < row && y >= 0 && y < col) { return 1; } else { return 0; } } void BFS(int x, int y, int color) { int areaCnt = 1; mapCopy[x][y] = color; visited[x][y] = 1; q.push({x, y}); while(!q.empty()) { x = q.front().first; y = q.front().second; q.pop(); for(int i = 0; i < 4; i++) { // if((map[x][y] & (1 << i)) != 0) // { // continue; // } int xpos = x+dx[i]; int ypos = y+dy[i]; if(safe(xpos, ypos) == 1 && visited[xpos][ypos] == 0 && (map[x][y] & (1 << i)) == 0) { visited[xpos][ypos] = 1; q.push({xpos, ypos}); mapCopy[xpos][ypos] = color; areaCnt++; if(roomMax < areaCnt) { roomMax = areaCnt; } } } } roomArea[++roomCnt] = areaCnt; } void crash_wall() { for(int i = 0; i < row; i++) { for(int j = 0; j < col; j++) { for(int k = 0; k < 4; k++) { int x = i+dx[k]; int y = j+dy[k]; if(safe(x, y) == 1 && mapCopy[i][j] != mapCopy[x][y]) { if(crashMax < roomArea[mapCopy[i][j]] + roomArea[mapCopy[x][y]]) { crashMax = roomArea[mapCopy[i][j]] + roomArea[mapCopy[x][y]]; } } } } } } int main(void) { // freopen("B2234_input.txt", "r", stdin); cin >> col >> row; for(int i = 0; i < row; i++) { for(int j = 0; j < col; j++) { cin >> map[i][j]; } } for(int i = 0; i < row; i++) { for(int j = 0; j < col; j++) { if(visited[i][j] == 0) { ++color; BFS(i, j, color); } } } crash_wall(); cout << roomCnt << endl; cout << roomMax << endl; cout << crashMax << endl; return 0; } | cs |
'Baekjoon > BFS' 카테고리의 다른 글
[백준 5427] 불 (BFS) (C/C++) (★) (0) | 2019.12.05 |
---|---|
[백준 3055] 탈출 (BFS) (C/C++) (★) (0) | 2019.12.05 |
[백준 12886] 돌 그룹 (BFS) (C/C++) (★) (0) | 2019.12.05 |
[백준 14442] 벽 부수고 이동하기2 (BFS) (C/C++) (★) (0) | 2019.12.04 |
[백준 2206] 벽 부수고 이동하기 (BFS) (C/C++) (0) | 2019.12.04 |