#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<intint>> q;
int visited[55][55];
 
int dx[4= {0-101}; // 서 북 동 남 
int dy[4= {-1010}; // 서 북 동 남
 
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

+ Recent posts