-
Notifications
You must be signed in to change notification settings - Fork 0
2-YooGyeongMo #9
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
Conversation
feat: ํ์ดํ ์ฎ๊ธฐ๊ธฐ 1 ๋ฌธ์ ํ์ด ์ถ๊ฐ - python ์ธ์ด ์ฌ์ฉ ์ด๋ฒ์๋ง.
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๋ก ์ ๊ฐ ํ๊ณ ์๊ฐ ํฐ์ง ์ฝ๋์ ๋๋ค ใ ใ file changed๊ฐ์๋ฉด dp ์ฝ๋์์ด์ฌ.... ใ
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.
์ ์ ์ด์ ์ด๊ฒ DP๋ก ํ๋ฆด๊ฑฐ๋ผ๋ ์๊ฐ์ ์ ํ ๋ชปํด์, ์ผ๋จ ๊ฒฝ๋ก์ฐพ๊ธฐ๋ก ํ๋ค๊ฐ ๊ฐ์ด๋์ค๊ธฐ๋ฅผ ๋ฐ๋ฌ๋๋ฐ, ์์ ๋ ๋ค ๋ง๋๋ฐ ์ด๊ฒ ์๊ฐ์ด๊ณผ ์ด์ ์ ํ๋ ธ์ต๋๋ค๊ฐ ๋จ๋๋ผ๊ตฌ์..
์๊ฐ์ด๊ณผ๋ฅผ ๋ชฉํ๋ก ๊ณ์ ์กฐ์ ํด์ฃผ๋ค๊ฐ ์คํจํ๊ณ ๋ณด๋ด์คฌ์ต๋๋ค..
import Foundation
enum Direction {
case down
case right
case diagonal
}
func solution(_ N: Int, _ graph: [[Int]]) {
var map: [[Int]] = Array(repeating: Array(repeating: 0, count: N + 1), count: N + 1)
var queue: [(d: Direction, x: Int, y: Int)] = [(.right, 1, 2)]
var pointer = 0
while queue.count > pointer {
let (d, x, y) = queue[pointer]
map[x][y] += 1
// var nextMoveArr: [(dd: Direction, dx: Int, dy: Int)] = [(.diagonal, 1, 1)] //๋๊ฐ์ ๊ฐ๋ ค๋ฉด ์ฌ๋ฐฉ์ด ๋ค 0์ด์ด์ผํ๋๋ค ..
var nextMoveArr: [(dd: Direction, dx: Int, dy: Int)] = []
if x < N, y < N,
graph[x - 1][y] == 0,
graph[x][y - 1] == 0,
graph[x][y] == 0
{
nextMoveArr.append((.diagonal, 1, 1))
}
switch d {
case .right:
if y < N, graph[x - 1][y] == 0 {
nextMoveArr.append((.right, 0, 1))
}
case .down:
if x < N, graph[x][y - 1] == 0 {
nextMoveArr.append((.down, 1, 0))
}
case .diagonal:
if y < N, graph[x - 1][y] == 0 {
nextMoveArr.append((.right, 0, 1))
}
if x < N, graph[x][y - 1] == 0 {
nextMoveArr.append((.down, 1, 0))
}
}
// print(x, y, d, nextMoveArr)
for item in nextMoveArr {
let (nd, nx, ny) = (item.dd, x + item.dx, y + item.dy)
if isRange(nx, ny) {
queue.append((d: nd, x: nx, y: ny))
}
}
pointer += 1
}
print(map[N][N])
// print(map)
func isRange(_ x: Int, _ y: Int) -> Bool {
if x < 1 || x > N || y < 1 || y > N || graph[x - 1][y - 1] == 1 {
return false
}
return true
}
}|
@bishoe01 ์ค dp์ฒ๋ผ ์ ๊ทผ์ ์์ฒญ ์ํ์
จ๋๋ฐ์ ??? ์ ๋ dp๋ ๋ฌด์๊ฒ ํ๋ค์ด์ ํํผ์ค์ธ๋ฐ ์ด๊ฒ ๊ฑธ๋ฆด ์ค์ด์ผ... |
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.
์๋ ์ ๋ ๋ณด๋๋ฌธ์ ๊ธธ๋ ์ด!? BFS, DFS ์ด๋ ๊ฒ ์๊ฐํ๊ณ ํ์๋๋ฐ
์๊ฐ์ด๊ณผ์๋ค์,,, ๋ณด๋๋ผ๊ณ ๋ฌด์กฐ๊ฑด BFS๋ผ๋ ์ต๊ด์ ๋ฒ๋ ค์ผํ ํ
๋ฐ..
๋ฐ๋ก ์๊ฐ์ ๋๋ฌด ๋ง์ด์จ์ ๊ฒฐ๊ตญ ์ธํฐ๋ท๊ณผ ํ, ๋ฐ๋ฏธ์์ ์ฝ๋๋ฅผ ์ฐธ๊ณ ํด๋ฒ๋ ธ๋ค์ ใ
- ์ธํฐ๋ท์์ ์ฐธ๊ณ ํ๋๋ฐ ์ฝ๋๊ฐ ๊น๋ํ ๊ฒ ๊ฐ์์ ์ฒจ๋ถํด์!
import Foundation
struct Queue<T> {
var enq = [T]()
var pointer = -1
var counter = 0
var isEmpty: Bool {
counter == 0
}
mutating func append(_ element: T) {
enq.append(element)
counter += 1
}
mutating func popFirst() -> T {
pointer += 1
counter -= 1
return enq[pointer]
}
}
enum Direction {
case horizontal
case vertical
case diagonal
}
let N = Int(readLine()!)!
var arr = [[Int]]()
var q = Queue<(Direction, Int, Int)>()
var answer = 0
for _ in 0..<N {
arr.append(readLine()!.split(separator: " ").map{Int($0)!})
}
func myfunc(_ dir: Direction, _ x: Int, _ y: Int){
if x == N-1 && y == N-1 {
answer += 1
return
}
var hubo = [(Direction, Int, Int)]()
switch dir {
case .horizontal:
hubo = [(Direction.horizontal, x, y+1), (Direction.diagonal, x+1, y+1)]
case .vertical:
hubo = [(Direction.vertical, x+1, y), (Direction.diagonal, x+1, y+1)]
case .diagonal:
hubo = [(Direction.horizontal, x, y+1), (Direction.vertical, x+1, y), (Direction.diagonal, x+1, y+1)]
}
for (dire, X, Y) in hubo {
if X >= N || Y >= N {
continue
}
switch dire {
case .horizontal, .vertical:
if !(arr[X][Y] == 0) {
continue
}
case .diagonal:
if !(arr[X][Y] == 0 && arr[X-1][Y] == 0 && arr[X][Y-1] == 0) {
continue
}
}
myfunc(dire, X, Y)
}
}
myfunc(Direction.horizontal, 0, 1)
print(answer)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์ ์กฐํ,
switch dir {
case .horizontal:
hubo = [(Direction.horizontal, x, y+1), (Direction.diagonal, x+1, y+1)]
case .vertical:
hubo = [(Direction.vertical, x+1, y), (Direction.diagonal, x+1, y+1)]
case .diagonal:
hubo = [(Direction.horizontal, x, y+1), (Direction.vertical, x+1, y), (Direction.diagonal, x+1, y+1)]
}
``` ํด๋น ๋ฐฉ์ ๋๋ฌด ์ข์๊ฑฐ๊ฐ์์
๋๊ฐ์ ์ ์ฅ ๋ฐฉ์์ DP์์ ๋๊ฐ์ ์ ๋ด์๋ ์ด๋ค ๋ฐฉ์์ด ์์์ง ๊ณ ๋ฏผ์ ๋ชปํ์ด์ ์ฝ๋๋ฅผ ์ฐธ๊ณ ํ์๋ค์ ใ
ใ
MuchanKim
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๋ก ํ์์ต๋๋ค.
๋
ผ๋ฆฌ์ ์ธ ๊ธฐ๋ฅ? ๋จ์๋ก ๋๋ ์ ํ์ด๋ดค๋๋ฐ ๋๋ฆ ์ข์ ๋ฐฉ๋ฒ์ด๋ ์๊ฐ๋ ๋๋ค์. ๊ธฐ์กด์ ํ์ด๋๊ณ ๋ถ๋ฆฌ!
+ ๊ธธ์ด typealias ํ์ฉํ๋ ๊ฑฐ ๋ณด๊ณ ๊ฐ๋
์ฑ ์ข์์ง๋ ๊ฒ ๊ฐ์์ ์ ๋ ํจ ์จ๋ด์
๊ธ๊ตฌ ์ ๋ฌธ์ ๋ฅผ ํ ๋ ํ์ดํ๋ฅผ tail๊ณผ head ๋ ์ ์ผ๋ก ์ง๊ด์ ์ผ๋ก ํํํ๋๋ฐ, ๋ฐ๋ฏธ์์ head ์์น์ ๋ฐฉํฅ state๋ง์ผ๋ก ํ์ดํ๋ฅผ ํํํ์ จ๊ตฐ์.. ๊ณจ๋ ๋ฌธ์ ๋งค์ฐ ์ด๋ ต์ต๋๋ค. ํ์ด์๊ฐ: 2์๊ฐ+@
import Foundation
typealias Board = [[Int]]
typealias Position = (tail: (Int, Int), head: (Int, Int))
let boardSize: Int = Int(readLine()!)!
func getBoard(_ size: Int) -> Board {
var newBoard: Board = []
for _ in 0..<size {
let row = readLine()!.split(separator: " ").map { Int($0)! }
newBoard.append(row)
}
return newBoard
}
// ํ์ดํ์ ํ์ฌ ๋ฐฉํฅ ํ์ธ
func getDirection(_ position: Position) -> Int {
let (tailRow, tailCol) = position.tail
let (headRow, headCol) = position.head
// ๊ฐ๋ก ๋ฐฉํฅ (0)
if tailRow == headRow && tailCol + 1 == headCol {
return 0
}
// ์ธ๋ก ๋ฐฉํฅ (1)
else if tailRow + 1 == headRow && tailCol == headCol {
return 1
}
// ๋๊ฐ์ ๋ฐฉํฅ (2)
else {
return 2
}
}
// ์ด๋ ๊ฐ๋ฅํ์ง ํ์ธ
func canMove(_ position: Position, _ direction: Int, _ board: Board, _ N: Int) -> Bool {
let (headRow, headCol) = position.head
// ๊ฐ๋ก ์ด๋
if direction == 0 {
let newCol = headCol + 1
return newCol < N && board[headRow][newCol] == 0
}
// ์ธ๋ก ์ด๋
else if direction == 1 {
let newRow = headRow + 1
return newRow < N && board[newRow][headCol] == 0
}
// ๋๊ฐ์ ์ด๋
else {
let newRow = headRow + 1
let newCol = headCol + 1
return newRow < N && newCol < N &&
board[headRow][newCol] == 0 &&
board[newRow][headCol] == 0 &&
board[newRow][newCol] == 0
}
}
// ํ์ดํ ์ด๋
func movePipe(_ position: Position, _ direction: Int) -> Position {
let (headRow, headCol) = position.head
let tail = position.head
var head: (Int, Int) = (0, 0)
// ๊ฐ๋ก ์ด๋
if direction == 0 {
head = (headRow, headCol + 1)
}
// ์ธ๋ก ์ด๋
else if direction == 1 {
head = (headRow + 1, headCol)
}
// ๋๊ฐ์ ์ด๋
else {
head = (headRow + 1, headCol + 1)
}
return (tail, head)
}
func solution(_ N: Int, _ board: Board) -> Int {
// ์์ ํ์ดํ ์์น - ์ฒ์์๋ (0,0)์ (0,1) ์์น์ ํ์ดํ๊ฐ ๋์ฌ ์์!
let startPosition: Position = ((0, 0), (0, 1))
var count = 0
func dfs(_ position: Position) {
// ํ์ดํ ๋์ด (N-1, N-1)์ ๋๋ฌํ๋ฉด ์นด์ดํธ ์ฆ๊ฐ
let (headRow, headCol) = position.head
if headRow == N-1 && headCol == N-1 {
count += 1
return
}
let currentDirection = getDirection(position)
// ํ์ฌ ๋ฐฉํฅ์ ๋ฐ๋ผ ๊ฐ๋ฅํ ์ด๋ ๋ฐฉํฅ ํ์ธ
if currentDirection == 0 { // ๊ฐ๋ก
// ๊ฐ๋ก๋ก ์ด๋
if canMove(position, 0, board, N) {
dfs(movePipe(position, 0))
}
// ๋๊ฐ์ ์ผ๋ก ์ด๋
if canMove(position, 2, board, N) {
dfs(movePipe(position, 2))
}
}
else if currentDirection == 1 { // ์ธ๋ก
// ์ธ๋ก๋ก ์ด๋
if canMove(position, 1, board, N) {
dfs(movePipe(position, 1))
}
// ๋๊ฐ์ ์ผ๋ก ์ด๋
if canMove(position, 2, board, N) {
dfs(movePipe(position, 2))
}
}
else { // ๋๊ฐ์
// ๊ฐ๋ก๋ก ์ด๋
if canMove(position, 0, board, N) {
dfs(movePipe(position, 0))
}
// ์ธ๋ก๋ก ์ด๋
if canMove(position, 1, board, N) {
dfs(movePipe(position, 1))
}
// ๋๊ฐ์ ์ผ๋ก ์ด๋
if canMove(position, 2, board, N) {
dfs(movePipe(position, 2))
}
}
}
dfs(startPosition)
return count
}
let board = getBoard(boardSize)
let result = solution(boardSize, board)
print(result)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ํ์๋ ์ ๋ ํฐ์ ธ์ dp์ธ๊ฑธ์์๋๋ฐ..ใ ใ ใ
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.
๋ฌด ์ฝ๋ ์ ๋ดค์ต๋๋ค !
struct Pipe {
var tail: (Int, Int)
var head: (Int, Int)
var direction: Int {
if tail.0 == head.0 && tail.1 + 1 == head.1 {
return 0
} else if tail.0 + 1 == head.0 && tail.1 == head.1 {
return 1
} else {
return 2
}
}
func moved(to newDirection: Int) -> Pipe {
let (r, c) = head
let newHead: (Int, Int)
switch newDirection {
case 0: newHead = (r, c + 1)
case 1: newHead = (r + 1, c)
default: newHead = (r + 1, c + 1)
}
return Pipe(tail: head, head: newHead)
}
}
``` ์ ์ฝ๋์ฒ๋ผ ํ์ดํ๋ฅผ ๊ตฌ์กฐ์ฒด๋ก ๋์๋๊ฑด ์ด๋จ๊น์?
์ดํ
```swift
let currentDirection = pipe.direction ์ด๋ฐ์์ผ๋ก ์ฌ์ฉํ๋ฉด ๊ฐ๋ ์ฑ๋ ๋์์ง๊บผ๊ฐ์ต๋๋ค !
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 Direction: Int {
case horizontal = 0, vertical = 1, diagonal = 2
}
``` ์ด๋ฐ์์ผ๋ก๋ ๊ฐ๋ฅํ ๊ฑฐ๊ฐ์์ !
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.
C2์ด์๋ก ์์ง ์ ๋ง์ ์ ๋ต์ฝ๋๋ ์ ์ถํ์ง ๋ชปํ์ต๋๋ค ใ
ใ
+
ํ์ด์ฌ ์ฝ๋ ์ง์ง ๋ณด๊ธฐ ์ข๋ค์ ใ
ใ
ใ
dfs๋ ๊ฒฐํฉ๋ ํ๋ค์ด dp?๊ฐ์์.
๋๊ฐ์ ์ํํ๋ฉด์ ์ธ์ ํ ์ธ ์นธ์ด ๋ค empty ์กฐ๊ฑด ์ฒ๋ฆฌ๋ ์ผ๋ฌด์ง๋ค์!!!
๐ ๋ฌธ์ ๋งํฌ
ํ์ดํ ์ฎ๊ธฐ๊ธฐ 1
โ๏ธ ์์๋ ์๊ฐ
dfs๋ก ํ์๋ค๊ฐ ์๊ฐ ์ด๊ณผ ๋ ์....


dp๋ก ์๊ฐํด๋ผ ์ ์์ด์ ํ์ด๋ฅผ ๋ณธ 1์ธ...
์ด๊ฑด ์ผ์ฑ ๊ธฐ์ถ ์ถ์๋ ๋ชป๋๋ค๋ .... ใ ใ
โจ ์๋ ์ฝ๋
๋ฌธ์ ๋ฅผ ๋จผ์ ์ฝ์ด๋ณด์๋ฉด-> ๊ฐ๋ก, ์ธ๋ก, ๋๊ฐ์ ํ์ดํ๋ฅผ ์ธ์ ์ด๋ค ํ์ด๋ฐ์ ํด๋น ํ์ดํ๋ค์ ๋์ ์ ์๋์ง ์์๋ณผ ํ์๊ฐ ์์ต๋๋ค.
์ ์กฐ๊ฑด๋ค์ ์ฒดํฌํ์ จ๋ค๋ฉด, ๋ฒฝ์ ๋ํ ์กฐ๊ฑด๋ ๊ณ ๋ คํด์ฃผ์ ์ผํฉ๋๋ค.
์ ๋ DP๋ฅผ ๊ณ ๋ คํ ๋ 2์ฐจ์ ๋ฐฐ์ด์ ๊ฐ๋กํ์ดํ, ๋๊ฐ์ ํ์ดํ, ์ธ๋กํ์ดํ๊ฐ ์ด์ ์ ์ถ๊ฐ๋ ๊ฒ๋ค์ ๋์ ์ํค๋ฉด์ ์ฆ๊ฐ ์์ผ์ ์ ์ฅํ๋ ๋ฐฉ์์ ์ ํํ๋๋ฐ์.
๋จผ์ ๊ทธ๋ฆผ์ ๋ณด์๋ฉด, 2์ฐจ์ ๋ฐฐ์ด์ ํ ์ขํ์ ์ด๋ฌํ dp๊ฐ ์๋ค๊ณ ๋ณด์๋ฉด๋๋๋ฐ์.

์๋ ์ฌ์ง๊ณผ ๊ฐ์ ํ์์ผ๋ก ์ด๊ธฐํ๊ฐ ๋์ ๋ค๊ณ ๋ณด๋ฉด๋ฉ๋๋ค.

์ด๋ ๊ฒ ๋์์๋ 3์ฐจ์ ๋ฐฐ์ด์ ํ์ฉํด์ [ํ์ดํ ์ข ๋ฅ][ํ][์ด] ์ด๋ฐ์์ผ๋ก ์ฒดํฌํ๋ฉด์ ์ด์ ์ ์์ธ ๊ฐ๋ค์ ๋์ ํ๊ณ ์ต์ข ์ ์ธ [N - 1][N - 1] ์ ๋๋ฌํ๋ ํ์ดํ ์ข ๋ฅ๋ค์ ๋ฐฉ๋ฒ์๋ฅผ ์ถ๋ ฅํ๋ฉด ๋๋ ๋ฌธ์ ์ ๋๋ค !
๐ ์๋กญ๊ฒ ์๊ฒ๋ ๋ด์ฉ
์ ๋ ์ด ๋ฌธ์ ๋ฅผ ์ ํ์ ๋ dfs๊ฐ ์๊ฐ๋์ dfs๋ก ์ ๊ทผํ์๋๋, ๋๋ฌด ๋ง์ ์กฐ๊ฑด์๋ฅผ ์ฒดํฌํ ๊ฒฐ๊ณผ ... ํฐ์ก์ต๋๋ค.. ์...
์๋ ์ฝ๋๋ ์ ๊ฐ ์๊ฐ์ด๊ณผ๋ก ํฐ์ง ์ฒซ ์์ด๋์ด์์ ๋์จ dfs์ฝ๋์ ๋๋ค.
ใ ใ
๊ทธ๋์ ์ ๋ dp๋ก ํผ ๋ฌธ์ ๋ค์ ๊ฒ์ํ์๊ณ ์๋์ ๋ธ๋ก๊ทธ๋ฅผ ์ฐธ๊ณ ํ์ฌ ์ดํดํ๊ณ ํ์์ต๋๋ค !
๋ธ๋ก๊ทธ ๋งํฌ