-
Notifications
You must be signed in to change notification settings - Fork 5
YoonYn9915 / 7월 5주차 / 3문제 #240
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| ''' | ||
| 요구사항 | ||
| - k (1~10)점을 어피치가 a발을 맞췄고, 라이언이 b발을 맞혔을 때, b> a이면 k점을 라이언이 가져가고, 아니면 어피치가 k점을 가져간다. 단 a = b = 0일 경우 누구도 k점을 가져가지 못한다. | ||
| - 모든 과녁에 대하여 두 선수의 총점 계산, 단 최종 점수가 같으면 어피치를 우승자로 | ||
| - 어피치가 n발을 쏜 후 그 정보를 가지고 라이언이 어피치를 가장 큰 점수차로 이기기 위해 n발을 어떻게 맞춰야 하는지 구해라. | ||
| - 라이언이 우승할 수 없는 경우 -1을 출력하고, 라이언이 가장 큰 점수차로 이길 수 있는 방법이 여러개인 경우 가장 낮은 점수를 더 많이 맞힌 경우를 반환해야 한다. | ||
|
|
||
|
|
||
| 아이디어 | ||
| - 이 문제는 greedy는 아닌거 같다. greedy로 하려면 큰 점수를 무조건 먹기 위해 어피치가 쏜 화살 + 1만큼 할 텐데 입출력 예 1번처럼 라이언이 10점을 먹어도 9점 8점을 어피치가 먹으면 라이언이 지게 된다. | ||
| - 따라서 완전 탐색을 진행해야 하며, dfs와 backtracking을 사용하여 깊이 우선으로 모든 경우의 수 계산. | ||
| - 양궁대회 시뮬레이션 아이디어: info(어피치의 결과 배열)가 10점부터 0점까지의 나열이므로 10점부터 0점까지 내림차순으로 화살을 쏜다. 모든 경우를 구할 수 없으므로 현재 점수에 대해 라이언이 0발 혹은 info[i] + 1발을 맞춘 경우만 고려하고 나머지 경우는 진행하지 않는다. 즉 어피치가 10점에 2발을 맞췄다면, 라이언은 0발을 맞추고 화살을 아껴 다음 점수로 가던지, 2+1발을 맞춰 화살을 쓰더라도 점수를 얻고 가던지 두가지 경우만 해야 한다. | ||
|
|
||
|
|
||
| 구현 | ||
| 1. dfs 진행 | ||
| - dfs 종료 조건: 현재 점수판이 0점이거나, 남은 화살이 없을때 | ||
| - dfs 진행: (점수 그대로, 화살개수 그대로) 혹은 (현재 점수 먹기, 화살개수 소모하기) 두가지 버전으로 나뉨 | ||
| 2. 라이언과 어피치 중 누가 얼마의 차이로 이겼는지 확인하는 함수 | ||
| - 차이가 같다면 가장 낮은 점수를 더 많이 맞춘 배열 선택 | ||
|
|
||
| ''' | ||
|
|
||
| # 점수차가 같은 경우 가장 낮은 점수를 많이 쏜 것을 정답으로 | ||
| def choose_answer(arr1, arr2): | ||
| length = len(arr1) | ||
|
|
||
| # 낮은 점수부터 체크해야 하니까 역순으로 | ||
| for i in range(length-1, -1, -1): | ||
| if arr1[i] > arr2[i]: | ||
| return arr1 | ||
| elif arr2[i] > arr1[i]: | ||
| return arr2 | ||
|
|
||
| # 반복문이 끝났는데 여기까지 온 경우는 두 배열이 같다는 뜻이므로 아무거나 반환 | ||
| return arr1 | ||
|
|
||
|
|
||
|
|
||
| # 승자와 점수차를 반환. | ||
| def get_winner(lion, appeach): | ||
| lion_score = 0 | ||
| appeach_score = 0 | ||
| length = len(appeach) | ||
| for i in range(length): | ||
| if lion[i] > 0 or appeach[i] > 0: | ||
| # 라이언이 맞춘 화살이 어피치보다 많으면 라이언이 점수를 가져오고 | ||
| if lion[i] > appeach[i]: | ||
| lion_score += (10 -i) | ||
| # 그렇지 않으면 어피치가 점수를 가져온다. | ||
| else: | ||
| appeach_score += (10 - i) | ||
|
|
||
| # 라이언 승 | ||
| if lion_score > appeach_score: | ||
| return 1, lion_score - appeach_score | ||
| else: | ||
| return -1, appeach_score - lion_score | ||
|
|
||
| def dfs(n, idx, lion, appeach): | ||
| global max_result, answer | ||
|
|
||
| # dfs 종료 조건. 현재 점수판이 0점이거나, 남은 화살이 없을 때 | ||
| if n == 0 or idx == 10: | ||
| # 점수판이 0점이면 라이언의 남은 화살 모두 0점에 맞춤 | ||
| if idx == 10: | ||
| lion[idx] = n | ||
|
|
||
| # 둘 중 누가 승자인지 판단하고, 라이언이 승자면 최대점수인지 확인 | ||
| winner, score_gap = get_winner(lion, appeach) | ||
|
|
||
| # 라이언이 승자이면 | ||
| if winner == 1: | ||
| if max_result < score_gap: | ||
| max_result = score_gap | ||
| answer = lion | ||
| elif max_result == score_gap: | ||
| answer = choose_answer(answer, lion) | ||
| return | ||
|
|
||
| # 현재 점수를 포기하고 화살을 아껴서 다음 dfs 진행 | ||
| dfs(n, idx+1, lion[:], appeach) | ||
|
|
||
| # 어피치보다 한 발 더 쏴서 현재 점수를 먹고 다음 dsf 진행 | ||
| if n >= appeach[idx] + 1: | ||
| tmp = lion[:] | ||
| tmp[idx] = appeach[idx] + 1 | ||
| dfs(n - (appeach[idx] + 1), idx+1, tmp, appeach) | ||
|
|
||
|
|
||
|
|
||
|
|
||
| def solution(n, info): | ||
| global answer, max_result | ||
| # 라이언이 맞춘 점수 초기화 | ||
| lion = [0] * 11 | ||
| # 정답이 될 배열 | ||
| answer = [0] * 11 | ||
|
|
||
| max_result = 0 | ||
|
|
||
| # 10점부터 0점까지 점수판을 내림차순으로 순회하며 몇 발을 맞췄는지 시뮬레이션 하기 위함. | ||
| # n은 남은 화살, idx는 현재 점수판(0이면 10점, 10이면 0점) | ||
| dfs(n, 0, lion, info) | ||
|
|
||
| if max_result == 0: | ||
| print([-1]) | ||
| else: | ||
| print(answer) | ||
|
|
||
|
|
||
| solution(10, [0,0,0,0,0,0,0,0,3,4,3]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
|
|
||
| from collections import deque | ||
|
|
||
| def bfs(places, i, j, k): | ||
| queue = deque() | ||
| queue.append((j, k, 0)) # row, col, dist | ||
| visited = [[False] * 5 for _ in range(5)] | ||
| visited[j][k] = True | ||
|
|
||
| dx = [-1, 1, 0, 0] | ||
| dy = [0, 0, -1, 1] | ||
|
|
||
| while queue: | ||
| row, col, dist = queue.popleft() | ||
|
|
||
| if dist != 0 and places[i][row][col] == 'P': | ||
| return 1 | ||
|
|
||
| if dist == 2: | ||
| continue | ||
|
|
||
| for d in range(4): | ||
| nr = row + dx[d] | ||
| nc = col + dy[d] | ||
|
|
||
| if 0 <= nr < 5 and 0 <= nc < 5 and not visited[nr][nc]: | ||
| if places[i][nr][nc] != 'X': | ||
| visited[nr][nc] = True | ||
| queue.append((nr, nc, dist + 1)) | ||
|
|
||
| return 0 | ||
|
|
||
|
|
||
| def solution(places): | ||
| answer = [] | ||
|
|
||
| for i in range(5): # 5 rooms | ||
| violated = False | ||
| for j in range(5): | ||
| for k in range(5): | ||
| if places[i][j][k] == 'P': | ||
| if bfs(places, i, j, k): | ||
| violated = True | ||
| break | ||
| if violated: | ||
| break | ||
| answer.append(0 if violated else 1) | ||
|
|
||
| return answer | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| import sys | ||
|
|
||
| input = sys.stdin.readline | ||
| from collections import deque | ||
|
|
||
| N = int(input()) | ||
| graph = [list(map(int, input().split())) for _ in range(N)] | ||
| high = 0 | ||
|
|
||
| for i in range(N): | ||
| for j in range(N): | ||
| if graph[i][j] > high: | ||
| high = graph[i][j] | ||
|
|
||
| dx, dy = [0, 0, -1, 1], [-1, 1, 0, 0] | ||
| queue = deque() | ||
|
|
||
|
|
||
| def bfs(i, j, high): | ||
| queue.append((i, j)) | ||
| visited[i][j] = 1 | ||
|
|
||
| while queue: | ||
| x, y = queue.popleft() | ||
|
|
||
| for i in range(4): | ||
| nx = dx[i] + x | ||
| ny = dy[i] + y | ||
|
|
||
| if nx < 0 or nx >= N or ny < 0 or ny >= N: | ||
| continue | ||
|
|
||
| if graph[nx][ny] > high and visited[nx][ny] == 0: | ||
| visited[nx][ny] = 1 | ||
| queue.append((nx, ny)) | ||
|
|
||
|
Comment on lines
+19
to
+36
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. BFS로 풀이하셨군요! 이 문제는 DFS, BFS 모두 풀이 가능해서 어떻게 풀이하셨을지 궁금했습니다! |
||
|
|
||
| result = 0 | ||
| for k in range(high): | ||
| visited = [[0] * N for _ in range(N)] | ||
| ans = 0 | ||
|
|
||
| for i in range(N): | ||
| for j in range(N): | ||
| if graph[i][j] > k and visited[i][j] == 0: | ||
| bfs(i, j, k) | ||
| ans += 1 | ||
|
|
||
| if result < ans: | ||
| result = ans | ||
|
|
||
| print(result) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| n = int(input()) | ||
| a = list(map(int, input().split())) | ||
| b = list(map(int, input().split())) | ||
| a.sort() | ||
| s = 0 | ||
| for i in range(n): | ||
| b_max = max(b) | ||
| s += a[i] * b_max | ||
| b.remove(b_max) | ||
|
|
||
| print(s) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
하나의 함수에서 하나의 역할만 할 수 있도록 bfs 함수 내에서 bfs 탐색 역할만 하도록 코드를 짜신 점이 잘하신 점인 것 같습니다