当前位置:网站首页>判断是否是数独
判断是否是数独
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
}
边栏推荐
- Aneng logistics' share price hit a new low: the market value evaporated by nearly 10 billion yuan, and it's useless for chairman Wang Yongjun to increase his holdings
- The best blog to explain the basics of compilation (share)
- Flex layout
- OpenShift 部署应用
- 使用wireshark抓取Tcp三次握手
- Live broadcast platform development, flexible menu, and freely adjust the horizontal size of the menu bar
- Use the numbers 5, 5, 5, 1 to perform four operations. Each number should be used only once, and the operation result value is required to be 24
- STM32-新建工程(参考正点原子)
- Sqli labs (post type injection)
- Getting started with k8s: building MySQL with Helm
猜你喜欢
c语言自定义类型——结构体,位段(匿名结构体,结构体的自引用,结构体的内存对齐)
Valin cable: BI application promotes enterprise digital transformation
Openfeign is easy to use
Generate database documents with one click, which can be called swagger in the database industry
路由基础—动态路由
2022 Heilongjiang's latest eight member (Safety Officer) simulated test question bank and answers
Sentinel 简单使用
链表经典面试题(反转链表,中间节点,倒数第k个节点,合并分割链表,删除重复节点)
Minecraft插件服开服
Chrome debugging
随机推荐
Linked list classic interview questions (reverse the linked list, middle node, penultimate node, merge and split the linked list, and delete duplicate nodes)
Force deduction method summary: find classes
Force buckle method summary: sliding window
Makefile Fundamentals
idea中注释代码取消代码的快捷键
Minecraft插件服开服
STM32-新建工程(参考正点原子)
Getting started with k8s: building MySQL with Helm
Analysis of the use of comparable, comparator and clonable interfaces
zipkin 简单使用
什么是SQL注入
Deep understanding of JVM
Hcia - Application Layer
KubeSphere 虚拟化 KSV 安装体验
sqli-labs第1关
程序猿学英语-Learning C
Web安全--核心防御机制
c语言自定义类型——结构体,位段(匿名结构体,结构体的自引用,结构体的内存对齐)
Causes of laptop jam
Data asset management function