-
Notifications
You must be signed in to change notification settings - Fork 0
4-giljihun #23
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
4-giljihun #23
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| /* MARK: - SOLUTION | ||
|
|
||
| 1. 8x8 ์ฒด์คํ ๋ง๋ค๊ธฐ | ||
| 2. ์ปจํธ๋กค๋ฌ ์ ์ | ||
| 3. ์ด๋ ๋ก์ง | ||
| 4. ๋์ ๋ฐ์น๋ ๋ก์ง | ||
|
|
||
| ํต์ฌ ๊ตฌํ์.. ์ํ๋ฒณ -> ์ขํ ๋ณํ | ||
|
|
||
| ์์คํค ์ฝ๋ ์ฐ๋ฉด๋ ๋ฏ | ||
| ๊ทผ๋ฐ ์ด์ผ ๋ณํํ์ง | ||
|
|
||
| */ | ||
| import Foundation | ||
|
|
||
| // MARK: - Input & ์ด๋ ๋ก์ง | ||
| guard let N = readLine(), | ||
| N.components(separatedBy: " ").count >= 3 else { exit(0) } | ||
| let firstPositionOfKing = N.components(separatedBy: " ")[0] | ||
| let firstPositionOfStone = N.components(separatedBy: " ")[1] | ||
|
Comment on lines
+17
to
+20
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. ๋ฌธ์์ด ์ชผ๊ฐค ๋ split๋์ components๋ฅผ ์ฐ์ ์ด์ ๊ฐ ์์๊น์? split์ด substring์ ๋ฐํํ๊ธฐ ๋๋ฌธ์ String ์์ฐ๋ ๊ฒ ๋ณด๋ค components๊ฐ ๋ซ๋ค๊ณ ์๊ฐํ์ จ๋์?? ์ด๊ฒ ํจ์จ์ด ์ข๋์?? ํน์ ๊ฐ๋ ์ฑ?? (๋ฌผ์ํ์ด์ธ๋ง)
Member
Author
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. ๋ถํ์ํ String ๋ณํ์ด ๋ง๋ค๋ฉด, split์ด ๋์ ์๋ ์์ง๋ง |
||
|
|
||
| // let movingCnt = Int(N.components(separatedBy: " ")[2]) | ||
| guard let movingCnt = Int(N.components(separatedBy: " ")[2]) else { exit(0) } | ||
|
Comment on lines
+17
to
+23
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. ์ ๋ ฅ ๋ํ ์ผ ... He knows the way !!! ์บฌ.. |
||
|
|
||
| var chessBoard = Array(repeating: Array(repeating: ".", count: 8), count: 8) | ||
|
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. ์ค 8*8 ํ๋ ฌ์ ๋ง๋์ จ๊ตฐ์ ! ์ด๋ฒ์ ์ ๋ ์ ๋ง๋ค๊ณ ์์ฒด์ ์ผ๋ก ๋ฒ์ด๋๋์ง ์ ๋ฒ์ด๋๋์ง๋ง ํ์ธํ์์ด์ ! |
||
|
|
||
| let direction: [String: (dx: Int, dy: Int)] = [ | ||
| "R": (1, 0), "L": (-1, 0), | ||
| "B": (0, 1), "T": (0, -1), | ||
| "RT": (1, -1), "LT": (-1, -1), | ||
| "RB": (1, 1), "LB": (-1, 1) | ||
| ] | ||
|
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. ์ค ์ด๊ฒ๋ ๋๋ ๊ฐ๋ค
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. ์ ๋ enum Command: String {
case R, L, B, T, LB, RB, LT, RT
func getDirection() -> (row: Int, col: Int) {
switch self {
case .R: return (0, 1)
case .L: return (0, -1)
case .B: return (-1, 0)
case .T: return (1, 0)
case .LB: return (-1, -1)
case .RB: return (-1, 1)
case .LT: return (1, -1)
case .RT: return (1, 1)
}
}
}์๋์ ์ผ๋ก ์ผ์ด์ค๊ฐ ๋๋๋ ๊ฒ์ ๋ํด์๋
Member
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. ์คํธ ์ ๋ ๋์ ๋๋ฆฌ ์ฌ์ฉํ๋๋ฐ enum์ผ๋ก ํ๋ฉด ํด๋จผ ์๋ฌ ๋ฐฉ์ง ๋ฑ์ ์ข๊ฒ ๋ค์! |
||
|
|
||
| // ์ด๊ธฐ ์์น๋ก ์ด๊ธฐํ! (x, y) ํํ๋ก ๋ง๋ค์ | ||
| var posOfKing = toRealCoordinate(firstPositionOfKing) | ||
| var posOfStone = toRealCoordinate(firstPositionOfStone) | ||
|
|
||
| // ์ด๋ ๋ก์ง | ||
| for _ in 0..<movingCnt { | ||
| guard let command = readLine(), | ||
| // ๊ทผ๋ฐ ์๋ชป๋ ์ปค๋งจ๋๊ฐ ๋ค์ด์ค๋ฉด? ๋ฌธ์ ์กฐ๊ฑด์ ์๋ค. | ||
| let moving = direction[command] else { exit(0) } | ||
|
|
||
| let nextPosOfKing = (posOfKing.0 + moving.dx, posOfKing.1 + moving.dy) | ||
|
|
||
| // ๋ค์ ํน์ ์์น๊ฐ ์ฒด์คํ ์์ญ์ ์๋์ง | ||
| if (0..<8).contains(nextPosOfKing.0), (0..<8).contains(nextPosOfKing.1) { | ||
|
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. ์ด๋ฐ๊ฒฝ์ฐ์๋ ํญ์ 0 < nextPosOfKing.0 < 8 ์ด๋ฐ์์ผ๋ก ํด์ค ์๊ฐ๋งํ๋๋ฐ, containsํ๋๊น ํจ์ฌ ๊น๋ํ๊ฒ๊ฐ์์. ์ฐธ swift์ค๋ฌ์ด๊ฑธ ์ ์ฐ์๋๊ตฐ์ ๋ค์์ ์ ์จ๋จน๊ฒ ์ต๋๋ท
Member
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. ์คํธ ๋ง๋ค์ contains๋ฅผ ์ ์ฌ์ฉํด์ ๋ณด๊ธฐ ์ข๋ค์!
Member
Author
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. ์ ๋ ์ฒ์์จ๋ดค๋๋ฐ. ์ ๊ธฐ์ต์ผ๋ก (0..<cnt).forEach ์ด๋ฐ ๊ตฌ๋ฌธ์ ์ฐ๋ ค๊ณ ํ๋ฉด์ |
||
|
|
||
| // ๋์ด๋ ์์น๊ฐ ๊ฐ์์ง๋ค๋ฉด | ||
| if nextPosOfKing == posOfStone { | ||
| let nextPosOfStone = (posOfStone.0 + moving.dx, posOfStone.1 + moving.dy) | ||
|
|
||
| // ๋ค์ ๋์ ์์น๊ฐ ์ฒด์คํ ์์ญ์ ์๋์ง | ||
| if (0..<8).contains(nextPosOfStone.0), (0..<8).contains(nextPosOfStone.1) { | ||
|
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. ํ ์ด๊ฒ๋ ๋๋ ๋๊ฐ๋ค ใ ใ
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. ๊ธ๋ก๋ ์ฝ๋๋ ๊ฑฐ์ ๋๊ฐ๋ค์! 0..<8 ๋ฒ์๊ฐ ์์ฃผ ๋์ค๋๋ฐ ์ด๋ฐ ๊ฐ์ ์์๋ก ๋นผ์ค๋ ์ข์ ๋ฏ ํฉ๋๋ค! let chessMapRange: Range<Int> = 0..<8
Member
Author
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. ๋งค์ง๋๋ฒ ์กฐ์ฌํด์ผ๊ฒ ์ด์! |
||
| posOfKing = nextPosOfKing | ||
| posOfStone = nextPosOfStone | ||
| } | ||
| } else { | ||
| posOfKing = nextPosOfKing | ||
| } | ||
| } | ||
| } | ||
|
Comment on lines
+39
to
+62
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. ์ด ๋ถ๋ถ์ ์ ์ ๋๋ฌด ๋๊ฐ์ด ํ์๋๋ฐ์ ? import Foundation
// ๊ฐ์ฅ ์๋๊ฐ 1 ๊ฐ์ฅ ์๊ฐ 8 ํ์ -๊ฐ ์๋ก๊ฐ๋๊ฒ.
let direction: [String: (Int, Int)] = [
"R": (1,0),
"L": (-1,0),
"B": (0,-1),
"T": (0,1),
"RT": (1,1),
"LT": (-1,1),
"RB": (1,-1),
"LB": (-1,-1)
]
// .first/.last๋ ํญ์ Optional(Element?) ํ์
, .asciiValue, unicodeScalar๋ ๊ฐ์ด ์์์๋ ์์ด์ Optional๋ก ๋ฐํ
func transformToNum(_ chess: String) -> (Int, Int) {
let colValue = Int(chess.first!.asciiValue! - Character("A").asciiValue!)
let rowValue = Int(String(chess.last!))! - 1// Int()๋ String, Double, Float, NSNumber๋ฑ์ ๋จ Char๋ X
return (colValue, rowValue)
}
func transformToChess(_ target: (Int,Int)) -> String {
let colChar = String(UnicodeScalar(target.0 + Int(Character("A").asciiValue!))!)
let rowChar = String(target.1 + 1)
return colChar + rowChar
}
let targetInfo = readLine()!.split(separator: " ").map{ String($0) }
var kingInfo = transformToNum(targetInfo[0])
var stoneInfo = transformToNum(targetInfo[1])
let testCase = Int(targetInfo[2])!
func solution() {
for _ in 0..<testCase {
let moveInfo = readLine()!
guard let move = direction[moveInfo] else { continue }
let nKing = (kingInfo.0 + move.0 , kingInfo.1 + move.1)
// ์ฒด์คํ ๋ฒ์ด๋๋ฉด ๋ฌด์ 0 Based Indexing
if nKing.0 < 0 || nKing.0 >= 8 || nKing.1 < 0 || nKing.1 >= 8 {
continue
}
//ํน์ด ์ด๋ํ ์๋ฆฌ์ ๋์ด ์์ผ๋ฉดใ
if nKing == stoneInfo {
let nStone = (stoneInfo.0 + move.0, stoneInfo.1 + move.1)
if nStone.0 < 0 || nStone.0 >= 8 || nStone.1 < 0 || nStone.1 >= 8 {
continue
}
//๋ ์ด๋ ๊ฐ๋ฅํ๋ฉด ใฑใฑ
kingInfo = nKing
stoneInfo = nStone
} else {
//๊ทธ๋ฅ ํน๋ง
kingInfo = nKing
}
}
print(transformToChess(kingInfo))
print(transformToChess(stoneInfo))
}
contains์ if or๋ฌธ and๋ฌธ ๋น๊ต๊ฐ ๋ญ๊ฐ ๋ ๋น ๋ฅธ์ง ๊ถ๊ธํ๋ค์ !
Member
Author
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. ์ฐพ์๋ณด๋.. contains์ฌ์ฉ์ผ๋ก๋ ๋ด๋ถ์ ์ผ๋ก ํจ์ ํธ์ถ๋ ์๊ณ ํด์ ์ฝ๊ฐ ๋๋ฆด ์ ์์ง๋ง ๋ฏธ๋นํด๋ณด์
๋๋ค. ์ถ๊ฐ์ ์ผ๋ก contains ์ฌ์ฉ์ ๋ณด๋ค. ๋ณธ ๋ฌธ์ ์์ ์ง๊ด์ ์ด๊ณ ๊ฐ๊ฒฐํ ์ธก๋ฉด์์ ์ข์๋ณด์ด๋ค์! |
||
|
|
||
| print(toChessCoordinate(from: posOfKing)) | ||
| print(toChessCoordinate(from: posOfStone)) | ||
|
|
||
| // MARK: - Functions | ||
| // asciiValue: ๋ฌธ์ โ ์ซ์ (ex. โAโ โ 65) | ||
| // UnicodeScalar: ์ซ์ โ ๋ฌธ์ (ex. 65 โ โAโ) | ||
|
|
||
| func toRealCoordinate(_ pos: String) -> (Int, Int) { | ||
| let x = Int(pos.first!.asciiValue! - 65) | ||
| let y = 8 - Int(String(pos.last!))! | ||
| return (x, y) | ||
| } | ||
|
Comment on lines
+68
to
+75
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. ์ขํ ๋ณํ์ ์์คํค๋ก ํ์ จ๊ตฐ์.. ๋ค๋ฅธ ํ์ด๋ ๋ธ๋ก๊ทธ ๊ธ๋ ์ข ๋ดค๋๋ฐ ๋๋ถ๋ถ์ด ์ด๋ ๊ฒ ํ์ดํ์ ๋ฏ! ์ข์ ์ ๊ทผ์ ๋๋ค~! |
||
|
|
||
| func toChessCoordinate(from position: (Int, Int)) -> String { | ||
| let x = String(UnicodeScalar(position.0 + 65)!) | ||
| let y = String(8 - position.1) | ||
| return x + y | ||
| } | ||
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.
์ค ์ด๊ฑด ๋๋ ๊ฐ๊ตฐ