Coding Test - cpp/DFS
[백준 1926: 그림] - C++
에드윈H
2021. 2. 10. 14:22
1926번: 그림
어떤 큰 도화지에 그림이 그려져 있을 때, 그 그림의 개수와, 그 그림 중 넓이가 가장 넓은 것의 넓이를 출력하여라. 단, 그림이라는 것은 1로 연결된 것을 한 그림이라고 정의하자. 가로나 세로
www.acmicpc.net
#include<iostream>
#include <limits.h>
using namespace std;
int map[501][501];
bool check[501][501];
int cntArea = 0;
int n, m;
int dir[4][2] = {
{-1,0}
,{0,1}
,{1,0}
,{0,-1}
};
void D(int _x, int _y)
{
check[_x][_y] = true;
cntArea++;
for (int i = 0; i < 4; i++)
{
int nextX = _x + dir[i][0];
int nextY = _y + dir[i][1];
if (nextX < 0 || nextY < 0 || n <= nextX || m <= nextY)
continue;
if (map[nextX][nextY] == 1 && !check[nextX][nextY])
{
D(nextX, nextY);
}
}
}
int main() {
cin >> n >> m;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cin >> map[i][j];
}
}
int count = 0;
int maxValue = INT_MIN;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (map[i][j] == 1 && !check[i][j])
{
count++;
cntArea = 0;
D(i, j);
if (maxValue < cntArea)
{
maxValue = cntArea;
}
}
}
}
if (count == 0)
{
cout << 0 << endl;
cout << 0 << endl;
return 0;
}
cout << count << endl;
cout << maxValue << endl;
return 0;
}