-
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
Conversation
giljihun/๊ตฌํ/[BOJ] ํน.swift
Outdated
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.
์ค ์ด๊ฑด ๋๋ ๊ฐ๊ตฐ
giljihun/๊ตฌํ/[BOJ] ํน.swift
Outdated
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.
์ค ์ด๊ฒ๋ ๋๋ ๊ฐ๋ค
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.
์ ๋ enum์ผ๋ก ํด์คฌ์ด์ !
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)
}
}
}์๋์ ์ผ๋ก ์ผ์ด์ค๊ฐ ๋๋๋ ๊ฒ์ ๋ํด์๋ enum์ ์ข ์จ๋ดค์ต๋๋ค. ์ฌ์ฉํ ๋๋ command.getDirection()ํ๋ฉด์ ํด๋น ์ปค๋งจ๋์ ๋ํ ์ด๋ํ ์ขํ๊ฐ์ ๊ฐ์ ธ์์ด์ !
๊ธธ์ ์๋์ฝ๋๋ณด๋๊น dictionary๋ก ํ๋ฉด ์ฐธ์กฐ ๋๋ฒ ์๊ฑฐ์ณ๋ ๋๊ณ ํจ์ฌ ๊น๋ํ ๊ฒ ๊ฐ๋ค์!
์๋ง ์ฌ๊ธฐ์ enum๋ฐฉ์์ด ๋น์ ๋ณด๋ ค๋ฉด ์ฌ๊ธฐ(enum)์์์ ๋ค๋ฅธ ๋ฉ์๋๋ฅผ ๋ง๋ค๋์ง.. ์ข ๋ ๋ง์ ๊ฒ์ ํ์ด์ผ ํ๋ ๊ฒ ๊ฐ์ต๋๋ท
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.
์คํธ ์ ๋ ๋์ ๋๋ฆฌ ์ฌ์ฉํ๋๋ฐ enum์ผ๋ก ํ๋ฉด ํด๋จผ ์๋ฌ ๋ฐฉ์ง ๋ฑ์ ์ข๊ฒ ๋ค์!
giljihun/๊ตฌํ/[BOJ] ํน.swift
Outdated
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.
ํ ์ด๊ฒ๋ ๋๋ ๋๊ฐ๋ค ใ ใ
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.
๊ธ๋ก๋ ์ฝ๋๋ ๊ฑฐ์ ๋๊ฐ๋ค์! 0..<8 ๋ฒ์๊ฐ ์์ฃผ ๋์ค๋๋ฐ ์ด๋ฐ ๊ฐ์ ์์๋ก ๋นผ์ค๋ ์ข์ ๋ฏ ํฉ๋๋ค!
let chessMapRange: Range<Int> = 0..<8There 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.
๋งค์ง๋๋ฒ ์กฐ์ฌํด์ผ๊ฒ ์ด์!
|
@sep037 ์ ๊ฐ ์ฝ๋ค๊ณ ํ ์ด์ ๋ ์ด๋ฏธ ํ์ด๋ดค๋ ๋ฌธ์ ์๊ธฐ ๋๋ฌธ์ ๊ทธ๋ฐ ๊ฑฐ์์ด์.............. ^^ ๊ทธ๋ฆฌ๊ณ ๊ธธ ๋ฌธ์ ์น๊ณค ์ฌ์ ๋ค? |
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.
์ฟ ์ฟ ํ์ด๋ดค๋ ๋ฌธ์ ๋ค์ด ๋ฑ์ฅํ๊ธฐ ์์ํ๊ตฐ์.
์ ์ ํจ์ํ ํ๋ก๊ทธ๋๋ฐ ๋ฐฉ์์ ์ ์ํด๋ณด์๊ณ ์ด ๋ฌธ์ ๋ฅผ ํ์ตํ๋ฉด์ ํ์์๋ ๊ธฐ์ต์ด๋ฉ๋๋ค..
๋ฌธ์ ๋ฅผ ์ฒ์๋ถํฐ ํ์ง ์๊ณ ์์ ๋ฌธ์ ๋ฅผ ๋ณด๊ณ ๋ค๋ฌ๋ ์ ๋๋ก๋ง ํ์ต๋๋ค..!
๋ค๋ค ์ขํ๋ฅผ ์์คํค๋ก ๋ณํํ์ จ๊ตฐ์? ์ํ์ ์ ๊ทผ.. ์ ์์ง ์ด๋ ค์์ ์ง๊ด์ ์ผ๋ก ํ๋์. ์ง๊ด์ ์ธ๊ฒ ์ข์๊ฑฐ๊ฐํ์~~(์์๋ฆฌ ๊ฐ๋ฅ)
import Foundation
let moveFunctions: [String: (Int) -> Int] = [
"R": { $0 + 1 },
"L": { $0 - 1 },
"T": { $0 + 8 },
"B": { $0 - 8 },
"RT": { $0 + 9 },
"LT": { $0 + 7 },
"RB": { $0 - 7 },
"LB": { $0 - 9 }
]
// ํน๊ณผ ๋์ด ๊ฐ์ ์์น์ ์๋์ง ํ์ธํ๋ ํจ์
func checkSamePosition(king: Int, stone: Int) -> Bool {
return king == stone
}
// ์ฃผ์ด์ง ์์น์์ ํด๋น ๋ฐฉํฅ์ผ๋ก ์ด๋ ๊ฐ๋ฅํ์ง ํ์ธํ๋ ํจ์
func moveableCheck(position: Int, command: String) -> Bool {
switch command {
case "R": return (position + 1) % 8 == 0 ? false : true
case "L": return (position == 0 || position % 8 == 0) ? false : true
case "T": return 56...63 ~= position ? false : true
case "B": return 0...7 ~= position ? false : true
case "RT": return (position + 1) % 8 == 0 || 56...62 ~= position ? false : true
case "LT": return (position == 0 || position % 8 == 0) || 56...62 ~= position ? false : true
case "RB": return 0...7 ~= position || (position + 1) % 8 == 0 ? false : true
case "LB": return 0...7 ~= position || (position == 0 || position % 8 == 0) ? false : true
default: return false
}
}
// ํน๊ณผ ๋์ ์ด๋์ ์ฒ๋ฆฌํ๋ ํจ์
// ์ด๋์ด ๋ถ๊ฐ๋ฅํ ๊ฒฝ์ฐ nil์ ๋ฐํ
func processMove(king: Int, stone: Int, command: String) -> (king: Int, stone: Int)? {
// ์ด๋ ํจ์๊ฐ ์กด์ฌํ๊ณ , ํน์ด ์ด๋ ๊ฐ๋ฅํ์ง ํ์ธ
guard let moveFunction = moveFunctions[command],
moveableCheck(position: king, command: command) else {
return nil
}
// ํน์ ์๋ก์ด ์์น ๊ณ์ฐ
let newKing = moveFunction(king)
// ํน์ด ๋๊ณผ ๊ฐ์ ์์น๋ก ์ด๋ํ๋ ๊ฒฝ์ฐ
if checkSamePosition(king: newKing, stone: stone) {
// ๋๋ ๊ฐ์ ๋ฐฉํฅ์ผ๋ก ์ด๋ ๊ฐ๋ฅํ์ง ํ์ธ
guard moveableCheck(position: stone, command: command) else {
return nil
}
// ํน๊ณผ ๋ ๋ชจ๋ ์ด๋
return (newKing, moveFunction(stone))
}
// ํน๋ง ์ด๋
return (newKing, stone)
}
// ์ฒด์คํ์ ๋ชจ๋ ์์น๋ฅผ ๋ฌธ์์ด๋ก ํํํ ๋ฐฐ์ด
let chessMap: [String] = ["A1", "B1", "C1", "D1", "E1", "F1", "G1", "H1",
"A2", "B2", "C2", "D2", "E2", "F2", "G2", "H2",
"A3", "B3", "C3", "D3", "E3", "F3", "G3", "H3",
"A4", "B4", "C4", "D4", "E4", "F4", "G4", "H4",
"A5", "B5", "C5", "D5", "E5", "F5", "G5", "H5",
"A6", "B6", "C6", "D6", "E6", "F6", "G6", "H6",
"A7", "B7", "C7", "D7", "E7", "F7", "G7", "H7",
"A8", "B8", "C8", "D8", "E8", "F8", "G8", "H8"]
// Input
let input: [String] = readLine()!.components(separatedBy: " ").map { $0 }
var kingPosition: Int = chessMap.firstIndex(of: input[0])!
var stonePosition: Int = chessMap.firstIndex(of: input[1])!
let cycle: Int = Int(input[2])!
for _ in 0..<cycle {
let command: String = readLine()!
// ์ด๋์ด ๊ฐ๋ฅํ ๊ฒฝ์ฐ์๋ง ์์น ์
๋ฐ์ดํธ
if let newPositions = processMove(king: kingPosition, stone: stonePosition, command: command) {
kingPosition = newPositions.king
stonePosition = newPositions.stone
}
}
// ์ต์ข
์์น ์ถ๋ ฅ
print(chessMap[kingPosition])
print(chessMap[stonePosition])
giljihun/๊ตฌํ/[BOJ] ํน.swift
Outdated
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.
๋ฌธ์์ด ์ชผ๊ฐค ๋ split๋์ components๋ฅผ ์ฐ์ ์ด์ ๊ฐ ์์๊น์? split์ด substring์ ๋ฐํํ๊ธฐ ๋๋ฌธ์ String ์์ฐ๋ ๊ฒ ๋ณด๋ค components๊ฐ ๋ซ๋ค๊ณ ์๊ฐํ์ จ๋์?? ์ด๊ฒ ํจ์จ์ด ์ข๋์?? ํน์ ๊ฐ๋ ์ฑ?? (๋ฌผ์ํ์ด์ธ๋ง)
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.
๋ถํ์ํ String ๋ณํ์ด ๋ง๋ค๋ฉด, split์ด ๋์ ์๋ ์์ง๋ง
components๊ฐ ๋ณต์ฌ ๋น์ฉ์ด ์๊ธฐ์ ์ผ๋ถ ์ฐจ์ด๊ฐ ์์ง๋ง,
์ ๊ฐ ์๊ธฐ๋ก ์ฑ๋ฅ ์ฐจ์ด๊ฐ ๊ฑฐ์ ์๊ณ ๋ณธ ๋ฌธ์ ์์ ๊ฐ๋
์ฑ ์ธก๋ฉด์์ components๊ฐ ๊ฐ๊ฒฐํ๊ณ ์ข๋ค๊ณ ํ๋จํ์ต๋๋ค!
giljihun/๊ตฌํ/[BOJ] ํน.swift
Outdated
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.
๊ธ๋ก๋ ์ฝ๋๋ ๊ฑฐ์ ๋๊ฐ๋ค์! 0..<8 ๋ฒ์๊ฐ ์์ฃผ ๋์ค๋๋ฐ ์ด๋ฐ ๊ฐ์ ์์๋ก ๋นผ์ค๋ ์ข์ ๋ฏ ํฉ๋๋ค!
let chessMapRange: Range<Int> = 0..<8
giljihun/๊ตฌํ/[BOJ] ํน.swift
Outdated
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.
์ขํ ๋ณํ์ ์์คํค๋ก ํ์ จ๊ตฐ์.. ๋ค๋ฅธ ํ์ด๋ ๋ธ๋ก๊ทธ ๊ธ๋ ์ข ๋ดค๋๋ฐ ๋๋ถ๋ถ์ด ์ด๋ ๊ฒ ํ์ดํ์ ๋ฏ! ์ข์ ์ ๊ทผ์ ๋๋ค~!
giljihun/๊ตฌํ/[BOJ] ํน.swift
Outdated
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.
์ด๋ฐ๊ฒฝ์ฐ์๋ ํญ์
0 < nextPosOfKing.0 < 8 ์ด๋ฐ์์ผ๋ก ํด์ค ์๊ฐ๋งํ๋๋ฐ, containsํ๋๊น ํจ์ฌ ๊น๋ํ๊ฒ๊ฐ์์. ์ฐธ swift์ค๋ฌ์ด๊ฑธ ์ ์ฐ์๋๊ตฐ์ ๋ค์์ ์ ์จ๋จน๊ฒ ์ต๋๋ท
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.
์คํธ ๋ง๋ค์ contains๋ฅผ ์ ์ฌ์ฉํด์ ๋ณด๊ธฐ ์ข๋ค์!
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.
์ ๋ ์ฒ์์จ๋ดค๋๋ฐ. ์ ๊ธฐ์ต์ผ๋ก (0..<cnt).forEach ์ด๋ฐ ๊ตฌ๋ฌธ์ ์ฐ๋ ค๊ณ ํ๋ฉด์
๋น์ทํ ๊ฐ๊ฒฐ์ฑ์ ๊ฐ์ง .contains๋ฅผ ์ฐจ์ฉํ์ต๋๋ค! ใ
ใ
bishoe01
left a comment
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.
import Foundation
typealias Position = (row: Int, col: Int)
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)
}
}
}
// var Board = Array(repeating: Array(repeating: 0, count: 8), count: 8)
func isRange(_ pos: Position) -> Bool {
return pos.row >= 0 && pos.row <= 7 && pos.col >= 0 && pos.col <= 7
}
// ์ด๋ฏธ ๋ณํํ ์น๊ตฌ๋ฅผ ๋ค์ ์์คํค๋ก ๋๋ ค์ค๋ ์์
func translate(_ pos: Position) -> String {
return String(UnicodeScalar(pos.col + 65)!) + String(pos.row + 1)
}
func solution(king locationKing: String, stone locationStone: String, _ n: Int, _ commands: [String]) {
let king = Array(locationKing)
let stone = Array(locationStone)
var kingPos: Position = (row: Int(String(king[1]))! - 1, col: Int(king[0].asciiValue! - 65))
var stonePos: Position = (row: Int(String(stone[1]))! - 1, col: Int(stone[0].asciiValue! - 65))
// ์์์ ์ฒจ๋ถํ ์ฌ์ง ๋ก์ง ๊ทธ๋๋ก ํ๋ฆ ๊ฐ์ ธ๊ฐ์ด์ !
// ๋ก์ง ์์
for command in commands {
let command = Command(rawValue: command)!.getDirection()
let newKingPos: Position = (row: kingPos.row + command.row, col: kingPos.col + command.col)
// ๊ฐ ์ ์๋ ์๋ ?
if !isRange(newKingPos) {
// print("range")
continue
}
else {
// ๊ณต๋ฐฑ์ ํน ์ฎ๊ธฐ๊ธฐ
if newKingPos != stonePos {
kingPos = newKingPos
}
else {
let newStonePos = (row: stonePos.row + command.row, col: stonePos.col + command.col)
// ๋์ด ๋ชป๊ฐ๋ !
if !isRange(newStonePos) {
continue
}
// ๋๋ ์ด๋ํ ์ ์์ผ๋ ๋๋ค ์ฎ๊ธฐ๊ธฐ
else {
kingPos = newKingPos
stonePos = newStonePos
}
}
}
}
print(translate(kingPos))
print(translate(stonePos))
}์ ๋ ์ฒ์์ ํ(A ~ H) ์ด(1 ~ 8) ์ด๋๊น ์ด๊ฑธ ์๋ ๋ณด๋(2์ฐจ์๋ฐฐ์ด)์์ ๊ฑด๋๋ ค์ฃผ๋ ค๊ณ
ํ์ ์ซ์๋ก ๋ณํ์์ผ์ 2์ฐจ์๋ฐฐ์ด์์ ์ฉ์ดํ๊ฒ ์ด๋ํด์ฃผ๋ ค๊ณ ์์์ ํ๋๋ฐ, ๋ง์ ์์ฑํ๊ณ ๋ณด๋ ๊ตณ์ด 2์ฐจ์๋ฐฐ์ด์ด ํ์์๊ฒ ๋ค ์ถ์ด์ ๋นผ์คฌ์ต๋๋ค.
๊ณ ๋ คํ ๊ฑฐ๋
- 0~7์์ ๋ฒ์ด๋๋?
- ์ด๋ํ ๊ณต๊ฐ์ ๋์ด ์๋?
- ์ด๋ํ ๊ณต๊ฐ์ ๋์ด ์์ผ๋ฉด ๋๋ ์ด๋ํ ์ ์๋?
์ด๊ฑฐ 3๊ฐ๋ผ๊ณ ์๊ฐํด์ ๋์จ ์ฌ์ง์ ํ๋ฆ๋๋ก ์กฐ๊ฑด๋ฌธ์ ํ๋ฆ์ ๋ฐ๋ผ๊ฐ์ ๋ค๋ฉด ์ดํด๊ฐ ์ฌ์ธ ์ ์์ต๋๋ค!
๊ทธ๋ ๊ฒํ๊ณ ๋ง์ง๋ง์ ์ด์ ๋ค์ ๊ฒฐ๊ณผ๊ฐ์ธkingPos์stonePos๋ฅผtranslateํจ์๋ฅผ ํตํด ์์คํค๋ก ๋ณํํด์ ์ถ๋ ฅํด์คฌ์ด์.
// ์๋๋ ์๋ ์๋ํ๋ ค๊ณ ํ์๋ chessPosition ๊ตฌ์กฐ์ฒด ์ฝ๋ ์
๋๋ค.
// ์ฌ๊ธฐ์์์ isRange ํจ์๋ ์ฐ๊ณ , translate ํจ์๋ ์ฐ๊ณ ํ๋ คํ๋๋ฐ, ๋๋ฌด ์ต์ํ์ง ์์ ๋ฐฉํฅ์ด๋ผ์ ์ด๋ป๊ฒ ํ์ฉํ ์ง ์ ๋ชจ๋ฅด๊ฒ ๋๋ผ๊ตฌ์..! ๊ทธ๋์ ์ค๋ํ์ฐจํ์
จ์ต๋๋ค.
struct ChessPosition {
var row: Int
var col: Int
init(_ position: String) {
let chars = Array(position)
self.row = Int(String(chars[1]))! - 1
self.col = Int(chars[0].asciiValue! - Character("A").asciiValue!)
}
}
alstjr7437
left a comment
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.
๋ค๋ฅธ ๋ถ๋ค ๋ฆฌ๋ทฐ๋ฅผ ๋ค ์๋ฌ์์ฃผ์ ์ ์ข๋ค์!!
์ฒ์์ ํ ์ฒด์ค๊ฒ์ ๊ตฌํ์ธ๊ฐ ํ๊ณ ๊ฒ๋จน์๋ค๊ฐ ๋คํ์ด ์ด๋์ด๋ผ ํธํ๊ฒ ํ์์ต๋๋ค!!
์ฝ๋๋ ๋ค๋ฅธ๋ถ๋ค์ด๋ ๊ฑฐ์ ๋น์ทํ ๊ฒ ๊ฐ์์!
giljihun/๊ตฌํ/[BOJ] ํน.swift
Outdated
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.
์คํธ ์ ๋ ๋์ ๋๋ฆฌ ์ฌ์ฉํ๋๋ฐ enum์ผ๋ก ํ๋ฉด ํด๋จผ ์๋ฌ ๋ฐฉ์ง ๋ฑ์ ์ข๊ฒ ๋ค์!
giljihun/๊ตฌํ/[BOJ] ํน.swift
Outdated
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.
์คํธ ๋ง๋ค์ contains๋ฅผ ์ ์ฌ์ฉํด์ ๋ณด๊ธฐ ์ข๋ค์!
YooGyeongMo
left a comment
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.
์ ๋ 0 Based indexing์ผ๋ก ํ์ด์ -1, +1 ์ ํด์ฃผ์ด์ผํ๊ณ
์ฒด์ค๋ณด๋๋ฅผ ๋ง๋ค์ง ์๊ณ ์์ฒด์ ์ผ๋ก ์ฒดํฌํด์ฃผ๋ฉด๋๋ค ์๊ฐํด์ ๋ฐ๋ก ์ฒด์คํ์ ์ถ์ ํ์ง์์์ต๋๋ท !
giljihun/๊ตฌํ/[BOJ] ํน.swift
Outdated
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.
์ ๋ ฅ ๋ํ ์ผ ... He knows the way !!! ์บฌ..
giljihun/๊ตฌํ/[BOJ] ํน.swift
Outdated
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.
์ค 8*8 ํ๋ ฌ์ ๋ง๋์ จ๊ตฐ์ ! ์ด๋ฒ์ ์ ๋ ์ ๋ง๋ค๊ณ ์์ฒด์ ์ผ๋ก ๋ฒ์ด๋๋์ง ์ ๋ฒ์ด๋๋์ง๋ง ํ์ธํ์์ด์ !
giljihun/๊ตฌํ/[BOJ] ํน.swift
Outdated
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.
์ด ๋ถ๋ถ์ ์ ์ ๋๋ฌด ๋๊ฐ์ด ํ์๋๋ฐ์ ?
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๋ฌธ ๋น๊ต๊ฐ ๋ญ๊ฐ ๋ ๋น ๋ฅธ์ง ๊ถ๊ธํ๋ค์ !
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.
์ฐพ์๋ณด๋.. contains์ฌ์ฉ์ผ๋ก๋ ๋ด๋ถ์ ์ผ๋ก ํจ์ ํธ์ถ๋ ์๊ณ ํด์ ์ฝ๊ฐ ๋๋ฆด ์ ์์ง๋ง ๋ฏธ๋นํด๋ณด์
๋๋ค.
๋ฐ๋ฏธ๊ฐ์ฒ๋ผ ์ง์ ์กฐ๊ฑด์ ๋น๊ตํ๋ ๋ฐฉ์์ ์ฐ์ฐ์ผ๋ก๋ง ์ฒ๋ฆฌ๋ฅผ ํ๋ ๋ ๋น ๋ฅผ ๊ฒ ๊ฐ์ต๋๋ค!
์ถ๊ฐ์ ์ผ๋ก contains ์ฌ์ฉ์ ๋ณด๋ค. ๋ณธ ๋ฌธ์ ์์ ์ง๊ด์ ์ด๊ณ ๊ฐ๊ฒฐํ ์ธก๋ฉด์์ ์ข์๋ณด์ด๋ค์!
๋ฌผ๋ก ์กฐ๊ฑด๋ฌธ์ผ๋ก ๋์ดํ๋ฉด~ ์์ธ์ฒ๋ฆฌ๋ ์ด๋ฐ ๋ถ๋ถ์์ ๋์ฑ ๊ฐ์ ์ด ์์ด๋ณด์ฌ์!


๐ ๋ฌธ์ ๋งํฌ
ํน
โ๏ธ ์์๋ ์๊ฐ
1h
โจ ์๋ ์ฝ๋
์ ๊ทผ
8x8 ์ฒด์คํ ๋ง๋ค๊ธฐ-> ์ค์ ๊ตฌํ์ค์ ๋ฒ๋ ค์ก์๋๋ค.ํต์ฌ ๊ตฌํ ๋์
๋ณธ ๋ฌธ์ ์ ํต์ฌ์ ๊ตฌํ์ค์์๋
"์ํ๋ฒณ -> ์ขํ" ๋ณํ ์ ์ ๊ตฌํํ๋ ๋ฌธ์ ์ธ๊ฑฐ๊ฐ์ต๋๋ค.
์๋๋ฉด,
์ค์ ํจ์๋ถ๋ฆฌ๋ ์๋์ฒ๋ผ ์ปจ๋ฒํฐ ๊ตฌํ๋ง ํ๋๋ฐ์!
์ด ๋๊ฐ๋ ์ตํ๋๋ฉด ๋ถ๋ช ํจ์ฉ์ด ์์ด๋ณด์ ๋๋ค!
Int() ์ด๊ธฐํ๋ ํญ์ ์ต์ ๋(Int?)์ ๋ฑ๋๋ค.
๋ณธ ์ฝ๋๋ฅผ ๋ณด๋ฉด ๊ธฐ์กด์ ์์ฒ๋ผ ์์ฑํ๋ค๊ฐ guard let์ผ๋ก ๊ฐ์ผ ๋ถ๋ถ์ด ์์ด์.
์ด์ ๊ฐ ์ค๊น์?
์ด์ ๋ movingCnt๋ฅผ ํ์ฉํ๋ for๋ฌธ์ ์์ต๋๋ค.
guard let์ผ๋ก ์ต์ ๋์ ์ธ๋ํ ํด์ฃผ์ง ์์ผ๋ฉด, movingCnt์๋ ์ต์ ๋ Int๊ฐ ๋ค์ด๊ฐ๊ธฐ ๋๋ฌธ์ for๋ฌธ์ ์กฐ๊ฑด์ผ๋ก ๋ฃ์ ์๊ฐ ์์ง์.
Int()๋ ๋ณํ ์คํจ ๊ฐ๋ฅ์ฑ ๋๋ฌธ์ ํญ์ Int? ํ์ ์ ๊ฐ์ ๋ฐํํฉ๋๋ค.
ํ๊ธฐ
๋ค๋ฅธ ๊ตฌํ์ ์ผ๋ฐ์ ์ธ ๊ตฌํ์ด๋ผ ๋ฐ๋ก ์ ์ง ์๊ฒ ์ต๋๋ค.
์ฝ๋๋ณด๊ณ ๊ถ๊ธํ๊ฑฐ ์ฌ์ญค๋ด์ฃผ์ธ์ !! ~