Coding Test - cpp/DFS
[백준 1743: 음식물 피하기] - C++
에드윈H
2021. 2. 4. 18:40
1743번: 음식물 피하기
첫째 줄에 통로의 세로 길이 N(1 ≤ N ≤ 100)과 가로 길이 M(1 ≤ M ≤ 100) 그리고 음식물 쓰레기의 개수 K(1 ≤ K ≤ 10,000)이 주어진다. 그리고 다음 K개의 줄에 음식물이 떨어진 좌표 (r, c)가 주어진
www.acmicpc.net
#include<iostream>
#include<string>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
int map[100][100];
int check[100][100];
int n, m;
int k;
int dir[4][2] = {
{-1,0}
,{0,1}
,{1,0}
,{0,-1}
};
int cnt = 0;
void D(int _x,int _y)
{
check[_x][_y] = 1;
for (int i = 0; i < 4; i++)
{
int nextX = _x + dir[i][0];
int nextY = _y + dir[i][1];
if(nextX<1 || nextY<1 || n<nextX || m<nextY)
continue;
if (map[nextX][nextY] == 1 && check[nextX][nextY]==0)
{
cnt++;
D(nextX, nextY);
}
}
}
int main()
{
cin >> n >> m >> k;
if (n < 1 || m < 1 || k < 1)
{
cout << 0 << endl;
return 0;
}
int x;
int y;
for (int i = 0; i < k; i++)
{
cin >> x >> y;
map[x][y] = 1;
}
int max = cnt;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
if (map[i][j] == 1 && check[i][j] == 0)
{
cnt = 1;
D(i, j);
if (max < cnt)
max = cnt;
}
}
}
cout << max << endl;
return 0;
}