diff --git a/presentation.md b/presentation.md new file mode 100644 index 0000000..bca6f4c --- /dev/null +++ b/presentation.md @@ -0,0 +1,3 @@ +## 발표 자료 + +https://cheezeasy.notion.site/fc948f29a86e4eee9e24c0573c6cf451 diff --git a/week6/chaewon/week6_10026.py b/week6/chaewon/week6_10026.py new file mode 100644 index 0000000..e1fba58 --- /dev/null +++ b/week6/chaewon/week6_10026.py @@ -0,0 +1,66 @@ +# BJ 10026 / GOLD V / 96ms + +import sys +from collections import deque + +sys.setrecursionlimit(10000) # 런타임 에러 방지 : 기본 재귀 깊이 제한 = 1000으로 매우 얕음, 10000으로 변경함으로써 런타임 오류 해결, 재귀함수 사용할 때는 꼭 적어두는 게 좋음 + +n = int(input()) + +graph = [[0] * n for _ in range(n)] +visited = [[0] * n for _ in range(n)] + +normal_cnt = 0 +rb_cnt = 0 + +dx = [0, 0, 1, -1] +dy = [1, -1, 0, 0] + +for i in range(n): + graph[i] = list(sys.stdin.readline().strip()) + + +def dfs(x, y): + visited[x][y] = 1 + current_color = graph[x][y] + + for k in range(4): + nx = x + dx[k] + ny = y + dy[k] + if (0 <= nx < n) and (0 <= ny < n): + if visited[nx][ny] == 0 and current_color == graph[nx][ny]: + dfs(nx, ny) + return 0 + + +# 정상인이 볼 때 +for i in range(n): + for j in range(n): + if visited[i][j] == 0: + dfs(i, j) + normal_cnt += 1 + + +# 적록색약인 사람이 볼 때 -> graph에서 G를 R로 바꾸어 줌 +for row in range(n): + for column in range(n): + if graph[row][column] == 'G': + graph[row][column] = 'R' + +# visited 초기화 +visited = [[0]*n for _ in range(n)] + +for i in range(n): + for j in range(n): + if visited[i][j] == 0: + dfs(i, j) + rb_cnt += 1 + +print(normal_cnt, rb_cnt) + + +''' +NOTE: +계속 비슷한 문제인 것 같은데 조금만 달라져도 풀기 어려운 주제인 것 같다 흑 + +''' \ No newline at end of file diff --git a/week6/chaewon/week6_1012.py b/week6/chaewon/week6_1012.py new file mode 100644 index 0000000..8eac200 --- /dev/null +++ b/week6/chaewon/week6_1012.py @@ -0,0 +1,68 @@ +# BJ 1012 / SILVER II / 72ms + +import sys +from collections import deque + +dx = [0, 0, 1, -1] +dy = [1, -1, 0, 0] + +t = int(sys.stdin.readline().strip()) + + +def bfs(graph, a, b): + queue = deque() + queue.append((a, b)) + graph[a][b] = 0 + + while queue: + x, y = queue.popleft() + for i in range(4): + nx = x + dx[i] # 행 + ny = y + dy[i] # 열 + if nx < 0 or nx >= n or ny < 0 or ny >= m: + continue + if graph[nx][ny] == 1: + graph[nx][ny] = 0 + queue.append((nx, ny)) + + return 0 + + +for _ in range(t): + cnt = 0 + n, m, k = map(int, sys.stdin.readline().strip().split(' ')) + graph = [[0] * m for _ in range(n)] + + for _ in range(k): + x, y = map(int, sys.stdin.readline().strip().split(' ')) + graph[x][y] = 1 + + # 2차원 리스트를 보기 좋게 출력하는 코드 + # for i in graph: + # for j in i: + # print(j, end=" ") + # print() + + for a in range(n): + for b in range(m): + if graph[a][b] == 1: + bfs(graph, a, b) + cnt += 1 + + print(cnt) + + +''' +NOTE: + +핵심 아이디어가 인접한 1을 0으로 모두 바꾸어주는 건데 +아예 생각지도 못했다 ... + +즉, graph[x][y] = 1일 때, 해당 위치에서 인접한 1을 모두 0으로 바꾸어 주는 bfs를 실행한다. +이때 bfs는, x와 y에서 상하좌우로 인접한 1 뿐만 아니라, x와 y에서 상하좌우로 이동한 좌표 nx, ny에서 인접한 1 까지 0으로 변경해준다. +이 과정을 인접한 1이 없을 때까지 반복한다. + +이 아이디어를 떠올리는 것이 핵심이었는데 +문제를 많이 안풀어봐서 그런가 생각을 못해냈다 ㅠ + +''' \ No newline at end of file diff --git a/week6/chaewon/week6_1260.py b/week6/chaewon/week6_1260.py new file mode 100644 index 0000000..7c8afdc --- /dev/null +++ b/week6/chaewon/week6_1260.py @@ -0,0 +1,65 @@ +# BJ 1260 / SILVER II / 164ms + +import sys +from collections import deque + +n, m, v = map(int, input().split(' ')) + + +# 인접 행렬 생성 : 5 by 5 이차원 리스트 생성 +graph = [[0] * (n + 1) for _ in range(n + 1)] # n + 1인 이유 : n의 범위가 1부터이기 때문에, 노드값이 0인 노드가 존재X = 인덱스가 0인 graph값을 0으로 남겨두어야 함 + +for _ in range(m): + a, b = map(int, sys.stdin.readline().strip().split(' ')) + graph[a][b] = 1 # 두 노드를 연결하는 엣지는 양방향 + graph[b][a] = 1 + +dfs_visited = [0] * (n + 1) +bfs_visited = [0] * (n + 1) + + +def dfs(v): + dfs_visited[v] = 1 # 시작 노드 v를 방문 -> visited 값을 1로 변경 + print(v, end=' ') + + for i in range(1, n + 1): + if (not dfs_visited[i]) and graph[v][i]: + dfs(i) + + +def bfs(v): + queue = deque([v]) + bfs_visited[v] = 1 + + while queue: + v = queue.popleft() + print(v, end=' ') + for j in range(1, n + 1): + if (not bfs_visited[j]) and graph[v][j]: + queue.append(j) + bfs_visited[j] = 1 + + +dfs(v) +print() +bfs(v) + + +''' +NOTE: +DFS, BFS의 가장 기본 문제인 것 같다 +기본 개념과 예제 코드까지 다 익히고 나서 문제를 푼건데도 어려웠다 +예제에서는 graph를 처음부터 인접 리스트로 저장해뒀었는데 +여기서는 입력 받고 인접 행렬 또는 인접 리스트로 직접 바꿔 저장하는 부분을 구현하는 게 어려웠다 ㅠ + +인접 리스트를 구현하는 코드는 아래와 같다 + +graph = [[] for _ in range(N + 1)] + +for _ in range(M): + a, b = map(int, input().split()) + graph[a].append(b) + graph[b].append(a) + +파이팅... +''' \ No newline at end of file diff --git a/week6/chaewon/week6_2178.py b/week6/chaewon/week6_2178.py new file mode 100644 index 0000000..d8a0c33 --- /dev/null +++ b/week6/chaewon/week6_2178.py @@ -0,0 +1,53 @@ +# BJ 2178 / SILVER I / 76ms + +import sys +from collections import deque + +n, m = map(int, sys.stdin.readline().strip().split(' ')) + +graph = [[0] for _ in range(n)] +visited = [[0] * m for _ in range(n)] + +for i in range(n): + graph[i] = list(map(int, sys.stdin.readline().strip())) + +dx = [0, 0, 1, -1] +dy = [1, -1, 0, 0] + + +def bfs(): + cnt = 1 + queue = deque() + queue.append((0, 0, cnt)) + visited[0][0] = 1 + + while queue: + x, y, cnt = queue.popleft() + if x == n-1 and y == m-1: + break + for i in range(4): + nx = x + dx[i] # 행 + ny = y + dy[i] # 열 + if nx < 0 or nx >= n or ny < 0 or ny >= m: + continue + if graph[nx][ny] == 1 and visited[nx][ny] == 0: + visited[nx][ny] = 1 + queue.append((nx, ny, cnt + 1)) + print(cnt) + return 0 + + +bfs() + + +''' +NOTE: + +배추 문제랑 비슷한 것 같으면서도 최단 거리라는 조건이 하나 더 붙어서 뭔가 헷갈렸던 문제 + +처음에는 메모리 초과가 났는데, 32번라인에서 visited[nx][ny] == 0 조건을 추가했더니 해결됐다 +방문 여부를 queue에 추가하기 전에 체크하지 않아서 그랬던 것 같다 + +또 처음 cnt 선언을 0으로 해서 헤매기도 했다...^^ 정말 어렵고도 어려운 알고리즘 +2학기에 수업 꼭 듣고 만다 +''' \ No newline at end of file diff --git a/week6/chaewon/week6_2589.py b/week6/chaewon/week6_2589.py new file mode 100644 index 0000000..7e5aa90 --- /dev/null +++ b/week6/chaewon/week6_2589.py @@ -0,0 +1,70 @@ +# BJ 2589 / GOLD V / 752ms + +import sys +from collections import deque + +sys.setrecursionlimit(10000) + +n, m = map(int, input().split(' ')) + +dx = [0, 0, 1, -1] +dy = [1, -1, 0, 0] + +graph = [list(sys.stdin.readline().strip()) for _ in range(n)] +visited = [[0] * m for _ in range(n)] + + +def bfs(x, y): + queue = deque() + cnt = 0 + queue.append((x, y)) + visited[x][y] = 1 # 놓친 부분 + + while queue: + x, y = queue.popleft() + for i in range(4): + nx = x + dx[i] + ny = y + dy[i] + + if nx < 0 or nx >= n or ny < 0 or ny >= m: + continue + elif graph[nx][ny] == 'L' and visited[nx][ny] == 0: + visited[nx][ny] = visited[x][y] + 1 + cnt = max(cnt, visited[nx][ny]) # 현재 위치에서 가장 먼 육지까지의 거리+1 값을 구함 + queue.append((nx, ny)) + return cnt - 1 + + +result = 0 + +for i in range(n): + for j in range(m): + if i > 0 and i + 1 < n: + if graph[i - 1][j] == "L" and graph[i + 1][j] == "L": + continue + if j > 0 and j + 1 < m: + if graph[i][j - 1] == "L" and graph[i][j + 1] == "L": + continue + + if graph[i][j] == 'L': + visited = [[0] * m for _ in range(n)] + result = max(result, bfs(i, j)) + +print(result) + + +''' +NOTE: +대부분의 로직은 구현했는데, cnt 구하는 과정에서 막혔다ㅠ + +32번 라인 : visited[nx][ny] = visited[x][y] + 1 를 통해, 현재 위치에서 각 육지위치까지의 거리를 구할 수 있게 된다는 것, +51번 라인 : 육지좌표 간 가장 거리가 먼 값을 구하는 과정인데, 여기서는 바로바로 result와 bfs()를 비교하는 데 max()를 써도 되지만 + list를 만들고 bfs()값을 append한 뒤 max(list)해도 된다. + +42~47번 라인 : 백트래킹 코드. 육지이지만 가장자리가 아닌 부분을 제외하는 과정이다. + 육지이지만 가장자리가 아닌 부분을 지나면, 가장 먼 최단거리에 해당할 수 없기 때문에 애초부터 배제해주는 것 + 이 코드가 없으면 pypy3로 제출해야 시간초과가 안난다 + 이 부분이 있어야 python3로 제출 가능하다 + +골드 v라 그런가 확실히 어렵다!!! 그래도 점점 나아지는 중이당 +''' \ No newline at end of file diff --git a/week7/bada/backjoon_form.py b/week7/bada/backjoon_form.py new file mode 100644 index 0000000..beddb97 --- /dev/null +++ b/week7/bada/backjoon_form.py @@ -0,0 +1,18 @@ +''' +/* 문제 정보 */ +??번 - ?? +난이도 - ?? + +/* 풀이 방법 */ + + +''' + + + +''' +/* 오답노트 */ + +/* 느낀점 */ + +''' \ No newline at end of file diff --git a/week7/chaewon/week7_14889.py b/week7/chaewon/week7_14889.py new file mode 100644 index 0000000..4d615d7 --- /dev/null +++ b/week7/chaewon/week7_14889.py @@ -0,0 +1,49 @@ +# BJ 14889 : 스타트와 링크 / SILVER II / + +import sys + +n = int(sys.stdin.readline().strip()) +graph = [list(map(int, sys.stdin.readline().strip().split(' '))) for _ in range(n)] +visited = [0 for _ in range(n)] +min_diff = int(1e9) + + +def dfs(depth, idx): + global min_diff + if depth == n // 2: + power1, power2 = 0, 0 + for i in range(n): + for j in range(n): + if visited[i] and visited[j]: + power1 += graph[i][j] + elif not visited[i] and not visited[j]: + power2 += graph[i][j] + min_diff = min(min_diff, abs(power1 - power2)) + return 0 + + for i in range(idx, n): + if not visited[i]: + visited[i] = 1 + dfs(depth + 1, i + 1) + visited[i] = 0 + + +dfs(0, 0) +print(min_diff) + + +''' +NOTE: +(1~n)번까지의 팀원을 두 팀으로 나누어, 팀별로 팀원 간의 모든 능력치를 더해 빼는 방향 + +그래서 처음에 스타트 팀/링크 팀을 구분했는데, +스타트 팀을 기준으로 잡고 (1~n)번에서 n / 2 개를 랜덤 추출하고, +나머지 수를 링크 팀으로 넣어주었다 +그 후 두 팀간의 능력치합 차이를 구하는 것을 진행했었다. + +여기서 문제는, 두 개의 팀으로 나눌 때 스타트 팀을 랜덤 추출하는 것이 아니라, +팀이 나뉠 수 있는 모든 경우의 수를 고려해야 하는 것이었다. + +그래서 15650번을 참고해서, n에서 n/2 개의, 중복되지 않는 배열을 함수를 사용해서 뽑아내려고 했는데 +그 부분이 막혀서 결국 찾아봤다ㅠ +''' \ No newline at end of file diff --git a/week7/chaewon/week7_15649.py b/week7/chaewon/week7_15649.py new file mode 100644 index 0000000..a061f31 --- /dev/null +++ b/week7/chaewon/week7_15649.py @@ -0,0 +1,32 @@ +# BJ 15649 : N과 M (1) / SILVER III / 168ms + +import sys +sys.setrecursionlimit(10000) + +n, m = map(int, sys.stdin.readline().strip().split(' ')) + +ans = [] + + +def back(): + if len(ans) == m: + print(" ".join(map(str, ans))) + return 0 + for i in range(1, n+1): + if i not in ans: + ans.append(i) + back() + ans.pop() + + +back() + +''' +NOTE: +백트래킹과 depth-first search의 차이를 파악하기가 좀 어렵다 + +이 문제에서는 재귀함수를 이해하는 게 조금 힘들었다. +back()이 호출되면 그 안에서 for문이 다시 시행되는 형태인데 +이해가 잘 안돼서 시간이 좀 걸렸다 + +''' \ No newline at end of file diff --git a/week7/chaewon/week7_15650.py b/week7/chaewon/week7_15650.py new file mode 100644 index 0000000..fc25d4c --- /dev/null +++ b/week7/chaewon/week7_15650.py @@ -0,0 +1,36 @@ +# BJ 15650 : N과 M (2) / SILVER III / 136ms + +import sys + +n, m = map(int, sys.stdin.readline().strip().split(' ')) +ans = [] + + +def back(): + if len(ans) == m: + is_sorted = (sorted(ans) == ans) + if is_sorted: # ans가 오름차순 정렬되어 있으면 print + print(' '.join(map(str, ans))) + return 0 + for i in range(1, n + 1): + if i not in ans: + ans.append(i) + back() + ans.pop() + + +back() + + +''' +NOTE: +15649번 문제와 거의 유사하다. +각 원소가 오름차순 정렬되어야 한다는 조건이 하나 추가된 것이다. + +처음에는 for문 안에서, ans에 오름차순으로 원소를 append하려고 시도했었다. +그런데 너무 복잡해져서 다시 생각해봤는데, +print할 때 조건문을 걸어서 오름차순인 것들만 print하면 될 것 같았다. + +리스트의 정렬 여부를 파악하는 법을 찾아봤고, +만약 ans가 오름차순 정렬되어 있으면 print하는 식으로 수정했다. +''' \ No newline at end of file diff --git a/week7/chaewon/week7_15652.py b/week7/chaewon/week7_15652.py new file mode 100644 index 0000000..b0f31ad --- /dev/null +++ b/week7/chaewon/week7_15652.py @@ -0,0 +1,32 @@ +# BJ 15652 : N과 M (4) / SILVER III / + +import sys +sys.setrecursionlimit(10000) + +n, m = map(int, sys.stdin.readline().strip().split(' ')) +ans = [] + +def back(x): + if len(ans) == m: + print(' '.join(map(str, ans))) + return 0 + + for i in range(x, n + 1): + ans.append(i) + back(i) + ans.pop() + +back(1) + + +''' +NOTE: +처음엔 back()에 매개변수를 안줬는데 그러니까 시간초과가 떴다 +15650번 문제 코드에 중복된 원소도 추가할 수 있게끔 코드를 짰었는데 +재귀함수 호출하면서 for문 깊이가 깊어져서 시간 초과가 뜬 거였다. + +매개 변수 설정을 해주면 재귀함수 호출할 때 i부터 체크하기 때문에 +시간 초과가 뜨지도 않고, sorted로 비내림차순임을 체크할 필요도 없어진다. + +어렵당 +''' \ No newline at end of file diff --git a/week7/chaewon/week7_2580.py b/week7/chaewon/week7_2580.py new file mode 100644 index 0000000..7fa48ea --- /dev/null +++ b/week7/chaewon/week7_2580.py @@ -0,0 +1,78 @@ +# BJ 2580 : 스도쿠 / GOLD IV + +import sys + + +def check_row(x, n): + for i in range(9): + if n == board[x][i]: + return False + return True + + +def check_col(y, n): + for i in range(9): + if n == board[i][y]: + return False + return True + + +def check_sqr(x, y, n): + nx = x // 3 * 3 + ny = y // 3 * 3 + for i in range(3): + for j in range(3): + if n == board[nx + i][ny + j]: + return False + return True + + +def sudoku(n): + if n == len(blank): + for k in range(9): + print(*board[k]) + exit(0) + + for i in range(1, 10): + x = blank[n][0] + y = blank[n][1] + + if check_row(x, i) and check_col(y, i) and check_sqr(x, y, i): + board[x][y] = i + sudoku(n + 1) + board[x][y] = 0 + + +board = [] +blank = [] + +for i in range(9): + board.append(list(map(int, sys.stdin.readline().strip().split(' ')))) + for j in range(9): + if board[i][j] == 0: + blank.append([i, j]) + +sudoku(0) + + +''' +NOTE: +처음에 행 단위로 0 여부 검사 후 1~9 넣는건 성공했는데, +3x3 정사각형 단위로 검사하는 게 너무 어려웠다ㅠ + +2차원 배열인 board를 3x3으로 슬라이싱해서 새로운 리스트로 저장했었는데, +그러니까 현재 좌표에서 이 새로운 리스트로의 접근이 어려워졌다... + +코드 찾아보니까, 3x3 행렬에서 (0, 0)좌표에 해당하는 점을 (nx, ny)로 받아오던데 +이런건 정말 많이 풀어봐야 느는 사고인 것 같다 +나도 몫이나 나머지로 접근해보려고 시도는 했었는데 방법을 못찾았다ㅠ +그리고 또 나는 (0, 0)이 아니라 (1, 1), 그러니까 3x3행렬의 중앙값을 가져오려고 했었다 + +아 어렵다!! + ++ pypy로 제출해야 시간 초과가 안뜬다ㅠ +찾아보니까 해당 문제가 특히 python으로 시간 초과 안내기가 어렵다는 듯..? 잘 모르겠다 +pypy는 실행시 자주 쓰이는 코드를 캐싱하는 기능이 있어서 +"복잡하거나 반복되는 코드"를 사용한 경우에는 pypy를 사용하는 것이 메모리와 속도 측에서 낫다 + +''' \ No newline at end of file diff --git a/week7/minkyu/code.txt b/week7/minkyu/code.txt new file mode 100644 index 0000000..90a1d60 --- /dev/null +++ b/week7/minkyu/code.txt @@ -0,0 +1 @@ +... \ No newline at end of file diff --git a/week7/sumin/code.txt b/week7/sumin/code.txt new file mode 100644 index 0000000..90a1d60 --- /dev/null +++ b/week7/sumin/code.txt @@ -0,0 +1 @@ +... \ No newline at end of file diff --git a/week7/sunghoon/code.txt b/week7/sunghoon/code.txt new file mode 100644 index 0000000..90a1d60 --- /dev/null +++ b/week7/sunghoon/code.txt @@ -0,0 +1 @@ +... \ No newline at end of file diff --git a/week7/youngjun/code.txt b/week7/youngjun/code.txt new file mode 100644 index 0000000..90a1d60 --- /dev/null +++ b/week7/youngjun/code.txt @@ -0,0 +1 @@ +... \ No newline at end of file diff --git a/week8/bada/backjoon_form.py b/week8/bada/backjoon_form.py new file mode 100644 index 0000000..beddb97 --- /dev/null +++ b/week8/bada/backjoon_form.py @@ -0,0 +1,18 @@ +''' +/* 문제 정보 */ +??번 - ?? +난이도 - ?? + +/* 풀이 방법 */ + + +''' + + + +''' +/* 오답노트 */ + +/* 느낀점 */ + +''' \ No newline at end of file diff --git a/week8/chaewon/week8_1261.py b/week8/chaewon/week8_1261.py new file mode 100644 index 0000000..d2cd4f3 --- /dev/null +++ b/week8/chaewon/week8_1261.py @@ -0,0 +1,37 @@ +# BJ 1261 : 알고스팟 / GOLD IV / 80ms + +import sys +from collections import deque + +m, n = map(int, sys.stdin.readline().strip().split(' ')) + +miro = [] +visited = [[0]*m for _ in range(n)] + +for _ in range(n): + miro.append(list(map(int, list(sys.stdin.readline().strip())))) + +dx = [1, -1, 0, 0] +dy = [0, 0, 1, -1] + +queue = deque() +queue.append((0, 0)) +visited[0][0] = 1 + +while queue: + x, y = queue.popleft() + + for i in range(4): + nx = x + dx[i] + ny = y + dy[i] + + if 0 <= nx < n and 0 <= ny < m: + if visited[nx][ny] == 0: + if miro[nx][ny] == 1: + visited[nx][ny] = visited[x][y] + 1 + queue.append((nx, ny)) + else: + visited[nx][ny] = visited[x][y] + queue.appendleft((nx, ny)) + +print(visited[n-1][m-1] - 1) diff --git a/week6/chaewon/code.txt b/week8/chaewon/week8_12834.py similarity index 100% rename from week6/chaewon/code.txt rename to week8/chaewon/week8_12834.py diff --git a/week8/chaewon/week8_1584.py b/week8/chaewon/week8_1584.py new file mode 100644 index 0000000..e69de29 diff --git a/week8/chaewon/week8_20007.py b/week8/chaewon/week8_20007.py new file mode 100644 index 0000000..e69de29 diff --git a/week8/chaewon/week8_5944.py b/week8/chaewon/week8_5944.py new file mode 100644 index 0000000..e69de29 diff --git a/week8/minkyu/code.txt b/week8/minkyu/code.txt new file mode 100644 index 0000000..90a1d60 --- /dev/null +++ b/week8/minkyu/code.txt @@ -0,0 +1 @@ +... \ No newline at end of file diff --git a/week8/sumin/code.txt b/week8/sumin/code.txt new file mode 100644 index 0000000..90a1d60 --- /dev/null +++ b/week8/sumin/code.txt @@ -0,0 +1 @@ +... \ No newline at end of file diff --git a/week8/sunghoon/code.txt b/week8/sunghoon/code.txt new file mode 100644 index 0000000..90a1d60 --- /dev/null +++ b/week8/sunghoon/code.txt @@ -0,0 +1 @@ +... \ No newline at end of file diff --git a/week8/youngjun/code.txt b/week8/youngjun/code.txt new file mode 100644 index 0000000..90a1d60 --- /dev/null +++ b/week8/youngjun/code.txt @@ -0,0 +1 @@ +... \ No newline at end of file