Sudoku Checker

Time Limit: 2000/1000MS (Java/Others)Memory Limit:
128000/64000KB (Java/Others)

Problem 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-overlappingN×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 exactlyN2 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) andy 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

大致题意:给一个数独。看看符不符合要求。

PS:做个题还长姿势了。曾经仅仅听过数独,可是没玩过,这次A道题,搞懂了其本规则,哈哈。收获不小。

好了。那就介绍一下规则吧,现给出n^2*n^2的数独,推断是否满足:

1.每行要用1~n^2的数填满,而且每一个数仅仅出现一次。就是把1~n^2排列到每一行。

2.行的要求跟列一样;

3.还要满足均分成的n^2个n*n的矩形的元素也要满足1~n^2个数的排列。

AC代码:

#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
int a[40][40], b[40][1000], c[40][1000], d[1000];
int T,t,n; int main(){
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
scanf("%d", &T);
for(int t=1; t<=T; t++){
memset(b,0,sizeof(b));
memset(c,0,sizeof(c));
scanf("%d", &n);
for(int i=1; i<=n*n; i++){ //处理行列
for(int j=1; j<=n*n; j++){
scanf("%d", &a[i][j]);
b[i][a[i][j]]++;
c[j][a[i][j]]++;
}
}
int flag = 1;
for(int i=1; i<=n*n; i++){                      //推断行列
for(int j=1; j<=n*n; j++){
if(b[i][j]!=1 || c[i][j]!=1){
flag = 0;
break;
}
}
}
if(flag){
for(int i=1; i<=n; i++){                //推断每一个n*n矩阵
for(int j=1; j<=n; j++){
memset(d,0,sizeof(d));
for(int k=1; k<=n; k++){
for(int s=1; s<=n; s++){
d[a[(i-1)*n+k][(j-1)*n+s]]++;
}
}
for(int k=1; k<=n*n; k++){
if(d[k]!=1){
flag = 0;
break;
}
}
if(!flag) break;
}
if(!flag) break;
}
}
if(flag) printf("Case #%d: Yes\n", t);
else printf("Case #%d: No\n", t);
}
return 0;
}

ACdream 1195 Sudoku Checker (暴力)的更多相关文章

  1. ACdream 1195 Sudoku Checker (数独)

    Sudoku Checker Time Limit:1000MS     Memory Limit:64000KB     64bit IO Format:%lld & %llu Submit ...

  2. Spell checker(暴力)

    Spell checker Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 20188   Accepted: 7404 De ...

  3. Codeforce Gym 100015I Identity Checker 暴力

    Identity Checker 题目连接: http://codeforces.com/gym/100015/attachments Description You likely have seen ...

  4. 快速切题 acdream手速赛(6)A-C

    Sudoku Checker Time Limit: 2000/1000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Others) Submi ...

  5. POJ训练计划1035_Spell checker(串处理/暴力)

    Spell checker Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18418   Accepted: 6759 De ...

  6. hdu 1195:Open the Lock(暴力BFS广搜)

    Open the Lock Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  7. 2015南阳CCPC H - Sudoku 暴力

    H - Sudoku Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description Yi Sima was one of the best cou ...

  8. acdream 小晴天老师系列——晴天的后花园 (暴力+剪枝)

    小晴天老师系列——晴天的后花园 Time Limit: 10000/5000MS (Java/Others)    Memory Limit: 128000/64000KB (Java/Others) ...

  9. acdream暴力专场中的优美暴力

    F - 小晴天老师系列——苹果大丰收 Time Limit: 2000/1000MS (Java/Others)    Memory Limit: 128000/64000KB (Java/Other ...

随机推荐

  1. bzoj 2437 [Noi2011]兔子和鸡蛋 [二分图匹配]

    叙述性说明 这些日子.兔子和蛋像一个新的棋盘游戏. 这场比赛是在 n 行 m 在船上进行列. 前,棋盘上有一 个格子是空的,其他的格子中都放置了一枚棋子,棋子或者是黑色,或者是白色. 每一局游戏总是兔 ...

  2. linux笔记本上安装了双显卡驱动(intel+nvidia)

    为了提高linux图形性能并降低功耗,独特的文章. 我用的系统Fedora 20 Xfce x64,在安装驱动程序,以确保系统是最新的版本号. 最好安装gcc.kernel-devel和其他包.己主动 ...

  3. C#格式化字符串中转义大括号“{}”

    原文:C#格式化字符串中转义大括号"{}" 今天,用C#写程序操作Excel,读取单元格内容根据所需格式生成字符串,使用String.Format(string format,ob ...

  4. cocos2d-x注意事项(十)Lua发展飞机战争-4-创建主角

    二战中被称为二战飞机飞机,当然,以飞机作业.这是一个游戏,我们必须加入一个飞机--这是我们的英雄. 首先创建一个层(PlaneLayer)要显示飞机.然后,create飞机初始化方法 module(& ...

  5. docker study

    Author:Hyphen 同步公布在个人博客中:http://yanheven.github.io/docker-study/ 參考章宇兄的开源项目学习ABC的方法来对docker进行简单的学习与分 ...

  6. GPS转换为百度坐标

    原文地址:http://www.cnblogs.com/zhaohuionly/archive/2013/06/18/3142623.html 最近在做一个关于手机定位的小应用,需求是这样的,用户通过 ...

  7. 项目中经常使用的JS方法汇总,非常有用

    // 对Date的扩展,将 Date 转化为指定格式的String   // 月(M).日(d).小时(h).分(m).秒(s).季度(q) 可以用 1-2 个占位符,   // 年(y)可以用 1- ...

  8. crawler_网络爬虫之数据分析_httpwatcher

    所谓爬虫,首先要通过各种手段爬取到想要站点的数据. web2.0之后,各种网络站点类型越来越多,早期的站点多为静态页面[html .htm],后来逐步加入 jsp.asp,等交互性强的页面.再后来随着 ...

  9. Log4j2 简明教程

    一.概述 log4j2官方文档内容非常多,要一次性了解全部是不可能的.正确的步骤应当是先了解最常见的配置,当发现原有知识无法解决问题,再重新查看文档看有没有合适的配置.下面将从文件结构入手,再到简单的 ...

  10. BEGINNING SHAREPOINT&#174; 2013 DEVELOPMENT 第3章节--SharePoint 2013 开发者工具 SharePoint中基于Web开发

    BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第3章节--SharePoint 2013 开发者工具 SharePoint中基于Web开发         之前提到过, ...