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 |
Tags
- JWT
- 알고풀자
- 스터디
- node
- Enhanced Input System
- 정글사관학교
- 언리얼뮤지컬
- Express
- 언리얼프로그래머
- Unseen
- Jinja2
- 스마일게이트
- R
- 프린세스메이커
- EnhancedInput
- Bootstrap4
- 레베카
- 디자드
- Ajax
- flask
- 으
- VUE
- 프메
- 데이터베이스
- 카렌
- 언리얼
- 미니프로젝트
- 마인크래프트뮤지컬
- 파이썬서버
- 게임개발
Archives
- Today
- Total
Today, I will
[DFS, python] 연속된 1의 개수 세기 본문
DFS 코드
# N*N크기의 배열이 주어졌을때 1의 개수는 몇개인지 세어보기 dfs를 이용해서
# 하나의 시작 1로 부터 붙어져 있는 연속된 1의 개수 세어보기 => 2, 13이 답이 됨.
# 7
# 0000011
# 0000000
# 0011100
# 0010111
# 0110010
# 0011100
# 0000000
cnt = 0
# 상하좌우
dx = [0,0,-1,1] # 열
dy = [-1,1,0,0] # 행
def dfs(_y, _x):
global arr, cnt
arr[_y][_x] = 0
c_y = _y
c_x = _x
for k in range(4):
n_y = c_y +dy[k]
n_x = c_x + dx[k]
if 0 <= n_y and 0 <= n_x and n > n_y and n > n_x and arr[n_y][n_x] == 1:
arr[c_y][c_x] = 0
cnt += 1
dfs(n_y, n_x)
n = int(input())
arr =[]
for _ in range(n):
arr.append(list(map(int,input())))
for y in range(len(arr)):
for x in range(len(arr[y])):
if arr[y][x] == 1:
cnt = 1
dfs(y, x)
print(cnt)
'Computer Science > 알고리즘' 카테고리의 다른 글
[DFS, SWEA] 미로 (0) | 2023.12.22 |
---|---|
[SWEA 완전탐색] 파리퇴치(4중 포문 이용!) (0) | 2023.12.22 |
[SWEA][파이썬] 회문, 파이썬에서 2차원 배열 deepcopy, .strip()을 통한 ValueError: invalid literal for int() with base 10 해결 (1) | 2023.12.03 |
[SWEA][파이썬] 전기버스 (1) | 2023.12.03 |
[Python] 백준 빗물 14719 - Monotonic stack (0) | 2023.04.11 |