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 |
Tags
- NLP
- 그래프이론
- 지진대피소
- 공공데이터
- 자연어처리
- dp
- 우선순위큐
- 비트마스킹
- 코사인유사도
- 유클리드
- 그래프탐색
- 재귀
- TF-IDF
- 누적합
- 수학
- pandas
- 백준
- 구현
- xmltodict
- 건축물대장정보
- Geocoding
- 전처리
- 유사도
- GroupBy
- 그리디
- 깊이우선탐색
- cosine
- 분할정복
- 너비우선탐색
- geopy
Archives
- Today
- Total
정리용
[백준 2667] 파이썬 - 단지번호 본문
https://www.acmicpc.net/problem/2667
2667번: 단지번호붙이기
<그림 1>과 같이 정사각형 모양의 지도가 있다. 1은 집이 있는 곳을, 0은 집이 없는 곳을 나타낸다. 철수는 이 지도를 가지고 연결된 집의 모임인 단지를 정의하고, 단지에 번호를 붙이려 한다. 여
www.acmicpc.net
1. 코드 설명
from collections import deque
n=int(input())
arr = [list(map(int, input())) for _ in range(n)]
done =[[0] * n for _ in range(n)]
dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]
def bfs (x,y,cnt) :
q = deque()
q.append((x,y))
done[x][y] = cnt
while q :
x,y = q.popleft()
for i in range(4):
nx = dx[i] + x
ny = dy[i] + y
if 0 <= nx < n and 0 <= ny < n :
if arr[nx][ny] ==1 and done[nx][ny] ==0 :
q.append((nx,ny))
done[nx][ny] = cnt
cnt = 0
for i in range(n) :
for j in range(n) :
if arr[i][j] ==1 and done[i][j] ==0 :
cnt += 1
bfs(i,j,cnt)
print (cnt)
# 2차원인 done 을 1차원으로 변환 후 Counter 사용
from collections import Counter
ans = Counter( sum(done, []) )
ans.pop(0, None) #del ans[0]
ans = sorted(list(ans.values()))
for i in ans :
print(i)
2. 주의사항
정답을 출력할떄
ans.pop[0]은 keyerror 가 발생하고
del ans[0]은 발생하지 않는다
ans.pop(0, None) 는 오류가 발생하지 않는 것으로 보아
0 이라는 key 가 존재하지 않을때 keyerror가 발생하는 모양인데
그렇다면 왜 del ans[0]은 맞다고 채점될까??
'알고리즘 > 백준' 카테고리의 다른 글
[백준 14501] (0) | 2021.12.17 |
---|---|
[백준 9461] 파이썬 - 파도반 수열 (0) | 2021.12.16 |
[백준 11727] 파이썬 - 2xn 타일링 2 (0) | 2021.12.15 |
[백준 2178] 파이썬 - 미로탐색 (0) | 2021.12.14 |
[백준 10844] (0) | 2021.12.13 |
Comments