diff --git a/DFS BFS/5-10.py b/DFS BFS/5-10.py new file mode 100644 index 0000000..74179bf --- /dev/null +++ b/DFS BFS/5-10.py @@ -0,0 +1,34 @@ +N,M = map(int,input().split()) + +lst = [] +for i in range(N): + lst.append(list(map(int,input()))) + +cnt = 0 + +#상하좌우 +dx = [-1,1,0,0] +dy = [0,0,-1,1] + +def dfs(x,y): + #들른 곳 좌표 + lst[x][y] = 1 + + #상하좌우 이동 + for i in range(4): + lx = x + dx[i] + ly = y + dy[i] + + #내부에 있을 때 + if(lx >= 0 and lx < N and ly >= 0 and ly < M): + if (lst[lx][ly] == 0): + dfs(lx,ly) + + +for i in range(N): + for j in range(M): + if (lst[i][j] == 0): + dfs(i,j) + cnt += 1 + +print(cnt) \ No newline at end of file diff --git a/DFS BFS/5-11.py b/DFS BFS/5-11.py new file mode 100644 index 0000000..e65d65e --- /dev/null +++ b/DFS BFS/5-11.py @@ -0,0 +1,36 @@ +from collections import deque + +N,M = map(int,input().split()) + +lst = [] +for i in range(N): + lst.append(list(map(int,input()))) + +#상하좌우 +dx = [-1,1,0,0] +dy = [0,0,-1,1] + +def bfs(x,y): + queue = deque() + queue.append((x,y)) + + #큐가 빌 때까지 + while queue: + x,y = queue.popleft() + + for i in range(4): + lx = x + dx[i] + ly = y + dy[i] + + #내부 제한 + if(lx < 0 or ly < 0 or lx >= N or ly >= M): + continue + if(lst[lx][ly] == 0): + continue + #첫 방문시 기록 + if(lst[lx][ly] == 1): + lst[lx][ly] = lst[x][y] + 1 + queue.append((lx,ly)) + return lst[N-1][M-1] + +print(bfs(0,0)) \ No newline at end of file diff --git a/Implementation/4-1.py b/Implementation/4-1.py new file mode 100644 index 0000000..4cb403a --- /dev/null +++ b/Implementation/4-1.py @@ -0,0 +1,29 @@ +n = int(input()) + +###입력키 리스트 받기 +key = list(map(str,input().split())) + +x = 1 +y = 1 + +for i in key: + ###사각형 크기보다 작을 때 수행 + if(x < n and y < n): + if(i == "R"): + x += 1 + elif(i == "L"): + x -= 1 + elif(i == "U"): + y -= 1 + elif(i == "D"): + y += 1 + ###사각형 크기보다 크면 넘어가기 + else: + continue + ###좌표가 0일 때 처리 + if (x == 0): + x += 1 + elif (y == 0): + y += 1 + +print(y, x) \ No newline at end of file diff --git a/Implementation/4-2.py b/Implementation/4-2.py new file mode 100644 index 0000000..b40f17b --- /dev/null +++ b/Implementation/4-2.py @@ -0,0 +1,17 @@ +h = int(input()) + +cnt = 0 + +###h시간, 60분, 60초 동안 반복 +for i in range(h+1): + for j in range(60): + for k in range(60): + ###개수를 세기 위한 연산 + if((i//10 == 3 or i%10 == 3)): + cnt += 1 + elif((j//10 == 3 or j%10 == 3)): + cnt += 1 + elif((k//10 == 3 or k%10 == 3)): + cnt += 1 + +print(cnt) \ No newline at end of file diff --git a/Implementation/4-3.py b/Implementation/4-3.py new file mode 100644 index 0000000..04f72d7 --- /dev/null +++ b/Implementation/4-3.py @@ -0,0 +1,22 @@ +###체스판 좌표 +row = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'] +col = [1, 2, 3, 4 ,5, 6, 7, 8] + +data = list(input()) + +###위치를 좌표화 +loc = [row.index(data[0]), col.index(int(data[1]))] + +###나이트가 이동하는 경우의 수 +mov = [[-2,-1], [-2,1], [2,1], [2,-1], [1,-2], [1,2], [-1,2], [-1,-2]] + +###더한 후 0보다 크면 카운트 +cnt = 0 +for x,y in mov: + x += loc[0] + y += loc[1] + + if (x > 0 and y > 0): + cnt += 1 + +print(cnt) \ No newline at end of file diff --git a/Implementation/4-4.py b/Implementation/4-4.py new file mode 100644 index 0000000..5cf7f55 --- /dev/null +++ b/Implementation/4-4.py @@ -0,0 +1,55 @@ +N, M = map(int,input().split()) +x, y, d = map(int,input().split()) + +###맵 입력 +maps = [] +for i in range(N): + maps.append(list(map(int,input().split()))) + +###방향에 따른 이동 +dx = [-1,0,1,0] +dy = [0,1,0,-1] + +###처음 위치 표시 +maps[x][y] = 1 +cnt = 1 +dir = 0 + +while (1): + ###서쪽 바라보기 // 반복 + d -= 1 + if (d < 0): + d = 3 + + ###이동 후 위치 + lx = x + dx[d] + ly = y + dy[d] + + ###가보지 않은 곳 + if (maps[lx][ly] == 0): + maps[lx][ly] = 1 + + x = lx + y = ly + + cnt += 1 + dir = 0 + + continue + else: + dir += 1 + + ###4방향 모두 가본 칸 or 바다 + if(dir == 4): + lx = x - dx[d] + ly = y - dy[d] + + if(maps[lx][ly] == 0): + x = lx + y = ly + + else: + break + + +print(cnt) \ No newline at end of file diff --git a/Sort/6-10.py b/Sort/6-10.py new file mode 100644 index 0000000..c1e4379 --- /dev/null +++ b/Sort/6-10.py @@ -0,0 +1,10 @@ +N = int(input()) + +lst = [] +for i in range(N): + lst.append(int(input())) + +lst.sort(reverse = True) + +lst = list(map(str,lst)) +print(' '.join(lst)) \ No newline at end of file diff --git a/Sort/6-11.py b/Sort/6-11.py new file mode 100644 index 0000000..e2fe834 --- /dev/null +++ b/Sort/6-11.py @@ -0,0 +1,16 @@ +N = int(input()) + + +#리스트 입력 +lst = [] + +for i in range(N): + data = input().split() + lst.append([data[0],int(data[1])]) +print(lst) + +lst.sort(key = lambda x : x[1]) + +for i in lst: + print(i[0],end=' ') + diff --git a/Sort/6-12.py b/Sort/6-12.py new file mode 100644 index 0000000..aa7b178 --- /dev/null +++ b/Sort/6-12.py @@ -0,0 +1,17 @@ +N, K = map(int,input().split()) +A = list(map(int,input().split())) +B = list(map(int,input().split())) + +#리스트 정렬 +A.sort() +B.sort(reverse = True) + +#비교 후 바꾸기 +for i in range(K): + if(A[i] < B[i]): + A[i] = B[i] + + else: + break + +print(sum(A)) \ No newline at end of file