当前位置:网站首页>判断是否是数独
判断是否是数独
2022-07-02 06:30:00 【图图是只猫】
题目
数独是一类广受大众喜欢的填数游戏。最典型的数独由9*9的方格组成,其中会填入一些1~ 9的数字。玩家需要补上剩余格子的数字,使得每个格子所在的行、列、小九宫格都包含有1~9的数字,不重不漏。
现在小明已经做好了一些填好了所有数字的数独游戏题面,正准备挖空。但在此之前,他希望你帮忙检查一下题面是否正确,即做好的题面是否每个格子都满足所在行、列、小九宫格都包含1~9。
输入
第一行输入一个数T,表示小明已完成的数独题面个数。
之后每个题面输入共9行,每行9个数以空格隔开,表示填好的数独中的数字。
输出
输出共T行,每行一个字符串表示该数独题面是否正确。
正确输出"YES",错误则输出"NO"。
数据范围
对于100%的数据,1≤T≤5,1≤每个数字≤9。
输入样例
2
7 6 2 5 9 3 1 4 8
9 4 1 2 7 8 5 3 6
8 3 5 4 6 1 7 9 2
1 9 8 6 2 7 3 5 4
4 7 6 3 5 9 2 8 1
2 5 3 8 1 4 6 7 9
3 8 7 1 4 6 9 2 5
5 1 4 9 3 2 8 6 7
6 2 9 7 8 5 4 1 3
8 1 4 7 3 2 6 5 9
9 2 3 6 5 8 1 4 7
5 7 6 4 9 1 2 8 3
3 4 5 2 6 7 8 9 1
1 8 9 3 4 5 3 2 6
2 6 7 1 8 9 5 3 4
7 5 1 9 2 4 7 6 8
4 3 8 5 7 6 9 1 2
6 9 2 8 1 3 4 7 5
输出样例
YES
NO
题解
Go语言实现
package main
import (
"fmt"
)
func main() {
//输入
var T int
fmt.Scan(&T)
if T < 1 || T > 5 {
fmt.Println("T 超出范围")
return
}
flag := true
var result [5] bool
for i := 0; i < T; i++ {
//输入数组
var c [9][9] int
for i := 0; i < 9; i++ {
for j := 0; j < 9 ; j++ {
var num int
fmt.Scan(&num)
c[i][j] = num
}
}
for n := 0; n < 9 ; n++ {
//比较行
if isRepetition(c[n]) {
flag = false
break
}
//比较列
var col [9] int
for k := 0; k < 9 ;k++ {
col[k] = c[k][n]
}
if isRepetition(col){
flag = false
break
}
}
if flag {
//九宫格
var nine [9] int
for n := 0 ; n < 7; n++{
for k := 0 ; k < 7; k++{
nine[0] = c[n][k]
nine[1] = c[n+1][k]
nine[2] = c[n][k+1]
nine[3] = c[n+2][k]
nine[4] = c[n][k+2]
nine[5] = c[n+2][k+1]
nine[6] = c[n+1][k+2]
nine[7] = c[n+1][k+1]
nine[8] = c[n+2][k+2]
}
}
if isRepetition(nine) {
result[i] = false
}else {
result[i] = true
}
}else {
result[i] = false
}
}
for i := 0; i < T ; i++{
if result[i] {
fmt.Println("YES")
}else {
fmt.Println("NO")
}
}
}
//判断数组中9个数是否重复
func isRepetition(numChar [9] int) bool {
for i := 0 ; i < len(numChar)-1; i++ {
for j := i+1; j < len(numChar); j++ {
if numChar[i] == numChar[j] {
return true
}
}
}
return false
}
边栏推荐
- Analysis of the use of comparable, comparator and clonable interfaces
- Global and Chinese market of electric cheese grinder 2022-2028: Research Report on technology, participants, trends, market size and share
- One of the reasons for WCF update service reference error
- Googlenet network explanation and model building
- 路由基础—动态路由
- 旋转链表(图解说明)
- Pclpy projection filter -- projection of point cloud to cylinder
- idea中注释代码取消代码的快捷键
- k8s入门:Helm 构建 MySQL
- HCIA—数据链路层
猜你喜欢

Installation and use of simple packaging tools

cve_ 2019_ 0708_ bluekeep_ Rce vulnerability recurrence

Solid principle: explanation and examples

2022 Heilongjiang latest food safety administrator simulation exam questions and answers

Openshift deployment application

Web security -- core defense mechanism

File upload Labs

Valin cable: BI application promotes enterprise digital transformation

Luogu greedy part of the backpack line segment covers the queue to receive water

Sentinel 简单使用
随机推荐
c语言自定义类型——结构体,位段(匿名结构体,结构体的自引用,结构体的内存对齐)
程序猿学英语-指令式编程
Kubedm deploys kubernetes v1.23.5 cluster
Use C language to receive JSON strings
Installation and use of simple packaging tools
SQL operation database syntax
KubeSphere 虚拟化 KSV 安装体验
The source code of the live app. When the verification method is mailbox verification, the verification code is automatically sent to the entered mailbox
C language replaces spaces in strings with%20
Getting started with k8s: building MySQL with Helm
[blackmail virus data recovery] suffix Crylock blackmail virus
ICMP协议
Routing foundation - dynamic routing
[dynamic planning] p4170: coloring (interval DP)
Matlab-其它
Force deduction method summary: double pointer
Implementation of bidirectional linked list (simple difference, connection and implementation between bidirectional linked list and unidirectional linked list)
Don't know mock test yet? An article to familiarize you with mock
[blackmail virus data recovery] suffix Rook3 blackmail virus
链表经典面试题(反转链表,中间节点,倒数第k个节点,合并分割链表,删除重复节点)