ACdream区域赛指导赛之手速赛系列(6) A.

来源:互联网 发布:网络视频监控系统方案 编辑:程序博客网 时间:2024/06/05 22:49

A - Problem A

Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others)
SubmitStatus
题目链接:http://acdream.info/contest?cid=1102#problem

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-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

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
解题思路:
    本题大意是判断一个n^2 * n^2的矩阵是否满足每行元素属于1~n^2(且不相等)、
每列元素属于1~n^2(且不相等)、每个n*n子矩阵元素属于1~n^2(且不相等)。
    直接暴力····最精彩的是最后一个判断子矩阵的时候(四层循环)····直接上代码····
完整代码:
#include <functional>#include <algorithm>#include <iostream>#include <fstream>#include <sstream>#include <iomanip>#include <numeric>#include <cstring>#include <climits>#include <cassert>#include <complex>#include <cstdio>#include <string>#include <vector>#include <bitset>#include <queue>#include <stack>#include <cmath>#include <ctime>#include <list>#include <set>#include <map>using namespace std;#pragma comment(linker, "/STACK:102400000,102400000")typedef long long LL;typedef double DB;typedef unsigned uint;typedef unsigned long long uLL;/** Constant List .. **/ //{const int MOD = int(1e9)+7;const int INF = 0x3f3f3f3f;const LL INFF = 0x3f3f3f3f3f3f3f3fLL;const DB EPS = 1e-9;const DB OO = 1e20;const DB PI = acos(-1.0); //M_PI;int a[1111][1111];int vis[1111];int main(){    #ifndef ONLINE_JUDGE    freopen("in.txt","r",stdin);    #endif    int T;    scanf("%d",&T);    int cas = 1;    while(T--)    {        int n;        scanf("%d",&n);        int s = n * n;        int len = n * n * n * n;        for(int i = 0 ; i < s ; i ++)        {            for(int j = 0 ; j < s ; j ++)            {                scanf("%d",&a[i][j]);            }        }        printf("Case #%d: ", cas++);        int flag = 0;        for(int i = 0 ; i < s ; i ++)        {            memset(vis , 0 , sizeof(vis));            for(int j = 0 ; j < s ; j ++)                vis[a[i][j]] ++;            for(int k = 1 ; k <= s ; k ++)            {                if(vis[k] != 1)                {                    flag = 1;                    break;                }            }            if(flag)                break;        }        if(flag)        {            printf("No\n");            continue;        }        for(int j = 0 ; j < s ; j ++)        {            memset(vis , 0 , sizeof(vis));            for(int i = 0 ; i < s ; i ++)            {                vis[a[i][j]] ++;            }            for(int k = 1 ; k <= s ; k ++)            {                if(vis[k] != 1)                {                    flag = 1;                    break;                }            }            if(flag)                break;        }        if(flag)        {            printf("No\n");            continue;        }        for(int i = 0 ; i < s  ; i += n )        {            for(int j = 0 ; j < s ; j += n)            {                memset(vis , 0 , sizeof(vis));                for(int k = i ; k < n + i; k ++)                {                    for(int t = j ; t < n + j; t ++)                        vis[a[k][t]] ++;                }                for(int q = 1 ; q <= s; q ++)                {                    if(vis[q] != 1)                    {                        flag = 1;                        break;                    }                }                if(flag)                    break;            }            if(flag)                break;        }        if(flag)        {            printf("No");            continue;        }        printf("Yes\n");    }}


0 0
原创粉丝点击