Skip to content

Commit 004b35d

Browse files
authored
승연 백준 숙제 (#49)
* 5557 * 16953 * 1012 * 2644 * 11909 * 11909,11909-2 fix * 그래프 이론 실전문제 * 스타트업 코딩 페스티벌 1차 * 5,6 * 3061 * 4963 * 2178
1 parent 99a1d30 commit 004b35d

File tree

3 files changed

+130
-0
lines changed

3 files changed

+130
-0
lines changed

‎syheo/codingTest1/Baekjoon/2178.py‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#solved.ac
2+
#실버1
3+
#DFS
4+
#미로 탈출
5+
#2178
6+
7+
#visited는 리스트이므로 mutable한 속성을 갖음 -> 깊은 복사가 필요
8+
import sys,copy
9+
input = sys.stdin.readline
10+
11+
N,M = map(int,input().split())
12+
13+
maps = []
14+
for i in range(N):
15+
maps.append(list(map(int,list(input().rstrip()))))
16+
17+
#dfs
18+
def dfs(maps,v):
19+
cntList = []
20+
visited=[[False]*M for _ in range(N)]
21+
stack = [(v[0],v[1],v[2],visited)]
22+
while stack:
23+
vertex = stack.pop()
24+
#예외 처리
25+
if vertex[0]>=0 and vertex[0]<N and vertex[1]>=0 and vertex[1]<M:
26+
cnt = vertex[2]
27+
row = vertex[0]
28+
col = vertex[1]
29+
visiting = copy.deepcopy(vertex[3])
30+
#방문 가능 여부 판별
31+
if not visiting[row][col] and maps[row][col]==1:
32+
if row == N-1 and col == M-1:
33+
cntList.append(cnt)
34+
#방문 처리
35+
else:
36+
visiting[row][col]=True
37+
stack.extend([(row+1,col,cnt+1,visiting),(row-1,col,cnt+1,visiting),(row,col+1,cnt+1,visiting),(row,col-1,cnt+1,visiting)])
38+
print(min(cntList))
39+
40+
41+
dfs(maps,(0,0,1))

‎syheo/codingTest1/Baekjoon/3061.py‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#solved.ac
2+
#실버4
3+
#그리디
4+
#사다리 타기
5+
#3061
6+
import sys
7+
input = sys.stdin.readline
8+
9+
cntList = []
10+
11+
#스왑 변형 병합 메서드
12+
def swap(array,x,cur,N):
13+
if cur<x:
14+
array = array[0:cur] + array[cur+1:x+1] + array[cur:cur+1]+ array[x+1:N+1]
15+
else:
16+
array = array[0:x] + array[cur:cur+1] + array[x:cur]+array[cur+1:N+1]
17+
return array
18+
19+
for i in range(int(input())):
20+
N = int(input())
21+
#도착점이 index , 시작점이 value
22+
arr = list(map(int,input().split()))
23+
arr.insert(0,0)
24+
#ladder = [0 for _ in range(N)]
25+
#카운트 변수
26+
cnt = 0
27+
#swapping 해서 1번~N번까지 위치를 찾아주며 오름차순 정렬
28+
for j in range(1,N+1):
29+
#cur이 j의 위치로 가야 되는 index 값 0 1 6 2 3 4 5 7
30+
cur = arr.index(j)
31+
if cur!=j:
32+
cnt+=abs(cur-j)
33+
arr =swap(arr,j,cur,N)
34+
#print(arr)
35+
cntList.append(cnt)
36+
#print(arr)
37+
38+
for i in cntList:
39+
print(i)
40+

‎syheo/codingTest1/Baekjoon/4963.py‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#solved.ac
2+
#실버2
3+
#BFS
4+
#섬의 개수
5+
#4963
6+
7+
#인접 행렬 방식으로 풀이
8+
9+
from collections import deque
10+
import sys
11+
input = sys.stdin.readline
12+
cntList = []
13+
while True:
14+
w,h = map(int,input().split()) #가로 세로
15+
if w ==0 and h==0:
16+
break
17+
#맵 생성
18+
maps=[[0]*(w+2)]
19+
for i in range(h):
20+
tmp=[0]+list(map(int,input().split()))+[0]
21+
maps.append(tmp)
22+
maps.append([0]*(w+2))
23+
#BFS
24+
visited = [[False]*(w+2) for _ in range(h+2)]
25+
q = deque([])
26+
cnt = 0
27+
for i in range(1,h+1):
28+
for j in range(1,w+1):
29+
if not visited[i][j] and maps[i][j]==1:
30+
q.append((i,j))
31+
visited[i][j]=True
32+
while q:
33+
v = q.popleft()
34+
for x in range(-1,2,1):
35+
for y in range(-1,2,1):
36+
vr = v[0]+x
37+
vc = v[1]+y
38+
if not visited[vr][vc] and maps[vr][vc]==1:
39+
q.append((vr,vc))
40+
visited[vr][vc]=True
41+
cnt+=1
42+
cntList.append(cnt)
43+
#출력
44+
for cnt in cntList:
45+
print(cnt)
46+
47+
48+
49+

0 commit comments

Comments
 (0)