[백준 1743/ C++] 음식물 피하기

이진중·2022년 5월 29일
0

알고리즘

목록 보기
35/76

음식물 피하기


문제

유기농 배추와 99% 동일한 문제이다.


풀이

[백준 1012/C++] 유기농 배추


주의할점

x,y만 조심하면 된다 r:y, c:x에 해당한다. board[y][x]이다.


코드

#include <iostream>
#include <vector>
#include <algorithm>
#include <fstream>
#include <math.h>
#include <string>
#include <string.h>
#include <queue>
using namespace std;
#define endl "\n"

#define MAX 100+1
int n, m, k;
bool board[MAX][MAX];
bool visited[MAX][MAX];
int isMax;
int ans;
int dy[] = { 0,0,-1,1 };
int dx[] = { 1,-1,0,0 };


void dfs(int y, int x) {
	visited[y][x] = true;
	isMax++;

	for (int i = 0; i < 4; i++)
	{
		int nx = dx[i] + x;
		int ny = dy[i] + y;

		if (nx < 1 || nx > m || ny < 1 || ny > n) 
		{
			continue;
		}

		if (board[ny][nx] && !visited[ny][nx])
		{
			dfs(ny, nx);
		}
	}

}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);
	//ifstream cin; cin.open("input.txt");

	cin >> n >> m >> k;

	while (k--)
	{
		int x, y;
		cin >> y >> x;
		board[y][x] = true;
	}

	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= m; j++)
		{
			if (board[i][j] && !visited[i][j])
			{
				isMax = 0;
				dfs(i, j);
				if (ans < isMax)
				{
					ans = isMax;
				}
			}
		}
	}

	cout << ans;
}

참고

https://scarlettb.tistory.com/96 // x,y 변수 참고

0개의 댓글