문제) 백준 - DFS - 맥주 마시면서 걸어가기
https://www.acmicpc.net/problem/9205
9205번: 맥주 마시면서 걸어가기
송도에 사는 상근이와 친구들은 송도에서 열리는 펜타포트 락 페스티벌에 가려고 한다. 올해는 맥주를 마시면서 걸어가기로 했다. 출발은 상근이네 집에서 하고, 맥주 한 박스를 들고 출발한다.
www.acmicpc.net
좌표를 모두 입력받아 20*50을 넘는 점을 제외하고 graph에 인접 리스트 형태로 나타냅니다. DFS를 통해 탐색하면서 visited [N+1] 번째를 방문 여부에 따라 'happy' 또는 'sad'를 출력합니다.
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
while (tc--) { | |
for (int i = 0; i < MAX; ++i) | |
graph[i].clear(); | |
memset(visited, false, sizeof(visited)); | |
cin >> N; | |
for (int i = 0; i < N + 2; ++i) | |
cin >> coord[i].first >> coord[i].second; | |
for(int i = 0; i <N + 2;++i) | |
for (int j = i + 1; j < N + 2; ++j) | |
if (dist(coord[i], coord[j]) <= 20 * 50) { | |
graph[i].push_back(j); | |
graph[j].push_back(i); | |
} | |
dfs(0); | |
if (visited[N + 1]) | |
cout << "happy" << endl; | |
else cout << "sad" << endl; | |
} |
Full code)
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' 카테고리의 다른 글
[백준] 13549번 - 숨바꼭질 3 (C++) 문제 및 풀이 (0) | 2021.12.28 |
---|---|
[백준] 17142번 - 연구소 3 (Python) 문제 및 풀이 (0) | 2021.12.26 |
[백준] 2636번 - 치즈 (C++) 문제 및 풀이 (0) | 2021.12.23 |
[백준] 5582번 - 공통 부분 문자열 (C++) 문제 및 풀이 (0) | 2021.12.22 |
[백준] 10803번 정사각형 만들기 (C++) 문제 및 풀이 (0) | 2021.12.20 |
댓글