ACdream 1195 Sudoku Checker (数独)
Time Limit:1000MS Memory Limit:64000KB 64bit IO Format:%lld & %llu
Description
Sudoku is a popular single player game. The objective is to fill a 9x9 matrix with digits so that each column, each row, and all 9 non-overlapping 3x3 sub-matrices contain all of the digits from 1 through 9. Each 9x9 matrix is partially completed at the start of game play and typically has a unique solution.
Given a completed N2×N2 Sudoku matrix, your task is to determine whether it is a valid solution.
A valid solution must satisfy the following criteria:
- Each row contains each number from 1 to N2, once each.
- Each column contains each number from 1 to N2, once each.
- Divide the N2×N2 matrix into N2 non-overlapping N×N sub-matrices. Each sub-matrix contains each number from 1 to N2, once each.
You don't need to worry about the uniqueness of the problem. Just check if the given matrix is a valid solution.
Input
The first line of the input gives the number of test cases, T(1 ≤ T ≤ 100).
T test cases follow. Each test case starts with an integer N(3 ≤ N ≤ 6).
The next N2 lines describe a completed Sudoku solution, with each line contains exactly N2 integers.
All input integers are positive and less than 1000.
Output
For each test case, output one line containing "Case #x: y", where x is the case number (starting from 1) and y is "Yes" (quotes for clarity only) if it is a valid solution, or "No" (quotes for clarity only) if it is invalid.
Sample Input
3
3
5 3 4 6 7 8 9 1 2
6 7 2 1 9 5 3 4 8
1 9 8 3 4 2 5 6 7
8 5 9 7 6 1 4 2 3
4 2 6 8 5 3 7 9 1
7 1 3 9 2 4 8 5 6
9 6 1 5 3 7 2 8 4
2 8 7 4 1 9 6 3 5
3 4 5 2 8 6 1 7 9
3
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
3
5 3 4 6 7 8 9 1 2
6 7 2 1 9 5 3 4 8
1 9 8 3 4 2 5 6 7
8 5 9 7 6 1 4 2 3
4 2 6 8 999 3 7 9 1
7 1 3 9 2 4 8 5 6
9 6 1 5 3 7 2 8 4
2 8 7 4 1 9 6 3 5
3 4 5 2 8 6 1 7 9
Sample Output
Case #1: Yes
Case #2: No
Case #3: No
题意:检查一个方阵是不是数独。
题解:技巧暴力。
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxn=*;
int a[maxn][maxn];
bool vis[];//其实不用开这么大 超过n*n直接返回0了 开40就行 开大点保险
bool check(int n) //这里如果想不传参可以定义为全局变量
{
for(int i=; i<=n*n; i++)
{
memset(vis,,sizeof(vis));//注意初始化该放的位置
for(int j=; j<=n*n; j++)
{
if(a[i][j]<||a[i][j]>n*n) return ;//注意越界
if(vis[a[i][j]]) return ;
vis[a[i][j]]=;
}
}
for(int i=; i<=n*n; i++)
{
memset(vis,,sizeof(vis));
for(int j=; j<=n*n; j++)
{
if(a[j][i]<||a[j][i]>n*n) return ;//这里第一次直接复制的ij写反了 但是也过了 汗
if(vis[a[j][i]]) return ;
vis[a[j][i]]=;
}
}
for(int si=; si<n*n; si+=n)
for(int sj=; sj<n*n; sj+=n)
{
memset(vis,,sizeof(vis));
for(int i=; i<=n; i++)
{
for(int j=; j<=n; j++)
{
int x=si+i;
int y=sj+j;
if(a[x][y]<||a[x][y]>n*n) return ;
if(vis[a[x][y]]) return ;//注意vis是一维的
vis[a[x][y]]=;
}
}
}
return ;//默认也是返回1
}
int main()
{
int t,cas=,n;
cin>>t;
while(t--)
{
cin>>n;
for(int i=; i<=n*n; i++)
for(int j=; j<=n*n; j++)
cin>>a[i][j]; //也可以在这里判断一下如果越界直接No
if(check(n)) printf("Case #%d: Yes\n",cas++);//传n
else printf("Case #%d: No\n",cas++);
}
return ;
}
ACdream 1195 Sudoku Checker (数独)的更多相关文章
- ACdream 1195 Sudoku Checker (暴力)
Sudoku Checker Time Limit: 2000/1000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Others) Submi ...
- LeetCode:36. Valid Sudoku,数独是否有效
LeetCode:36. Valid Sudoku,数独是否有效 : 题目: LeetCode:36. Valid Sudoku 描述: Determine if a Sudoku is valid, ...
- 【POJ - 2676】Sudoku(数独 dfs+回溯)
-->Sudoku 直接中文 Descriptions: Sudoku对数独非常感兴趣,今天他在书上看到了几道数独题: 给定一个由3*3的方块分割而成的9*9的表格(如图),其中一些表格填有1- ...
- [LeetCode] Valid Sudoku 验证数独
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...
- ACM : POJ 2676 SudoKu DFS - 数独
SudoKu Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu POJ 2676 Descr ...
- ACM: ICPC/CCPC Sudoku DFS - 数独
Sudoku Time Limit : 3000/1000ms (Java/Other) Memory Limit : 65535/65535K (Java/Other) Total Submis ...
- lintcode:Valid Sudoku 判断数独是否合法
题目: 判断数独是否合法 请判定一个数独是否有效.该数独可能只填充了部分数字,其中缺少的数字用 . 表示. 样例 下列就是一个合法数独的样例. 注意 一个合法的数独(仅部分填充)并不一定是可解的.我们 ...
- HDU - 5547 Sudoku(数独搜索)
Description Yi Sima was one of the best counselors of Cao Cao. He likes to play a funny game himself ...
- [leetcode]36. Valid Sudoku验证数独
Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to th ...
随机推荐
- Session解析
1.除非关闭所有页面 或者超时session才销毁 2.在几个页面之间切换的时候 session保存用户状态. 3.遍历数组时候for循环中从0开始小于长度,不等于长度,用Matlab用习惯了,竟然从 ...
- c++实现gray code(格雷码)
今天别人问的一道题,强调用分治法实现 =.= 百度了一下格雷码,然后写了一下. 关于格雷码大家看百度的吧,特别详细,贴个图: 代码如下(header_file.h是我自己写的一个头文件,包括常见的ve ...
- 深入理解 Javascript 面向对象编程
一:理解构造函数原型(prototype)机制 prototype是javascript实现与管理继承的一种机制,也是面向对象的设计思想.构造函数的原型存储着引用对象的一个指针,该指针指向与一个原型对 ...
- ThinkPHP3.2 行为扩展以及插件机制介绍!
首先行为扩展这个概念是TP架构的核心组成之一,关于行为的解释我就粗略的概括一下吧:TP在从接受到HTTP请求到最终将视图输出,期间经历的很多步骤,这些步骤大家可以在http://document.th ...
- derby数据库操作比较难理解的错误及解决方法大全
一.插入(INSERT时报错) 1.错误:java.sql.SQLIntegrityConstraintViolationException: 列“test”无法接受空值. 可能原因:建表时test列 ...
- 如何查看ubuntu下显卡驱动是否已经成功安装
首先得安装mesa-utils,在终端输入命令:sudo apt-get install mesa-utils然后再运行命令:glxinfo | grep rendering如果结果是“yes”,证明 ...
- smarty 操作符号,大于、小于。。。
eq相等,6 w% x7 w6 |3 _ne.neq不相等,( i" }" ~( `# V( t& C, k; [gt大于,lt小于,gte.ge大于等于,lte.le 小 ...
- 模板插件aTpl.js新增功能
摘要: aTpl.js是一款模板插件,该插件支持ie5+,chrome等浏览器以及移动端浏览器,支持for和if语法,以及表达式.最近对aTpl.js模板插件增加了新的功能,支持字符串模板,同时增加了 ...
- css 伪类::after ::beftor 的使用方式
注释:对于 IE8 及更早版本中的 :before,必须声明 . ::before和::after这两个主要用来给元素的前面或后面插入内容,这两个常用"content"配合使用,见 ...
- 如何将北京时间批量转为Unix时间?用Excel!
前面我们说过Unix时间戳转换怎样在Excel批量修改,有些人就想如果有特殊需求,那能不能批量将北京时间批量转成unix时间呢?能!用Excel就可以实现!跟ytkah一起试试吧. 将unix时间戳转 ...