Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions giljihun/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
| 1์ฐจ์‹œ | 2025.03.30 | DP | [1๋กœ ๋งŒ๋“ค๊ธฐ](https://www.acmicpc.net/problem/1463)|https://github.com/AlgoLeadMe/AlgoLeadMe-14/pull/4|
| 2์ฐจ์‹œ | 2025.04.08 | DP | [๋™์ „ 1](https://www.acmicpc.net/problem/2293)|https://github.com/AlgoLeadMe/AlgoLeadMe-14/pull/8|
| 3์ฐจ์‹œ | 2025.04.30 | ๋ฌธ์ž์—ด | [IOIOI](https://www.acmicpc.net/problem/5525)|https://github.com/AlgoLeadMe/AlgoLeadMe-14/pull/15|
| 4์ฐจ์‹œ | 2025.04.30 | ๊ตฌํ˜„ | [ํ‚น](https://www.acmicpc.net/problem/1063)|https://github.com/AlgoLeadMe/AlgoLeadMe-14/pull/23|
---
81 changes: 81 additions & 0 deletions giljihun/๊ตฌํ˜„/[BOJ] ํ‚น.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/* MARK: - SOLUTION

1. 8x8 ์ฒด์ŠคํŒ ๋งŒ๋“ค๊ธฐ
2. ์ปจํŠธ๋กค๋Ÿฌ ์ •์˜
3. ์ด๋™ ๋กœ์ง
4. ๋Œ์„ ๋ฐ€์น˜๋Š” ๋กœ์ง

ํ•ต์‹ฌ ๊ตฌํ˜„์€.. ์•ŒํŒŒ๋ฒณ -> ์ขŒํ‘œ ๋ณ€ํ™˜
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์˜ค ์ด๊ฑด ๋‚˜๋ž‘ ๊ฐ™๊ตฐ


์•„์Šคํ‚ค ์ฝ”๋“œ ์“ฐ๋ฉด๋ ๋“ฏ
๊ทผ๋ฐ ์–ด์ผ€ ๋ณ€ํ™˜ํ•˜์ง€

*/
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
Copy link
Collaborator

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๊ฐ€ ๋‚ซ๋‹ค๊ณ  ์ƒ๊ฐํ•˜์…จ๋‚˜์š”?? ์ด๊ฒŒ ํšจ์œจ์ด ์ข‹๋‚˜์š”?? ํ˜น์€ ๊ฐ€๋…์„ฑ?? (๋ฌผ์Œํ‘œ์‚ด์ธ๋งˆ)

Copy link
Member Author

@giljihun giljihun May 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๋ถˆํ•„์š”ํ•œ String ๋ณ€ํ™˜์ด ๋งŽ๋‹ค๋ฉด, split์ด ๋‚˜์„ ์ˆ˜๋„ ์žˆ์ง€๋งŒ
components๊ฐ€ ๋ณต์‚ฌ ๋น„์šฉ์ด ์žˆ๊ธฐ์— ์ผ๋ถ€ ์ฐจ์ด๊ฐ€ ์žˆ์ง€๋งŒ,
์ œ๊ฐ€ ์•Œ๊ธฐ๋กœ ์„ฑ๋Šฅ ์ฐจ์ด๊ฐ€ ๊ฑฐ์˜ ์—†๊ณ  ๋ณธ ๋ฌธ์ œ์—์„  ๊ฐ€๋…์„ฑ ์ธก๋ฉด์—์„œ components๊ฐ€ ๊ฐ„๊ฒฐํ•˜๊ณ  ์ข‹๋‹ค๊ณ  ํŒ๋‹จํ–ˆ์Šต๋‹ˆ๋‹ค!


// let movingCnt = Int(N.components(separatedBy: " ")[2])
guard let movingCnt = Int(N.components(separatedBy: " ")[2]) else { exit(0) }
Comment on lines +17 to +23
Copy link
Collaborator

Choose a reason for hiding this comment

The 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)
Copy link
Collaborator

Choose a reason for hiding this comment

The 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)
]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์˜ค ์ด๊ฒƒ๋„ ๋‚˜๋ž‘ ๊ฐ™๋‹ค

Copy link
Collaborator

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)์•ˆ์—์„œ ๋‹ค๋ฅธ ๋ฉ”์„œ๋“œ๋ฅผ ๋งŒ๋“ค๋˜์ง€.. ์ข€ ๋” ๋งŽ์€ ๊ฒƒ์„ ํ–ˆ์–ด์•ผ ํ–ˆ๋˜ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ท

Copy link
Member

Choose a reason for hiding this comment

The 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) {
Copy link
Collaborator

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์Šค๋Ÿฌ์šด๊ฑธ ์ž˜ ์“ฐ์‹œ๋Š”๊ตฐ์š” ๋‹ค์Œ์— ์ž˜ ์จ๋จน๊ฒ ์Šต๋‹ˆ๋‹ท

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์˜คํ˜ธ ๋งž๋„ค์š” contains๋ฅผ ์ž˜ ์‚ฌ์šฉํ•ด์„œ ๋ณด๊ธฐ ์ข‹๋„ค์š”!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ €๋„ ์ฒ˜์Œ์จ๋ดค๋Š”๋ฐ. ์ œ ๊ธฐ์–ต์œผ๋กœ (0..<cnt).forEach ์ด๋Ÿฐ ๊ตฌ๋ฌธ์„ ์“ฐ๋ ค๊ณ ํ•˜๋ฉด์„œ
๋น„์Šทํ•œ ๊ฐ„๊ฒฐ์„ฑ์„ ๊ฐ€์ง„ .contains๋ฅผ ์ฐจ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค! ใ…‹ใ…‹


// ๋Œ์ด๋ž‘ ์œ„์น˜๊ฐ€ ๊ฐ™์•„์ง„๋‹ค๋ฉด
if nextPosOfKing == posOfStone {
let nextPosOfStone = (posOfStone.0 + moving.dx, posOfStone.1 + moving.dy)

// ๋‹ค์Œ ๋Œ์˜ ์œ„์น˜๊ฐ€ ์ฒด์ŠคํŒ ์˜์—ญ์— ์žˆ๋Š”์ง€
if (0..<8).contains(nextPosOfStone.0), (0..<8).contains(nextPosOfStone.1) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ํ—‰ ์ด๊ฒƒ๋„ ๋‚˜๋ž‘ ๋˜‘๊ฐ™๋‹ค ใ…‹ใ…‹

Copy link
Collaborator

@MuchanKim MuchanKim May 17, 2025

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

Copy link
Member Author

Choose a reason for hiding this comment

The 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
Copy link
Collaborator

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๋ฌธ ๋น„๊ต๊ฐ€ ๋ญ๊ฐ€ ๋” ๋น ๋ฅธ์ง€ ๊ถ๊ธˆํ•˜๋„ค์š” !

Copy link
Member Author

Choose a reason for hiding this comment

The 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
Copy link
Collaborator

Choose a reason for hiding this comment

The 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
}