-
Notifications
You must be signed in to change notification settings - Fork 0
주사위 굴리기 / 경사로 #148
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
Open
sangYuLv
wants to merge
2
commits into
main
Choose a base branch
from
liv
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+123
−0
Open
주사위 굴리기 / 경사로 #148
Changes from all commits
Commits
Show all changes
2 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,71 @@ | ||
| // 백준 - 14499 주사위 굴리기 | ||
|
|
||
| let line = readLine()!.split(separator: " ").compactMap { Int($0) } | ||
| let n = line[0]; let m = line[1]; let x = line[2]; let y = line[3] | ||
|
|
||
| var map = [[Int]]() | ||
| for _ in 1...n { map.append(readLine()!.split(separator: " ").compactMap { Int($0) }) } | ||
|
|
||
| let orders = readLine()!.split(separator: " ").compactMap { Int($0) } | ||
| let directions = [(0, 1), (0, -1), (-1, 0), (1,0)] | ||
|
|
||
| var dice = [Int](repeating: 0, count: 6) | ||
| var coord: (r: Int, c: Int) = (x, y) | ||
| var result = [String]() | ||
|
|
||
| for order in orders { | ||
| // 지도 다음 칸 찾기 | ||
| let direction = directions[order-1] | ||
| let next: (r: Int, c: Int) = (coord.r+direction.0, coord.c+direction.1) | ||
|
|
||
| // 다음 칸의 숫자 확인 | ||
| guard next.r >= 0 && next.r < n && next.c >= 0 && next.c < m else { continue } | ||
| coord = next | ||
|
|
||
| // 주사위 굴리기 | ||
| roll(order) | ||
|
|
||
| if map[coord.r][coord.c] == 0 { | ||
| map[coord.r][coord.c] = dice[5] | ||
| } else { | ||
| dice[5] = map[coord.r][coord.c] | ||
| map[coord.r][coord.c] = 0 | ||
| } | ||
|
|
||
| result.append(String(dice[0])) | ||
| } | ||
|
|
||
| print(result.joined(separator: "\n")) | ||
|
|
||
| func roll(_ order: Int) { | ||
| let temp = dice | ||
|
|
||
| switch order { | ||
| case 1: | ||
| dice[0] = temp[3] | ||
| dice[2] = temp[0] | ||
| dice[5] = temp[2] | ||
| dice[3] = temp[5] | ||
|
|
||
| case 2: | ||
| dice[0] = temp[2] | ||
| dice[3] = temp[0] | ||
| dice[5] = temp[3] | ||
| dice[2] = temp[5] | ||
|
|
||
| case 3: | ||
| dice[0] = temp[4] | ||
| dice[1] = temp[0] | ||
| dice[5] = temp[1] | ||
| dice[4] = temp[5] | ||
|
|
||
| case 4: | ||
| dice[0] = temp[1] | ||
| dice[4] = temp[0] | ||
| dice[5] = temp[4] | ||
| dice[1] = temp[5] | ||
|
|
||
| default: | ||
| break | ||
| } | ||
| } | ||
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 @@ | ||
| // 백준 - 14890 경사로 | ||
|
|
||
| let nl = readLine()!.split(separator: " ").compactMap { Int($0) } | ||
| let n = nl[0]; let l = nl[1] | ||
|
Contributor
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. |
||
| var map = [[Int]]() | ||
| for _ in 0..<n { | ||
| map.append(readLine()!.split(separator: " ").compactMap { Int($0) }) | ||
| } | ||
|
|
||
| var count = 0 | ||
| for i in 0..<n { | ||
| if check(map[i]) { count += 1 } | ||
|
|
||
| var column = [Int]() | ||
| for row in map { column.append(row[i]) } | ||
|
Comment on lines
+14
to
+15
Contributor
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. |
||
| if check(column) { count += 1 } | ||
| } | ||
|
|
||
| print(count) | ||
|
|
||
| func check(_ line: [Int]) -> Bool { | ||
| var used = [Bool](repeating: false, count: n) | ||
|
|
||
| for i in 0..<n-1 { | ||
| let diff = line[i+1] - line[i] | ||
|
|
||
| if diff == 0 { continue } | ||
|
|
||
| else if diff == 1 { | ||
| let start = i - l + 1 | ||
| if start < 0 { return false } | ||
| for j in start...i { | ||
| if used[j] { return false } | ||
| if line[j] != line[i] { return false } | ||
| used[j] = true | ||
| } | ||
| } | ||
|
|
||
| else if diff == -1 { | ||
| let end = i + l | ||
| if end >= n { return false } | ||
| for j in i+1...end { | ||
| if used[j] { return false } | ||
| if line[j] != line[i+1] { return false } | ||
| used[j] = true | ||
| } | ||
| } | ||
|
|
||
| else { return false } | ||
| } | ||
| return true | ||
| } | ||
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.
한 줄에 세미콜론(
;)을 사용하여 여러 변수를 선언하는 것은 가독성을 떨어뜨립니다. Swift 스타일 가이드에 따라 각 선언을 별도의 줄로 분리하는 것이 좋습니다.