๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ“– Problem Solution/Programmers

[Swift] ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์œ„ํด๋ฆฌ ์ฑŒ๋ฆฐ์ง€ 3์ฃผ์ฐจ ํผ์ฆ ์กฐ๊ฐ ์ฑ„์šฐ๊ธฐ

by Fomagran ๐Ÿ’ป 2021. 8. 20.
728x90
๋ฐ˜์‘ํ˜•

 

Problem

 

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - 3์ฃผ์ฐจ

[[1,1,0,0,1,0],[0,0,1,0,1,0],[0,1,1,0,0,1],[1,1,0,1,1,1],[1,0,0,0,1,0],[0,1,1,1,0,0]] [[1,0,0,1,1,0],[1,0,1,0,1,0],[0,1,1,0,1,1],[0,0,1,0,0,0],[1,1,0,1,1,0],[0,1,0,0,0,0]] 14 [[0,0,0],[1,1,0],[1,1,1]] [[1,1,1],[1,0,0],[0,0,0]] 0

programmers.co.kr


Solution

 

1. ๊ฒŒ์ž„๋ณด๋“œ์™€ ํ…Œ์ด๋ธ”์˜ ํผ์ฆ ์กฐ๊ฐ๋“ค์„ ์ฐพ๋Š”๋‹ค.

 

๊ฒŒ์ž„๋ณด๋“œ์™€ ํ…Œ์ด๋ธ”์˜ ํผ์ฆ ์กฐ๊ฐ๋“ค์„ ์ฐพ๊ธฐ ์œ„ํ•ด์„  BFS ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•ด์•ผ ๋ฉ๋‹ˆ๋‹ค.

 

๊ฒŒ์ž„๋ณด๋“œ๋ฅผ ํƒ์ƒ‰ํ•  ๋• ๋งŒ์•ฝ 0์„ ๋งŒ๋‚˜๊ฒŒ ๋˜์—ˆ๋‹ค๋ฉด ๊ทธ ์ฃผ๋ณ€ ์™ผ์ชฝ,์˜ค๋ฅธ์ชฝ,์œ„,์•„๋ž˜๊ฐ€ 0์ธ์ง€๋ฅผ ํŒŒ์•…ํ•ฉ๋‹ˆ๋‹ค.

 

๋งŒ์•ฝ 0์ด๋ผ๋ฉด ํ์— ํ•ด๋‹น ์œ„์น˜๋ฅผ ๋„ฃ๊ณ  ๋ฐ˜๋ณตํ•ด์ค˜์„œ ํผ์ฆ์˜ ์กฐ๊ฐ์„ ์™„์„ฑํ•ฉ๋‹ˆ๋‹ค.

 

func makePuzzle(x:Int,y:Int,isEmpty:Bool) {
    var queue:[(Int,Int)] = [(x,y)]
    while !queue.isEmpty {
        let first = queue.removeFirst()
        let new = [(first.0-1,first.1),(first.0+1,first.1),(first.0,first.1-1),(first.0,first.1+1)]
        
        for d in new {
            if 0..<boardCopy.count ~= d.0 && 0..<boardCopy.count ~= d.1 {
                if boardCopy[d.1][d.0] == 0 {
                    boardCopy[d.1][d.0] = 1
                    if isEmpty {
                        emptyBlocks[emptyBlocks.count-1].append(d)
                    }else {
                        blocks[blocks.count-1].append(d)
                    }
                    queue.append(d)
                }
            }
        }
    }
}

 

2. ํ…Œ์ด๋ธ”์˜ ํผ์ฆ ์กฐ๊ฐ์„ ํšŒ์ „ํ•ด์ค€๋‹ค.

 

๊ฒŒ์ž„๋ณด๋“œ์™€ ํ…Œ์ด๋ธ”์˜ ํผ์ฆ ์กฐ๊ฐ์ด ๊ฐ™์€์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด์„  ํ…Œ์ด๋ธ”์˜ ๊ฐ 0,90,180,270๋„๋กœ ํšŒ์ „๋œ ํผ์ฆ ์กฐ๊ฐ๋“ค์„ ๋น„๊ตํ•ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

90๋„ ์‹œ๊ณ„๋ฐฉํ–ฅ์œผ๋กœ ํšŒ์ „ํ•˜๋Š” ๊ณต์‹์€ (x,y) -> (y,-x)์ž…๋‹ˆ๋‹ค.

 

func rotateBlocks(block:[(Int,Int)]) -> [[(Int,Int)]] {
    var newBlock = block
    var rotateBlocks:[[(Int,Int)]] = [block]
    
    for _ in 0..<3 {
        var rotateBlock:[(Int,Int)] = []
        for b in newBlock {
            rotateBlock.append((b.1,-b.0))
        }
        newBlock = rotateBlock
        rotateBlocks.append(rotateBlock)
    }
    return rotateBlocks
}

 

3. ๊ฒŒ์ž„๋ณด๋“œ์™€ ํ…Œ์ด๋ธ”์˜ ํผ์ฆ ์กฐ๊ฐ์„ ๋ชจ๋‘ ๋น„๊ตํ•ด์ค๋‹ˆ๋‹ค.

 

๊ฒŒ์ž„๋ณด๋“œ์™€ ํ…Œ์ด๋ธ”์˜ ํผ์ฆ ์กฐ๊ฐ์ด ๊ฐ™์€ ๊ฒƒ์ธ์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ €๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋น„๊ตํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

 

1. ๋จผ์ € ํผ์ฆ ์กฐ๊ฐ์˜ ๋ชจ๋“  ์œ„์น˜๋ฅผ x๊ฐ€ ์ž‘์€ ๊ฒƒ์„ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•˜๊ณ  ๋งŒ์•ฝ ๊ฐ™๋‹ค๋ฉด y๊ฐ€ ์ž‘์€ ๊ฒƒ์„ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

 

2. ์ •๋ ฌ๋œ ํผ์ฆ ์กฐ๊ฐ์˜ ๊ฐ€์žฅ ์ฒซ ๋ฒˆ์งธ ์œ„์น˜๋ฅผ ์„œ๋กœ ๋น„๊ตํ•˜์—ฌ ๊ทธ ์ฐจ์ด๋ฅผ ๋”ํ•ด์ฃผ๊ณ  ๋น„๊ตํ–ˆ์Šต๋‹ˆ๋‹ค.

 

๋งŒ์•ฝ ์•„๋ž˜์™€ ๊ฐ™์€ ๋‘ ํผ์ฆ์ด ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๋ฉด ์ •๋ ฌ์„ ํ•ด์ฃผ๋ฉด 1. [00,10,20,21] 2. [11,21,31,32] ๊ฐ€ ๋  ๊ฒƒ ์ž…๋‹ˆ๋‹ค.

 

ex) 1. 00 10 20        2. 11 21 31

                     21                      32

 

๋‘ ํผ์ฆ์˜ ๊ฐ€์žฅ ์ฒซ ๋ฒˆ์งธ ์ฆ‰ 00๊ณผ 11์˜ ์ฐจ์ด๋Š” 0 - 1 = -1 0 - 1 = -1  -1,-1์ด๋ฏ€๋กœ

 

๋‘๋ฒˆ์งธ ํผ์ฆ์˜ x์™€ y์— -1,-1์„ ๋”ํ•ด์ฃผ๋ฉด ์ฒซ ๋ฒˆ์งธ ํผ์ฆ๊ณผ ๊ฐ™์ด ๋  ๊ฒƒ ์ž…๋‹ˆ๋‹ค.

 

๊ทธ๋ฆฌ๊ณค ๋ชจ๋“  ํผ์ฆ์˜ ์œ„์น˜๋ฅผ ๋น„๊ตํ•ด์ค๋‹ˆ๋‹ค.

 

func checkEqualPuzzleCount() -> Int {
    var answer:Int = 0
    while !emptyBlocks.isEmpty {
        let empty = emptyBlocks.removeFirst()
        outer:for (i,block) in blocks.enumerated() {
            if block.count != empty.count { continue }
            let emptySort = sortBlock(block: empty)
            second:for rotateBlock in rotateBlocks(block: block) {
                var rotateSort = sortBlock(block: rotateBlock)
                let x = emptySort.first!.0 - rotateSort.first!.0
                let y = emptySort.first!.1 - rotateSort.first!.1
                rotateSort = rotateSort.map{($0.0+x,$0.1+y)}
                for i in 0..<emptySort.count {
                    if emptySort[i] != rotateSort[i] {
                        continue second
                    }
                }
                    blocks.remove(at: i)
                    answer += empty.count
                    break outer
            }
        }
    }
    return answer
}

 

 

4. ๊ฐ™์€ ํผ์ฆ์˜ ์กฐ๊ฐ๋“ค์„ ์„ธ์–ด ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

 

 return checkEqualPuzzleCount()

Source Code

 

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€