当前位置:网站首页>判断是否是数独
判断是否是数独
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
}
边栏推荐
- Googlenet network explanation and model building
- HCIA - data link layer
- 用数字 5,5,5,1 ,进行四则运算,每个数字当且仅当用一次,要求运算结果值为 24
- ARP及ARP欺骗
- Use Wireshark to grab TCP three handshakes
- Sentinel 简单使用
- Global and Chinese market of wire loop, 2022-2028: Research Report on technology, participants, trends, market size and share
- C language custom types - structure, bit segment (anonymous structure, self reference of structure, memory alignment of structure)
- First week of JS study
- 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
猜你喜欢

Minecraft安装资源包

Kubesphere virtualization KSV installation experience

Use C language to receive JSON strings
![[blackmail virus data recovery] suffix Crylock blackmail virus](/img/b2/8e3a65dd250b9194cfc175138c740c.jpg)
[blackmail virus data recovery] suffix Crylock blackmail virus

HCIA - application layer

ARP and ARP Spoofing

St-link connection error invalid ROM table of STM32 difficult and miscellaneous diseases

Getting started with k8s: building MySQL with Helm

HCIA—應用層

Sqli labs Level 2
随机推荐
Data asset management function
顺序表基本功能函数的实现
Googlenet network explanation and model building
Call Stack
Matlab-其它
实现双向链表(带傀儡节点)
程序猿学英语-指令式编程
Vs code configuration problem
Sqli labs level 1
类和对象(类和类的实例化,this,static关键字,封装)
STM32 new project (refer to punctual atom)
The source code of the live app. When the verification method is mailbox verification, the verification code is automatically sent to the entered mailbox
Analysis of the use of comparable, comparator and clonable interfaces
IP protocol and IP address
Method recursion (Fibonacci sequence, frog jumping steps, tower of Hanoi problem)
sqli-labs第1关
Honeypot attack and defense drill landing application scheme
Matlab mathematical modeling tool
Kubesphere virtualization KSV installation experience
Minecraft模组服开服