Skip to content
Open
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
71 changes: 71 additions & 0 deletions WEEK12/백준_14499_주사위굴리기/liv.swift
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]
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

한 줄에 세미콜론(;)을 사용하여 여러 변수를 선언하는 것은 가독성을 떨어뜨립니다. Swift 스타일 가이드에 따라 각 선언을 별도의 줄로 분리하는 것이 좋습니다.

Suggested change
let n = line[0]; let m = line[1]; let x = line[2]; let y = line[3]
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
}
}
52 changes: 52 additions & 0 deletions WEEK12/백준_14890_경사로/liv.swift
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]
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

가독성을 위해 한 줄에 여러 문장을 작성하는 대신 줄바꿈을 사용하여 선언을 분리하는 것이 좋습니다.

Suggested change
let n = nl[0]; let l = nl[1]
let n = nl[0]
let l = nl[1]

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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

반복문을 사용하여 열(column) 데이터를 수동으로 추출하는 대신, map 함수를 사용하면 더 간결하고 Swift다운 코드를 작성할 수 있습니다.

Suggested change
var column = [Int]()
for row in map { column.append(row[i]) }
let column = map.map { $0[i] }

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
}