-
Notifications
You must be signed in to change notification settings - Fork 0
4-YooGyeongMo #22
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-YooGyeongMo #22
Conversation
update: commit ํ์ดํ๋ณ๊ฒฝ
giljihun
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.
๋ฐ๋ฏธ์ ์์ ์๋ฎฌ๋ ์ด์
+ ์ด๋ค ์๊ณ ๋ฆฌ์ฆ์ผ๋ก ํ์ด๋ณผ๋์ ์ ์ ๋ฌธ์ ๋ค์ ใ
ใ
๋ ์ด๋ป๊ฒ ๋ณด๋ฉด ์ ํ์ ์ธ DFS๋ฌธ์ ๊ฐ๊ธฐ๋ํ๊ณ ์.
์ฝ๋๊ฐ ์ค์ ์ธ์์ ๋ฐ์ํ๋ค๋ ๊ฐ์น๊ฐ ๋ด๊ธด ์ง๊ด์ ์ธ ์ค๊ณ ์์ฃผ ์ข์์ต๋๋ค.
๋ฐ๋ฏธ์ pr์ ์๋์ฝ๋์๋ ใ
์ฝ๋ฉํธ๋ฅผ ๋ฌ๊ณ ์ถ์ ์ ๋๋ก ์ ์ฝํ๊ณ ๋ญ๊ฐ ๊ฐ์ด ํ๊ณ ์๋ค๋ผ๋ ๋๋์ด ๋ค์ด์ ใ
ใ
์ ๋ ํ๊ธฐ๋ฐ bfs๋ก ๋ค์ ๋์ ํด๋ณด๊ฒ ์ต๋๋ค.
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.
์ฌ์ํ ์ง๋ฌธ๊ฐ๊ธฐ๋ ํ๋ฐ ๊ถ๊ธํด์ ์ฌ์ญค๋ณผ๊ฒ์!
์ด ํ๋ผ๋ฏธํฐ๊ฐ 4๊ฐ์ธ ํจ์๋ฅผ ๋ดค์ ๋, ๋ฐ๋ฏธ์์ ์ด๋ค ํ๋ผ๋ฏธํฐ๊ฐ ๊ฐ์ฅ ์ค์ํ๋ค๊ณ ์๊ฐ์ด ๋์๋์?
์ ๋ ๋ญ๊ฐ 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.
์ข์ ํผ๋๋ฐฑ์ด๋ค์ ๊ธธ ์ ๋ ๊ทธ๋ฐ ์๊ฐ์ด ๋ค์์ต๋๋ค.
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.
@giljihun ์ฌ์ค ๊ธธ ์ด์ผ๊ธฐ๋ฅผ ๋ฃ๊ณ ๋ณด๋... ํจ์ ํ๋ผ๋ฏธํฐ ์ ์ ์์ ์ค์๋ ํน์, ๊ฐ๋ ์ฑ์ ์๊ฐํ์ง ์๋ ๊ฒฝํฅ์ด ์๋๊ฑฐ ๊ฐ์์ ใ ใ ๋ฐ์ฑํ๊ฒ ๋ฉ๋๋ค. ์ฌ์ค ์ค์๋ ์ธก๋ฉด์์ ์๊ฐํ๊ธฐ ๋ณด๋ค๋ ์ค๊ณํ ๋ ์ผ๋จ ๊ตฌํํ๊ณ ๋ณด์๋ผ๋ ์๊ฐ์ผ๋ก ํจ์์ ๋ค ๋๋ ค๋ฃ์๋ ํนํ dfs/bfs ํ๋๋ ๊ทธ๋ฅ ๋ฃ๊ฒ ๋๋๋ฐ ๋ง๋ค์.. ์๊ฐํด๋ณด๋ x,y๊ฐ ์์์๋ค๋ฉด ? ์์ฃผ ์ข์์๊บผ๊ฐ์์.
- ๋ค๋งใ ใ dfs/bfs๋ฌธ์ ํผ๊ฑธ๋ณด๋ ์ฌ๊ท์ผ๋ ์ขํ๋ ๋ค์ชฝ์ ์ฐ๋ ๊ฒฝ์ฐ๊ฐ ๋ง๋๊ตฐ์ ์ ๋ ์๊ฐํ๊ณ ์ ์ด๋ด์ผ๊ฒ ์ด์ ๊ธธ ์ผ์ธ์ swift ์ปจ๋ฒค์ keeper @MuchanKim ์กฐ์ธ์ด๋ผ๋ฉด ๋ฐ์์ด์ผ์ฃ ํธํธ
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.
์ฌ๊ธฐ ์ฝ๋์ ์๋ฌธ์ด ์์ด์!
dfs๋ฅผ ๋ค์ด๊ฐ๋ฉด, ํ์ฌ ์์น๊ฐ ์ฒญ์๋์๋? ์ฒดํฌํ๊ณ ์ฒญ์๋ฅผ ํ์์์
// 1. ํ์ฌ ์์น๊ฐ ์ฒญ์๋์ง ์์๋ค๋ฉด -> ์ฒญ์
if board[y][x] == 0 {
board[y][x] = 2
}์ฌ๊ธฐ์ ์ฒ๋ฆฌ๋ฅผ ํ๋ค๊ณ ์ดํด๋ฅผ ํ๋๋ฐ,
๋ฉ์ธ์ฝ๋์์ ์ ์๋ฆฌ ์ฒญ์๋ฅผ ์ฒดํนํ๊ณ ํจ์๋ก ๋์ด๊ฐ๋ ์ด์ ๊ฐ ์์๊น์ ??
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.
์ค ๊ธธ, ์ค๋ณต์ฒญ์๊ธดํ๋ค์. ๋ค์ ๊ธฐ์ต์ ๋๋ฌ์ด๋ณด๋ , ์ ๋ ์ฌ์ค ์ฒ์์ ๊ทธ๋ฅ ์ ์ด์ ์ฒญ์ํ๊ฑฐ๋ผ๊ณ countํ๊ณ ์์ํ ๊น ์๋๋ฉด ์ด๋ป๊ฒํ ๊น ? ๊ณ ๋ฏผํ์๋๋ฐ์ .
์ฆ, board[r][c] = 2๋ฅผ ํ ์ด์ ๋ dfs ์์์๋ โ์ฒญ์ ์ ๋์ด ์๋ ์นธโ๋ง ์ฒญ์ํ๋ฏ๋ก ์์ ์์น๋ฅผ ๋ฏธ๋ฆฌ ์ฒญ์ ์ฒ๋ฆฌํ๊ณ , result = 1๋ก ์์ํด์ โ๋ฐฉ 1๊ฐ๋ ์ฒญ์ํจโ์ด๋ผ๊ณ ์
์ ์๊ฒ ํ ๊ฒ์ด๋ ์๋๊ฐ ์์์ด์.
๋ง์ฝ์ ์ ์ฝ๋๊ฐ ์๋ค๋ฉด dfs ๋ด๋ถ์์ ์ฒซ ์ฒญ์๊ฐ ์ผ์ด๋๊ณ , count = 0์์ ์์ํด๋ ๊ฒฐ๊ณผ๋ ๋์ผํ๋ค๊ณ ์๊ฐํ์ด์. ์ฆ, ์์ ์์น ์ฒญ์๋ฅผ ์ด๋์ ์ ํ ์ง์ ๋ํ ์๊ฐ ์ฐจ์ด์๋๊ฑฐ๊ฐ์์.
board[r][c] = 2 // ๋ฏธ๋ฆฌ ์ฒญ์ํ๋ค๊ณ ์ ์ธํ๊ณ
var result = 1 // "1์นธ ์ฒญ์ํ์" ์ผ๋ก ์์
dfs(result, d, c, r) // ๊ทธ๋์ dfs์์ ์ด๋ฏธ ์ฒญ์๋๋ค๊ณ ์ ์ ํน์ ๋ต๋ณ์ด ๋์์๊น์ฉ ? ๊ทผ๋ฐ ์ฌ์ค ์ค๋ณต์ฝ๋ ๊ฐ์์ ๋ฆฌํฉํ ๋งํ ํ์๊ฐ ์์ด๋ณด์ ๋๋ค.
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.
๋ฌธ์ ๊ฐ ์์ฒญ ๋ณต์กํ๋ค๊ณ ์๊ฐ์ ํ๋๋ฐ, ์ด๋ ๊ฒ dfs๋ก ์ ๋ฆฌ๋๊ฒ ๋ณด๋๊น ํ์ง์ ๋ค๋ก ๋นผ์ฃผ๋ฉด turnleft์๋ ๊น๋ํ dfs๋ค์ ์๋ดค์ต๋๋ค ~ ๊ทธ๋ฆฌ๊ณ ์ด ๋ฒ์ ์ฒ๋ฆฌ ํด์ฃผ๋ ๊ฒ์ ์ฌ๋๋ง๋ค ๋ค์ํ๋ค์ isRange ํจ์๋ก ์๋นผ์ฃผ๊ณ ๊ทธ๋ฅ continue ๋ฐ๋ก ํด์ฃผ๋๊น ๊ฐ๊ฒฐํด์ ๋ณด๊ธฐ ์ข์์ ๋ค์์ ํ ๋ฒ ์ฌ์ฉํด๋ณด๊ฒ ์ต๋๋ค.
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.
@bishoe01 ๋ง์์ ํ์ค ์ ๊ธฐ์ ์ ๋ ์ข ๊ณ ๋ฏผํ์ด์ ์ค๋ฅธ์ชฝ์ผ๋ก ๋์ง ์ผ์ชฝ์ผ๋ก ๋์ง ์๋๋ฉด ๋ญ ์ด๋ค์์ผ๋กํด์ผ ์ข ๋ ํจ์จ์ ์ผ๋ก ๊ณ์ฐํ ์ง, ์ฌ์ค ์ ์ฝ๋๋ ์ ๊ฐ ์ด๊ฒ์ด ์ฝ๋ฉํ ์คํธ๋ค ๋๋๋น ์ฑ ์์ dfs/bfs ์์ ์ ๋ฐ์์ผ๋ก ๋ง์ด์ฒดํฌ๋ฅผ ๋จผ์ ํ๊ณ ํ์ ์กฐ๊ฑด์ ๋ฃ๊ณ dfs ์คํํ๋ ์์๋ฅผ ๊ฐ์ ธ์ ๊ทธ ๊ตฌ์กฐ๋ฅผ ์ธ์ ์ด์ ๊ทธ๋ฐ๊ฑฐ๊ฐ์์. ์ข ๋ ์ข์ ๋ฐฉ์์ด ์์ผ๋ฉด ์๋ ค์ฃผ์๋ฉด ์ข์๊บผ๊ฐ์์ ํ์ค
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.
์ฒญ์๊ธฐ๋ฌธ์ ์ธ๋ฐ, ํ๋ฉด์ ๋ญ๊ฐ ์์น ๋ก ์ธ์์ ํด์ ์ฃผ์์ด ์์น ๋ก ๋์ด์์ต๋๋ค..! ์๋ฏธ๋ ๊ฐ์์ ~
์ฒ์์ ๊ทธ๋ฅ ํ ๋๋ฆฌ๋ง ์๋๊ฐ๋ฉด ๋๋์ง์๊ณ ํ์๋๋ฐ, 1๋ ์๋๋๋ผ๊ตฌ์..! ์ ๋ 1์ ์ด๋ฏธ ์์น ๋ ๊ณต๊ฐ์ผ๋ก ์ธ์์ ํ๊ณ ํ๋ค๊ฐ ์ข ์ค๋ ๊ฑธ๋ ธ์ต๋๋ค..
์ด๋ฒ๊น์ง๋ง ์ข struct ์ฐ๋ฉด์ ํ๋ก๊ทธ๋จ์ค๋ฝ๊ฒ ์ง๋ณด๋ ์ฐ์ต์ ํ๋๋ฐ, ์๊ฐ์ด ๋๋ฌด ์ค๋๊ฑธ๋ ค์ ๋ค์๋ถํฐ๋ ์ข ์๊ฐ์ง์ง ํ์ด๋จธ ์ฌ๋ฉด์ 1์๊ฐ ์์ ํธ์ ์๊ฒ๋ ์ปคํธ๋ผ์ธ์ ๋ฌ๋ณด๊ฒ ์ต๋๋ค.
๊ทผ๋ฐ ์๋ ์ด๋ฐ์์ผ๋ก ์ ์์ง๋ฒ๋ฆํด์ ์ข ํ์ต์ ํ ์ ์์๋ ๊ฒ ๊ฐ์์.. ์๋ฐ์ ๋ ์ด๋ ค์ด๋ฌธ์ ๊ฐ๋ํด๋ณด๋ฉด ์ข์๋ฏ?!
import Foundation
var Direction: [Int: (i: Int, j: Int)] = [0: (-1, 0), 1: (0, 1), 2: (1, 0), 3: (0, -1)]
struct Robot {
var direction: Int
var position: (i: Int, j: Int)
let N: Int
let M: Int
var count = 1
// ๊ธธ ์ ์๋๋ฌธ์ ์์๋ณธ ์ขํ ํ๋จ contains๋ก ํด์ฃผ๊ธฐ
func isRange(_ i: Int, _ j: Int) -> Bool {
return (0 ..< self.N).contains(i) && (0 ..< self.M).contains(j)
}
mutating func turnLeft() {
self.direction = (self.direction + 3) % 4
}
// 4๋ฐฉํฅ์ค์ ์์น ํ ๊ณณ ์๋์ ?!
func judgeAll(_ graph: [[Int]]) -> Bool {
for dir in 0 ..< 4 {
let next_move = Direction[dir]!
let next_pos = (i: self.position.i + next_move.i, j: self.position.j + next_move.j)
if self.isRange(next_pos.i, next_pos.j), graph[next_pos.i][next_pos.j] == 0 {
return true
}
}
return false
}
// ์์ผ๋ก๋ ๊ฐ ์ ์๋์ ? (์์น ๊ฐ๋ฅ? )
func judgeForward(_ graph: [[Int]]) -> Bool {
let next_move = Direction[self.direction]!
let next_pos = (i: self.position.i + next_move.i, j: self.position.j + next_move.j)
return self.isRange(next_pos.i, next_pos.j) && graph[next_pos.i][next_pos.j] == 0
}
// ๋ฐฉํฅ๋๋ก ์ด๋ํ์๋์ ํฌ์ง์
func forwardPosition() -> (i: Int, j: Int) {
let next_move = Direction[self.direction]!
let next_pos = (i: self.position.i + next_move.i, j: self.position.j + next_move.j)
return next_pos
}
// ํ์ง
func goBack() -> (i: Int, j: Int) {
// ๋ค๋๊ธฐ
let next_move = Direction[(self.direction + 2) % 4]!
let next_pos = (i: self.position.i + next_move.i, j: self.position.j + next_move.j)
return next_pos
}
}
func solution(_ N: Int, _ M: Int, _ robot: (i: Int, j: Int, d: Int), _ graph: [[Int]]) -> Int {
var graph = graph
var robot = Robot(direction: robot.d, position: (i: robot.i, j: robot.j), N: N, M: M)
graph[robot.position.i][robot.position.j] = 2
while true {
// 4๋ฐฉํฅ ์ค์ ์์น ํ ๊ณณ ์์ด์ !
if robot.judgeAll(graph) {
์ฃผ๋ณํ๋ฐฉ: for _ in 0 ..< 4 {
// ์์ผ๋๊น 2๋ฒ์ํฉ -> ์ผ๋จ ์ขํ์
robot.turnLeft()
// ์ขํ์ ํ๋๊น ์ด๊ฑฐ ์์น ๊ฐ๋ฅ ?
if robot.judgeForward(graph) {
let nextPosition = robot.forwardPosition()
robot.position = nextPosition
// 0์ด๋ 1์ ํผํด์ ๊ทธ๋ฅ 2๋ผ๊ณ ๋ฌ์
graph[nextPosition.i][nextPosition.j] = 2
robot.count += 1
break ์ฃผ๋ณํ๋ฐฉ
}
}
}
else {
let backPosition = robot.goBack()
// ๋ฒฝ์ ์๋ฟ์ผ๋ฉด์๋ ๊ฐ ์ ์๋ ๊ณณ์ธ์ง ํ๋จ
if robot.isRange(backPosition.i, backPosition.j) && graph[backPosition.i][backPosition.j] != 1 {
robot.position = backPosition
}
else {
break
}
}
}
return robot.count
}๋ก์ง์ ๋ฌธ์ ์ ๋์จ ์์๋๋ก ์ธ๊ฒ๊ฐ์์. ๋ฐฉํฅ๋ ์๊ณ ํ์ง๋ํ๊ณ ํ๋ค๋ณด๋ ์ข ๋ณต์กํด์ง๋ฏ..!
์ ์ฒด ๋ก์ง
-
4๋ฐฉํฅ ์ค์ ์์น ํ ๊ณณ ์๋์ง ํ๋จ
1 - 2. ๊ทธ๋ฌ๋ฉด ์ด์ ๋ถํฐ turnLeftํด์ฃผ๋ฉด์ ๋ ์์ ์์น ํ ๊ณณ ์๋์ง ํ๋จ
1 - 3. ์ฐพ์ผ๋ฉด count +1 -
4๋ฐฉํฅ์ค์ ์์น ํ ๊ฑฐ ์์
2 - 1. ๊ทธ๋ฌ๋ฉด ํ์งํด์ฃผ๋ฉด์ ๋ฒฝ์ธ์ง ํ๋จ
2 - 2. ๋ฒฝ์ด ์๋๋ฉด ํ์งํด์ฃผ๊ณ ๋ค์ 1๋ฒ์ผ๋ก ๋์๊ฐ๊ธฐ
2 - 3. ๋ฒฝ์ด๋ฉด ๋๋ด๊ธฐ
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(์ฒญ์ ์๋จ), 1(์ฒญ์๋จ), 2(๋ฒฝ)์ผ๋ก ์ ์ฅํ์
// ๋ฐฉํฅ ์ ํ์ dx, dy ๋ฐฐ์ด๋ก ๊ด๋ฆฌํ๋ฉด ์ฌ์ธ ๋ฏ -> ์ด๊ฑฐํ์ด ๋ ์ข์๋ณด์. - ๋ถ(0), ๋(1), ๋จ(2), ์(3)
// ๋ก๋ด ํ์ฌ ์์น/๋ฐฉํฅ ๋ฐ๋ก ๋ณ์ ๊ด๋ฆฌํ๊ณ , ๊ณ์ ๊ฐฑ์ ํด๊ฐ๋ฉด์ ์์ง์ ์ถ์
// 1. ํ์ฌ ์์น ์ฒญ์
// 2. ์ผ์ชฝ๋ถํฐ ๋๋ฉด์ ๊ฐ ๊ณณ ์๋ ํ์ธ (๋ฐ์๊ณ ํ์ )
// - ์ฒญ์ํ ๊ณณ ์์ผ๋ฉด ๊ทธ๋ฆฌ๋ก ์ด๋ํ๊ณ 1๋ฒ์ผ๋ก ๋์๊ฐ๊ธฐ
// - 4๋ฐฉํฅ ๋ชจ๋ ์ฒญ์๋๊ฑฐ๋ ๋ฒฝ์ด๋ฉด ํ์ง ์๋
// 3. ํ์ง๋ ๋ชป ํ๋ฉด ์์
์ข
๋ฃ, ์ฒญ์ํ ์นธ ์ ๋ฐํ
// ๋ก๋ด์ด ๋ฐ๋ผ๋ณด๋ ๋ฐฉํฅ ํ์ ์ (dir + 3) % 4
// ํ์ง์ ํ์ฌ ๋ฐฉํฅ์์ ์ ๋ฐ๋๋ก (dir + 2) % 4
import Foundation
typealias Position = (x: Int, y: Int)
enum Direction: Int {
case north = 0
case east = 1
case south = 2
case west = 3
var move: Position {
switch self {
case .north: return (-1, 0)
case .east: return (0, 1)
case .south: return (1, 0)
case .west: return (0, -1)
}
}
// ๋ฐ์๊ณ ๋ฐฉํฅ ํ์
func turnLeft() -> Direction {
return Direction(rawValue: (self.rawValue + 3) % 4)!
}
// ํ์ง ๋ฐฉํฅ (180๋ ํ์ )
func reverse() -> Direction {
return Direction(rawValue: (self.rawValue + 2) % 4)!
}
}
// input
let boardSize = readLine()!.split(separator: " ").map { Int($0)! }
let (N, M) = (boardSize[0], boardSize[1])
let robotInfo = readLine()!.split(separator: " ").map { Int($0)! }
var (r, c, d) = (robotInfo[0], robotInfo[1], robotInfo[2])
var map = Array(repeating: Array(repeating: 0, count: M), count: N)
for i in 0..<N {
let row = readLine()!.split(separator: " ").map { Int($0)! }
for j in 0..<M {
if row[j] == 1 {
map[i][j] = 2
}
}
}
func solution(position: Position, direction: Int, roomSize: (height: Int, width: Int)) -> Int {
// ๋ก๋ด์ ํ์ฌ ์ํ
var currentPosition = position
var currentDirection = Direction(rawValue: direction)!
var numberOfCleanedBlocks = 0
// ์ขํ๊ฐ ๋งต ๋ด์ ์๋์ง ํ์ธํ๋ ํจ์
func isInBounds(_ position: Position) -> Bool {
return position.x >= 0 && position.x < roomSize.height &&
position.y >= 0 && position.y < roomSize.width
}
// ์ฃผ์ด์ง ๋ฐฉํฅ์ผ๋ก ์ด๋ํ์ ๋์ ์ ์์น ๊ณ์ฐ
func positionAfterMove(from position: Position, direction: Direction) -> Position {
let move = direction.move
return (position.x + move.x, position.y + move.y)
}
while true {
// ํ์ฌ ์์น ์ฒญ์
if map[currentPosition.x][currentPosition.y] == 0 {
map[currentPosition.x][currentPosition.y] = 1
numberOfCleanedBlocks += 1
}
var foundCleanableBlock = false
for _ in 0..<4 {
// ์ผ์ชฝ์ผ๋ก ํ์ (๋ฐ์๊ณ 90๋)
currentDirection = currentDirection.turnLeft()
// ํ์ ํ ๋ฐ๋ผ๋ณด๋ ๋ฐฉํฅ์ ์์น ๊ณ์ฐ
let nextPosition = positionAfterMove(from: currentPosition, direction: currentDirection)
// ๋ฒ์ ๋ด์ ์๊ณ ์ฒญ์ํ ์ ์๋ ๋น ๊ณต๊ฐ์ธ ๊ฒฝ์ฐ
if isInBounds(nextPosition) && map[nextPosition.x][nextPosition.y] == 0 {
currentPosition = nextPosition
foundCleanableBlock = true
break
}
}
// ์ฒญ์ํ ๊ณต๊ฐ์ด ์๋ ๊ฒฝ์ฐ -> ํ์ง ๋๋ ์ข
๋ฃ
if !foundCleanableBlock {
// ํ์งํ ์์น
let backPosition = positionAfterMove(from: currentPosition, direction: currentDirection.reverse())
// ํ์งํ ์ ์๋์ง ํ์ธ
if isInBounds(backPosition) && map[backPosition.x][backPosition.y] != 2 {
currentPosition = backPosition
} else {
break
}
}
}
return numberOfCleanedBlocks
}
let res = solution(position: (r, c), direction: d, roomSize: (N, M))
print(res)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.
์ ๋ ฅ ์กฐ๊ฑด ์ฃผ์์ผ๋ก ์ ์ด๋์ผ์๋๊ฑฐ ๊ต์ฅํ ์ข์๋ณด์ ๋๋ค!! ์ ๋ ์ด๋ ๊ฒ ํด์ผ๊ฒ ์ด์^~^
+ ์ ๋ ฅ ๋ถ๋ถ ๊ฐ์ ธ๋ค์ผ์ด์ฉ!
|
์ ๋ ๋๋ฌด ์ค๋ ๊ฑธ๋ ค์ ๋ธ๋ก๊ทธ์์ ์ฌ๋ฌ๊ฐ ์ง์ง๊ธฐ ํ์๋๋ค .. //
// main.swift
// 14503
//
// Created by Seungeun Park on 5/19/25.
//
import Foundation
// ๋ฐฉํฅ์ ๋ํ๋ด๋ ์์: ๋ถ(0), ๋(1), ๋จ(2), ์(3)
// ๊ฐ๊ฐ์ ๋ฐฉํฅ์ ๋ํ ์ด๋ ์ขํ ๋ณํ๋ (dy, dx)
let dy = [-1, 0, 1, 0] // ๋ถ, ๋, ๋จ, ์
let dx = [0, 1, 0, -1]
let size = readLine()!.split(separator: " ").map { Int($0)! }
let N = size[0], M = size[1]
let robotInit = readLine()!.split(separator: " ").map { Int($0)! }
var r = robotInit[0], c = robotInit[1], d = robotInit[2] // ๋ก๋ด์ ์ด๊ธฐ ์์น์ ๋ฐฉํฅ
// ๋ฐฉ์ ์ํ ์ ์ฅ: 0์ ์ฒญ์ ๊ฐ๋ฅ, 1์ ๋ฒฝ
var map: [[Int]] = []
for _ in 0..<N {
map.append(readLine()!.split(separator: " ").map { Int($0)! })
}
// ๋ฐฉ๋ฌธ ์ฌ๋ถ ํ์ธ์ฉ ๋ฐฐ์ด (์ฒญ์ํ๋์ง ์ฌ๋ถ)
var cleaned = Array(repeating: Array(repeating: false, count: M), count: N)
var cleanedCount = 0 // ์ฒญ์ํ ์นธ ๊ฐ์
while true {
// ํ์ฌ ์์น๊ฐ ์ฒญ์๋์ง ์์์ผ๋ฉด ์ฒญ์
if !cleaned[r][c] {
cleaned[r][c] = true
cleanedCount += 1
}
var moved = false // 4๋ฐฉํฅ ์ค ์ด๋ํ๋์ง ์ฌ๋ถ
// ๋ฐ์๊ณ ๋ฐฉํฅ์ผ๋ก 4๋ฒ๊น์ง ํ์
for _ in 0..<4 {
// ์ผ์ชฝ ๋ฐฉํฅ์ผ๋ก ํ์
d = (d + 3) % 4 // ๋ถ(0) โ ์(3) โ ๋จ(2) โ ๋(1) ์์
let nr = r + dy[d]
let nc = c + dx[d]
// ์ด๋ํ ์นธ์ด ์ฒญ์ ์ ๋ ๋น ์นธ์ด๋ฉด ์ด๋
if map[nr][nc] == 0 && !cleaned[nr][nc] {
r = nr
c = nc
moved = true
break // ์ ์งํ์ผ๋ฏ๋ก ๋ค์ 1๋ฒ๋ถํฐ ์์
}
}
//๋ค ๋ฐฉํฅ ๋ชจ๋ ์ฒญ์ ๋ชปํ๋ฉด โ ํ์ง
if !moved {
let back = (d + 2) % 4 // ํ์ฌ ๋ฐฉํฅ์ ๋ฐ๋ ๋ฐฉํฅ
let br = r + dy[back]
let bc = c + dx[back]
// ๋ค๊ฐ ๋ฒฝ์ด๋ฉด ์ข
๋ฃ
if map[br][bc] == 1 {
break
} else {
// ํ์ง์ ๋ฐฉํฅ ๊ทธ๋๋ก ์ ์งํ๋ฉด์ ์์น๋ง ์ด๋
r = br
c = bc
}
}
}
print(cleanedCount) |
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.
ใ ใ ใ ใดใ ใดใ ใดใ ใ
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.
dใ ๋ฐ๋ฏธ์ ์ด๋ ๊ฒ ์ ๊ทผํ์ งใฑใด์ !
๐ ๋ฌธ์ ๋งํฌ
๋ก๋ด ์ฒญ์๊ธฐ
์ธ์ ๋ก๋ด ์ฒญ์๊ธฐ์ฝ๋๊น ์ง์ง... ํ..
โ๏ธ ์์๋ ์๊ฐ
40๋ถ + @
โจ ์๋ ์ฝ๋
๋ฌธ์ ์ ๋ง ์ฅ๋์ ์ดํดํ๋๋ฐ ๊ฝค๋ ์๊ฐ์ด ๊ฑธ๋ ธ์ต๋๋ค.
๋ฌผ๋ก ๋ฌธ์ ๋ฅผ ๋ฑ ๋ง์ฃผํ์๋ง์ ์ด๊ฑด DFS/BFS๋ฉด ์์ฝ๊ฒ ํ๋ฆฌ๊ฒ ๊ตฌ๋! ๋ผ๊ณ ์๊ฐํ์ค๊ฒ๋๋ค.
์ ๋ DFS๋ฅผ ์ ํํ๋๋ฐ์ ! ๊ทธ ์ด์ ๋ ! ๋ฌธ์ ๊ฐ ๋๋๊ณ Depth๊ฐ ์์ด์. ํด๋น ์กฐ๊ฑด๋ค์ ๋์ดํ๊ณ ์กฐ๊ฑด์ด ์๋ ์์ ๋ค์ ๋์ค๊ณ . ์ด๊ฒ ์ฌ๊ท๋ก ํ๋ฉด ์ข ๋ ์ง๊ด์ ์ด์ง ์์๊น? ํด์ ์ ํํ์ต๋๋ค.
์ค์ค ์ ์ด๋ณธ ๊ฒ์ธ๋ฐ์.
๋จผ์ ๋ก๋ด ์์ ใ ์ง์ง ๊ฐ๋จํฉ๋๋ค. ์ ํฌ ์ผ๋ฐ ๋ก๋ด ์ฒญ์๊ธฐ ๋ก์ง ์๊ฐํ๋ฉด๋ผ์.
์์ผ๋ก ๊ฐ๋ค๊ฐ, ์ฅ์ ๋ฌผ์ด ์์ผ๋ฉด ๋ฐฉํฅ์ ๋๊ณ , ์ฒญ์ํ ๋ฐ ์์ผ๋ฉด ๋ค๋ก ๊ฐ๊ณ ,
๋ฑ ๊ทธ ์์ค์ ๋๋ค. ๋ณต์กํ ๊ฒ ์์ด์.
๊ทผ๋ฐ ๋ฌธ์ ๋โฆ ๊ทธ๊ฑธ ๋ง๋ก ์ค๋ช ํ๋๊น ์์ฒญ ์ด๋ ต๊ฒ ๋๊ปด์ง๋ค๋ ๊ฑฐ์ฃ .
(์ผ์ชฝ์ผ๋ก ๋๋ค โ ๊ทธ ๋ฐฉํฅ์ ์ฒญ์ํ ๊ณต๊ฐ์ด ์์ผ๋ฉด ์ ์ง โ ๊ทธ๊ฒ ์๋ค๋ฉด ๋ค์ ์ผ์ชฝโฆ ์ด๊ฑธ ๋ค ๋ฒ ๋ฐ๋ณตโฆ ์ดํด ๐ตโ๐ซ)
๋ง๋ก๋ ๊ดด๋ํ์ง๋ง, ๋ก์ง ์์ฒด๋ ์ฐ๋ฆฌ๊ฐ ์ง์์ ๋ณด๋ ๊ทธ ๋ก๋ด ์ฒญ์๊ธฐ ๊ทธ๋๋ก์๊ณ , ๊ทธ๋์ ์ ๋ DFS๋ก ํ ์นธ ํ ์นธ ๋ฐ๋ผ๊ฐ๋ฉฐ ์ฌ๊ท๋ก ๊ตฌํํ์ต๋๋ค.
๋ฌธ์ ์์์์๋ ๋ท์ชฝ์ ๋์์ง๋ง, ๋ฌธ์ ์กฐ๊ฑด๋ค์ ๊ฑฐ๊พธ๋ก ๋ณธ๋ค๋ฉด ๋ถ๋๋จ์ ๋ฐฉํฅ์ ์ฒญ์ ์ํ ๊ณณ์ ์ผ์ชฝ๋ถํฐ ์ฒดํฌํ๋ฉด ๋ฉ๋๋ค.
๊ทธ๋์ ์ ๋ +3์ ํด์ฃผ์๊ณ % 4 -> ์ด ๋ก์ง์ ์๋ฎฌ๋ ์ด์ , ๊ตฌํ์์ ์ํ์ข์ฐ ๋น๊ต์ ๊ผญ ๋์ค๋ ์ธ์ฐ์๋ฉด ์ข์ต๋๋ค.
๊ทธ ๋ค์, dfs๋ ๊ฑฐ์ ๊ตฌ์กฐ์ ์ผ๋ก ๋ฌ๋ผ์ง ๊ฒ์ ์์์ด์.
ํ์ฌ ์์น ์ฒญ์
์ผ์ชฝ ๋ฐฉํฅ๋ถํฐ 4๋ฐฉํฅ ํ์
โข ์ฒญ์ ์ ๋ ์นธ ์์ผ๋ฉด โ ํ์ โ ์ ์ง โ ์ฌ๊ท ํธ์ถ
โข ์์ผ๋ฉด โ 4๋ฐฉ ๋ชจ๋ ๋์๋ค๋ ์๋ฏธ โ ํ์ง
ํ์ง ์ฒ๋ฆฌ
โข ๋ค์ชฝ์ด ๋ฒฝ์ด๋ผ๋ฉด ์ข ๋ฃ (exit(0) ์ฌ์ฉ)
โข ์๋๋ผ๋ฉด ๊ฐ์ ๋ฐฉํฅ์ผ๋ก ๋ค๋ก ํ ์นธ ์ด๋ํด ๋ค์ dfs
Tip. ์ฌ๊ธฐ์ ํต์ฌ์ ์ ๊ฐ ๊ฝค๋ ๊ณ ์ํ๊ฑด๋ฐ,,, ๊ฐ๋ก ์ธ๋ก ๊ฐ๋ ์ด ์ขํ๊ณ์์ ๋ฐ๋๊ฐ ๋๋ค๋ ๊ฒ์ ๋๋ค.
x ๋ ๊ฐ๋ก, y๊ฐ ์ธ๋ก์์์.
column์ด ๊ฐ๋ก๋ก ์ปค์ ธ์ผํ๊ณ , row๊ฐ ์ธ๋ก๋ก ์ปค์ ธ์ผํด์.
๊ทธ๋์ board[y][x]๋ก [r][c] ์์ผ๋ก ํด์ค์ผํฉ๋๋ค.
๋ํ =2 ์ฒ๋ผ ๊ผญ 2๊ฐ ์๋๋๋ผ๋ ๋ด๊ฐ ์ฒญ์ํ๋ค๋ผ๋ ๊ฒฝ๊ณ๊ฐ ์ ์ ๋ ์ค์ํ ๋ฏํฉ๋๋ค.
์๋์ ๊ฐ์ด ์ผ๊ด์ฑ ์๊ฒ ์ฒ๋ฆฌํด์ค์ผํฉ๋๋ค.
0 -> ์ฒญ์ํ์ง์์์นธ
1 -> ๋ฒฝ
2 -> ์ฒญ์ํ์นธ
๋ฌธ์ ์์์ ์กฐ๊ฑด ์ธ๋ฑ์ฑ์ ์์ง๋ง์์ผํฉ๋๋ค.
์ฒซ ์ขํ๋ r+1, c+1๋ฅผ ๊ฐ๋ฆฌํจ๋ค.
๊ฐ์ฅ ๋ถ์์ชฝ์ 0,0, ๊ฐ์ฅ ๋จ๋์ชฝ์ N-1,M-1
๊ทธ๋ฅ ์ธ๋ฑ์ค์ ๋๋ค. ์ ๋ ฅ์ ์ ๋ณด๊ณ ํท๊ฐ๋ฆฌ์ง ๋ง์์ผํฉ๋๋ค !!
๐ ์๋กญ๊ฒ ์๊ฒ๋ ๋ด์ฉ
์ขํ๊ณ์ ๋ฐฐ์ด ์ธ๋ฑ์ค๋ ๋ค๋ฅด๋ค
โข ๋ฌธ์ ์์ (r, c)๋ผ๊ณ ํํํ๊ณ , (0, 0)์ด ๊ฐ์ฅ ๋ถ์์ชฝ์ด๋ผ๊ณ ๋งํ์ง๋ง, ์ค์ ๋ก๋ ๊ทธ๋ฅ 2์ฐจ์ ๋ฐฐ์ด์ ์ธ๋ฑ์ค์ผ ๋ฟ์ด๋ค.
โข x๋ ๊ฐ๋ก(โ), y๋ ์ธ๋ก(โ)์ ์๋ฏธ๋ฅผ ๊ฐ์ง๋ง, ๋ฐฐ์ด์์๋ board[y][x] ์์ผ๋ก ์ ๊ทผํด์ผ ํ๋ฏ๋ก ํญ์ ์ฃผ์๊ฐ ํ์ํ๋ค.
๋ฐฉํฅ ํ์ ์ +3 % 4๋ก ์ฒ๋ฆฌํ๋ฉด ๊น๋ํ๋ค
โข ๋ถ(0), ๋(1), ๋จ(2), ์(3) ๊ธฐ์ค์ผ๋ก ์ผ์ชฝ ํ์ ์ (dir + 3) % 4๋ก ํํํ๋ฉด ๋๋ค.
โข ๊ตฌํ/์๋ฎฌ๋ ์ด์ ๋ฌธ์ ์์ ๋ฐฉํฅ ํ์ ์ ์์ฃผ ๋ฑ์ฅํ๋ฏ๋ก ๋ฐ๋์ ์ธ์๋๋ฉด ์ข๋ค.
์ฒญ์ ์ฌ๋ถ๋ ๊ฐ์ผ๋ก ๊ตฌ๋ถํ๋ฉด ๋๋ค
โข board[y][x] = 2์ฒ๋ผ, ์ฒญ์ํ ์นธ์ ์ซ์ ๊ฐ์ผ๋ก ํํํด๋ ๋๋ค. ์ค์ํ ๊ฑด ์ผ๊ด์ฑ ์๊ฒ ์ฒ๋ฆฌํ๋ ๊ฒ.
โข ๊ผญ 2๊ฐ ์๋์ด๋ ์๊ด์์ง๋ง, โ์ฒญ์ํ ์นธโ์ด๋ผ๋ ์ํ ํํ์ด ์ฝ๋ ๋ด์ ๋ช ํํ ๋๋ฌ๋์ผ ํ๋ค.
์ ๋ ฅ๋ 0-based ์ธ๋ฑ์ค๋ค
โข ๋ฌธ์ ์ค๋ช ์ด ์ขํ์ฒ๋ผ ๋๊ปด์ง ์ ์์ง๋ง, ์ ๋ ฅ๊ฐ ์์ฒด๋ 0๋ถํฐ ์์ํ๋ฏ๋ก -1 ๊ฐ์ ๋ณด์ ์ ํ์ ์๋ค.
โข (3, 4)๋ ๊ณง board[3][4]๋ฅผ ์๋ฏธํ๋ค.