Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions cmkim/BaekJoon/백준_11724.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
n, m = map(int, input().split())
arr = [[0] * (n + 1) for _ in range(n + 1)]
visited = [0] * (n + 1)
result = 0

def dfs(num):
visited[num] = 1

for i in range(1, n+1):
if visited[i] == 0 and arr[num][i] == 1:
visited[i] = 1
dfs(i)
return True



for i in range(m): #연결리스트
a, b = map(int, input().split())
arr[a][b] = 1
arr[b][a] = 1


for i in range(1, n + 1):
if visited[i] == 0:
dfs(i)
result += 1
print(result)

49 changes: 49 additions & 0 deletions cmkim/BaekJoon/백준_1600.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from collections import deque

dx, dy = [-2, -1, 1, 2, 2, 1, -1, -2], [1, 2, 2, 1, -1, -2, -2, -1] #시계방향
dx2, dy2 = [-1, 1, 0, 0], [0, 0, -1, 1] #상하좌우

k = int(input())
m, n = map(int, input().split()) # m이 가로 n이 세로
arr = []
visited = [[[0] * m for _ in range(n)] for _ in range(k+1)]
visited[0][0][0] = 1

for i in range(n): #맵 정보 입력
arr.append(list(map(int, input().split())))

q = deque()
q.append((0, 0, 0, 0)) # x좌표, y좌표, 이동비용, 말이 움직인 횟수


def bfs(): # x -> row | y -> col
while q:
x, y, count, action = q.popleft() # x, y 는 좌표 정보 | count는 움직인 횟수 | action은 말의 움직임을 쓴 횟수
if x == n - 1 and y == m - 1: #목적지 도착
print(count)
return

for i in range(4): #상하좌우 이동
nx = x + dx2[i]
ny = y + dy2[i]
if action >= k: #말의 움직임을 다 쓴 경우 visited에 최대 사용가능한 말의 움직임 k로 저장한다.
if 0 <= nx < n and 0 <= ny < m and visited[k][nx][ny] == 0 and arr[nx][ny] == 0:
visited[k][nx][ny] = 1
q.append((nx, ny, count + 1, action))
else: #말의 움직임을 다 안 쓴 경우 현재 사용한 말의 움직임을 visited에 저장
if 0 <= nx < n and 0 <= ny < m and visited[action][nx][ny] == 0 and arr[nx][ny] == 0:
visited[action][nx][ny] = 1
q.append((nx, ny, count + 1, action))

if action < k: #말의 움직임 이동 (시계방향)
for i in range(8):
nx = x + dx[i]
ny = y + dy[i]
if 0 <= nx < n and 0 <= ny < m and visited[action + 1][nx][ny] == 0 and arr[nx][ny] == 0:
visited[action + 1][nx][ny] = 1
q.append((nx, ny, count + 1, action + 1))
return -1 #큐를 다 돌렸는데 목적지 도착해서 return 되지 않은 경우 -1 return


if bfs() == -1: # 말의 이동
print(-1)
39 changes: 39 additions & 0 deletions cmkim/BaekJoon/백준_2667.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from collections import deque

n = int(input())
arr = [[0] * n for _ in range(n)]
visited = [[0] * n for _ in range(n)]
dx, dy = [-1, 1, 0 , 0], [0, 0, -1, 1] # 상하좌우 순
answer = []

def bfs():
count = 1
while q:
x, y = q.popleft() # x는 row, y는 col
visited[x][y] = 1
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if 0 <= nx < n and 0 <= ny < n and visited[nx][ny] == 0 and arr[nx][ny] == 1:
visited[nx][ny] = 1
q.append((nx, ny))
count += 1
return count


for i in range(n):
arr[i] = list(map(int, input().rstrip()))

q = deque()
for i in range(n):
for j in range(n):
if arr[i][j] and visited[i][j] == 0:
q.append((i, j))
result = bfs()
if result:
answer.append(result)

print(len(answer))
answer.sort()
for i in range(len(answer)):
print(answer[i])
2 changes: 1 addition & 1 deletion cmkim/문자열뒤집기_313p.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
temp = arr[0][i]
count += 1

print(count//2)
print((count+1)//2)