Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
📘 풀이한 문제
백준 1012 유기농배추
⭐ 문제에서 주로 사용한 알고리즘
유기농배추는 완전탐색이라 dfs, bfs 모두 사용할 수 있는데
문제풀이에서는 DFS를 이용했습니다.
📜 대략적인 코드 설명
격자의 좌상귀부터 하나씩 오른쪽으로 진행하며 모든 칸을 이동하면서 배추를 만나면 cnt를 하나씩 증가시킵니다.
그리고 해당 배추와 연결된 모든 배추(배추군락)를 트리로 보고 dfs로 탐색해서 visited를 1로 바꾸어 줍니다.
(방문하지 않은 초기값은 0)
이런식으로 모든 격자를 완전탐색을 하면 만나는 배추군락(트리) 마다 cnt가 1씩 증가하고
탐색을 마치면 배추군락(트리)의 갯수가 나오는데 이게 필요한 벌레의 수 입니다.