문제) 백준 - DFS - 양
https://www.acmicpc.net/problem/3184
3184번: 양
첫 줄에는 두 정수 R과 C가 주어지며(3 ≤ R, C ≤ 250), 각 수는 마당의 행과 열의 수를 의미한다. 다음 R개의 줄은 C개의 글자를 가진다. 이들은 마당의 구조(울타리, 양, 늑대의 위치)를 의미한다.
www.acmicpc.net
DFS를 통해 각 영역을 구분합니다. 영역을 구분하면서 영역에 존재하는 양과 늑대의 개수를 세어줍니다. 양이 많을 경우 양의 개수만 더하고, 늑대가 많을 경우 늑대의 개수만 더해주면 해결할 수 있습니다.
C++ 소스코드)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void dfs(int x, int y){ | |
visited[x][y] = true; | |
if(board[x][y] == 'o') | |
SheepNum++; | |
else if(board[x][y] == 'v') | |
WolfNum++; | |
for(int i = 0; i < 4; ++i){ | |
int nx = x + Dir[i].first; | |
int ny = y + Dir[i].second; | |
if(valid(nx, ny) && board[nx][ny] != '#' && !visited[nx][ny]) | |
dfs(nx, ny); | |
} | |
} |
Full Code)
https://github.com/Chocochip101/BOJ_Solution/blob/main/Solution/3184_%EC%96%91.cpp
GitHub - Chocochip101/BOJ_Solution: BOJ Solutions
BOJ Solutions. Contribute to Chocochip101/BOJ_Solution development by creating an account on GitHub.
github.com
반응형
'PS(Problem Solving) > 백준_BOJ' 카테고리의 다른 글
[백준] 1633번 - 최고의 팀 만들기 (C++) 문제 및 풀이 (0) | 2022.02.12 |
---|---|
[백준] 4803번 - 트리 (C++) 문제 및 풀이 (0) | 2022.02.12 |
[백준] 1032번 - 명령 프롬프트 (C++) 문제 및 풀이 (0) | 2022.02.12 |
[백준] 19238번 - 스타트 택시 (C++) 문제 및 풀이 (0) | 2022.02.10 |
[백준] 21000번 - Archer Vlad (C++) 문제 및 풀이 (0) | 2022.02.10 |
댓글