Created
September 18, 2019 02:12
-
-
Save m2kar/5cd5fb94a254feb278ffc3d0702ad3da to your computer and use it in GitHub Desktop.
给定数独板,检测数独是否正确 快手笔试题4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| 给定数独板,检测数独是否正确 | |
| 快手笔试题4 | |
| """ | |
| class E(Exception): | |
| pass | |
| def valid(l): | |
| d={} | |
| for v in l: | |
| if v!=0: | |
| if v in d: | |
| raise E | |
| d[v]=1 | |
| return True | |
| def inm(): | |
| m=[] | |
| for i in range(9): | |
| m.append([]) | |
| for c in input(): | |
| if c in "123456789": | |
| m[-1].append(int(c)) | |
| elif c=="X": | |
| m[-1].append(0) | |
| else: | |
| # print("false") | |
| raise E | |
| for l in m: | |
| if not valid(l): | |
| print("false") | |
| raise E | |
| for i in range(9): | |
| if not valid([m[j][i] for j in range(9)]): | |
| # print("false") | |
| raise E | |
| for i in range(3): | |
| for j in range(3): | |
| l=[] | |
| for k in range(3): | |
| for o in range(3): | |
| l.append(m[3*i+k][3*j+o]) | |
| if not valid(l): | |
| # print("false") | |
| raise E | |
| try: | |
| inm() | |
| except E: | |
| print("false") | |
| else: | |
| print("true") |
Author
m2kar
commented
Sep 18, 2019


Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment