ACdream 1195 Sudoku Checker (暴力),acdreamsudoku

來源:互聯網
上載者:User

ACdream 1195 Sudoku Checker (暴力),acdreamsudoku

Sudoku CheckerTime Limit: 2000/1000MS (Java/Others)Memory Limit:128000/64000KB (Java/Others)SubmitStatusProblem 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
335 3 4 6 7 8 9 1 26 7 2 1 9 5 3 4 81 9 8 3 4 2 5 6 78 5 9 7 6 1 4 2 34 2 6 8 5 3 7 9 17 1 3 9 2 4 8 5 69 6 1 5 3 7 2 8 42 8 7 4 1 9 6 3 53 4 5 2 8 6 1 7 931 2 3 4 5 6 7 8 91 2 3 4 5 6 7 8 91 2 3 4 5 6 7 8 91 2 3 4 5 6 7 8 91 2 3 4 5 6 7 8 91 2 3 4 5 6 7 8 91 2 3 4 5 6 7 8 91 2 3 4 5 6 7 8 91 2 3 4 5 6 7 8 935 3 4 6 7 8 9 1 26 7 2 1 9 5 3 4 81 9 8 3 4 2 5 6 78 5 9 7 6 1 4 2 34 2 6 8 999 3 7 9 17 1 3 9 2 4 8 5 69 6 1 5 3 7 2 8 42 8 7 4 1 9 6 3 53 4 5 2 8 6 1 7 9
Sample Output
Case #1: YesCase #2: NoCase #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_JUDGEfreopen("in.txt","r",stdin);#endifscanf("%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;}





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.