Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
Tags
- 소프티어
- 안드로이드
- 자바
- Flutter
- MVVM
- 다트
- Python
- programers
- 알고리즘
- 파이썬
- 개발
- baekjoon
- SWIFT
- 플러터
- kotlin
- aws
- DART
- 머신러닝
- 현대sw
- 프로그래머스
- VSCode
- GDSC
- softeer
- Android
- 백준
- 코틀린
- java
- 스위프트
- 아마존 웹 서비스
- 코테
Archives
- Today
- Total
조준장 개발자 생존기
[baekjoon] 백준 1245번(파이썬): 농장 관리 본문
문제
1245번: 농장 관리
첫째 줄에 정수 N(1<n≤100), m(1<m≤70)이="" 주어진다.="" 둘째="" 줄부터="" n+1번째="" 줄까지="" 각="" 줄마다="" 격자들의="" 높이를="" 의미하는="" m개의="" 정수가="" 입력된다.<="" p=""> </n≤100),>
www.acmicpc.net
알고리즘
- dfs 탐색을 통해 문제를 수행한다.
- 각 산봉우리를 탐색하면서 주변 산봉우리보다 작은지 확인한다.
- 산봉우리가 같으면 재귀적으로 탐색하여 가장 큰 산봉우리를 찾는다.
코드
import sys
sys.setrecursionlimit(10 ** 6)
# dfs 탐색
def dfs(a, b):
# 상/하/좌/우/대각선 확인
dx = [1, -1, 0, 0, 1, 1, -1, -1]
dy = [0, 0, 1, -1, 1, -1, -1, 1]
# 산봉우리인지 체크
global trigger
# 탐색 체크
visited[a][b] = True
# 8가지 방향 탐색
for i in range(8):
x = a + dx[i]
y = b + dy[i]
# 맵 안에 있으면
if -1 < x < n and -1 < y < m:
# 주변 산봉우리보다 작으면 False
if graph[a][b] < graph[x][y]:
trigger = False
# 같은 높이의 산봉우리 탐색
if not visited[x][y] and graph[x][y] == graph[a][b]:
dfs(x, y)
n, m = map(int, sys.stdin.readline().split())
# 2차원 그래프를 표현
graph = [list(map(int, sys.stdin.readline().split())) for _ in range(n)]
# 탐색 여부
visited = [[False] * m for _ in range(n)]
cnt = 0
for i in range(n):
for j in range(m):
# 산봉우리가 0보다 크고 탐색하지 않았다면
if graph[i][j] > 0 and not visited[i][j]:
trigger = True
dfs(i, j)
# 산봉우리이면 카운트
if trigger:
cnt += 1
print(cnt)
github
GitHub - junjange/CodingTest: 내가 푼 코딩 테스트 문제와 해결법
내가 푼 코딩 테스트 문제와 해결법. Contribute to junjange/CodingTest development by creating an account on GitHub.
github.com
'CodingTest > Baekjoon' 카테고리의 다른 글
[baekjoon] 백준 1389번(파이썬): 케빈 베이컨의 6단계 법칙 (0) | 2021.08.31 |
---|---|
[baekjoon] 백준 1068번(파이썬): 트리 (0) | 2021.08.30 |
[baekjoon] 백준 2823번(파이썬): 유턴 싫어 (0) | 2021.08.28 |
[baekjoon] 백준 17204번(파이썬): 죽음의 게임 (0) | 2021.08.27 |
[baekjoon] 백준 14496번(파이썬): 그대, 그머가 되어 (0) | 2021.08.26 |