출처: https://bumcrush.tistory.com/182 [맑음때때로 여름]
#include <iostream>
using namespace std;
const int MAX = 500;
typedef struct
{
int x, y;
} Dir;
Dir dirMove[4] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
bool visited[MAX][MAX];
int graph[MAX][MAX];
int n, m;
int cnt;
int area;
int maxArea;
void dfs(int x, int y)
{
visited[x][y] = true;
area++;
if (area > maxArea)
maxArea = area;
for (int i = 0; i < 4; i++)
{
int nextX = x + dirMove[i].x;
int nextY = y + dirMove[i].y;
if (!visited[nextX][nextY] && graph[nextX][nextY])
{
if (0 <= nextX && nextX < n && 0 <= nextY && nextY < m)
{
dfs(nextX, nextY);
}
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> m;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cin >> graph[i][j];
}
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (graph[i][j] && !visited[i][j])
{
dfs(i, j);
area = 0;
cnt++;
}
}
}
cout << cnt << "\n";
cout << maxArea;
}
그냥 일반적인 dfs 모양의 문제.
백준 5567 결혼식(bfs) (0) | 2020.08.26 |
---|---|
백준 2636 치즈(bfs) (0) | 2020.08.26 |
백준 1963 소수 경로(에스테라노스의 체, bfs) (0) | 2020.08.19 |
백준 3055 탈출(bfs) (0) | 2020.08.14 |
백준 14891 톱니바퀴(구현, 시뮬레이션) (0) | 2020.08.13 |
댓글 영역