スドック検事


作成日:2022年1月14日午後4:57

インプリメンテーションコード

# 스도쿠 검사
import sys
sys.stdin = open("input.txt", "rt")
sudoku = [list(map(int, input().split())) for _ in range(9)]
isSudoku = True
s1 = set()
s2 = set()
s3 = set()
s4 = set()
s5 = set()
s6 = set()
s7 = set()
s8 = set()
s9 = set()

for i in range(9):
    set1 = set(sudoku[0])
    if len(set1) != 9:
        isSudoku = False
    set2 = set()
    for j in range(9):
        set2.add(sudoku[j][i])
        if i < 3 and j < 3:
            s1.add(sudoku[i][j])
        elif i < 3 and j >= 3 and j < 6:
            s2.add(sudoku[i][j])
        elif i < 3 and j >= 6 and j < 9:
            s3.add(sudoku[i][j])
        elif i >= 3 and i < 6 and j < 3:
            s4.add(sudoku[i][j])
        elif i >= 3 and i < 6 and j >= 3 and j < 6:
            s5.add(sudoku[i][j])
        elif i >= 3 and i < 6 and j >= 6 and j < 9:
            s6.add(sudoku[i][j])
        elif i >= 6 and i < 9 and j < 3:
            s7.add(sudoku[i][j])
        elif i >= 6 and i < 9 and j >= 3 and j < 6:
            s8.add(sudoku[i][j])
        else:
            s9.add(sudoku[i][j])
    if len(set2) != 9:
        isSudoku = False

if len(s1) == len(s2) == len(s3) == len(s4) ==len(s5) ==len(s6) ==len(s7) ==len(s8) ==len(s9) == 9:
    pass
else:
    isSudoku = False

if isSudoku:
    print("YES")
else:
    print("NO")

模範解答

import sys
sys.stdin=open("input.txt", "r")
def check(a):
    for i in range(9):
        ch1=[0]*10
        ch2=[0]*10
        for j in range(9):
            ch1[a[i][j]]=1
            ch2[a[j][i]]=1
        if sum(ch1)!=9 or sum(ch2)!=9:
            return False
    for i in range(3):
        for j in range(3):
            ch3=[0]*10
            for k in range(3):
                for s in range(3):
                    ch3[a[i*3+k][j*3+s]]=1
            if sum(ch3)!=9:
                return False
    return True

a=[list(map(int, input().split())) for _ in range(9)]
if check(a):
    print("YES")
else:
    print("NO")

差異

  • 私が実装したコードでは,9×9スクールバンで3×3サイズの小さな領域で,ドアに条件文を掛けて確認する.
  • 模範解答四重for文を用いて,条件文数を減らすように3×3サイズの領域を調べた.