ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป ๋ฌธ์ œ

 

1780๋ฒˆ: ์ข…์ด์˜ ๊ฐœ์ˆ˜

N×Nํฌ๊ธฐ์˜ ํ–‰๋ ฌ๋กœ ํ‘œํ˜„๋˜๋Š” ์ข…์ด๊ฐ€ ์žˆ๋‹ค. ์ข…์ด์˜ ๊ฐ ์นธ์—๋Š” -1, 0, 1์˜ ์„ธ ๊ฐ’ ์ค‘ ํ•˜๋‚˜๊ฐ€ ์ €์žฅ๋˜์–ด ์žˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ์ด ํ–‰๋ ฌ์„ ์ ์ ˆํ•œ ํฌ๊ธฐ๋กœ ์ž๋ฅด๋ ค๊ณ  ํ•˜๋Š”๋ฐ, ์ด๋•Œ ๋‹ค์Œ์˜ ๊ทœ์น™์— ๋”ฐ๋ผ ์ž๋ฅด๋ ค๊ณ  ํ•œ๋‹ค.

www.acmicpc.net


โœ๐Ÿป ํ’€์ด

๐ŸŽจ Go

// https://www.acmicpc.net/problem/1780
// ์ฟผ๋“œํŠธ๋ฆฌ์™€ ๋น„์Šทํ•œ๋ฐ 4๊ฐœ ๋Œ€์‹  9๊ฐœ๋กœ ๋‚˜๋ˆ„๋Š” ๋ฌธ์ œ
package main

import (
	"bufio"
	"fmt"
	"os"
)

var (
	paper      [][]int
	minusCount int
	zeroCount  int
	plusCount  int
)

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	var n int
	fmt.Fscanln(reader, &n)

	paper = make([][]int, n)
	for i := 0; i < n; i++ {
		paper[i] = make([]int, n)
	}

	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			fmt.Fscanf(reader, "%d ", &paper[i][j])
		}
	}

	checkPaper(0, 0, n)
	fmt.Fprintln(writer, minusCount)
	fmt.Fprintln(writer, zeroCount)
	fmt.Fprintln(writer, plusCount)
}

func checkPaper(row, col, n int) {
	first := paper[row][col]
	isComplete := true
	for i := row; i < row+n; i++ {
		for j := col; j < col+n; j++ {
			if i == row && j == col {
				continue
			}
			if paper[i][j] != first {
				isComplete = false
				break
			}
		}
		if !isComplete {
			break
		}
	}
	if isComplete {
		switch first {
		case -1:
			minusCount++
		case 0:
			zeroCount++
		case 1:
			plusCount++
		}
		return
	}
	interval := n / 3
	checkPaper(row, col, interval)
	checkPaper(row, col+interval, interval)
	checkPaper(row, col+2*interval, interval)
	checkPaper(row+interval, col, interval)
	checkPaper(row+interval, col+interval, interval)
	checkPaper(row+interval, col+2*interval, interval)
	checkPaper(row+2*interval, col, interval)
	checkPaper(row+2*interval, col+interval, interval)
	checkPaper(row+2*interval, col+2*interval, interval)
}

๐ŸŽจ Python3

# https://www.acmicpc.net/problem/1780
# ์ฟผ๋“œํŠธ๋ฆฌ์™€ ๋น„์Šทํ•œ๋ฐ 4๊ฐœ ๋Œ€์‹  9๊ฐœ๋กœ ๋‚˜๋ˆ„๋Š” ๋ฌธ์ œ
import sys

paper = []
minus_count = 0
zero_count = 0
plus_count = 0

def check_paper(row, col, n):
    global paper, minus_count, zero_count, plus_count
    first = paper[row][col]
    is_complete = True
    for i in range(row, row+n):
        for j in range(col, col+n):
            if i == row and j == col:
                continue
            if paper[i][j] != first:
                is_complete = False
                break
        if not is_complete:
            break
    if is_complete:
        if first == -1:
            minus_count += 1
        elif first == 0:
            zero_count += 1
        elif first == 1:
            plus_count += 1
        return
    
    interval = n//3
    check_paper(row, col, interval)
    check_paper(row, col+interval, interval)
    check_paper(row, col+2*interval, interval)
    check_paper(row+interval, col, interval)
    check_paper(row+interval, col+interval, interval)
    check_paper(row+interval, col+2*interval, interval)
    check_paper(row+2*interval, col, interval)
    check_paper(row+2*interval, col+interval, interval)
    check_paper(row+2*interval, col+2*interval, interval)

if __name__ == "__main__":
    n = int(sys.stdin.readline())
    for i in range(n):
        paper.append(list(map(int, sys.stdin.readline().split())))

    check_paper(0, 0, n)
    print(minus_count)
    print(zero_count)
    print(plus_count)
728x90
๋Œ“๊ธ€