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

A - Problem A

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-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
 
  
 
  
 
  
 
  
 
  
解题思路:
    本题大意是判断一个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");
    }
}


 
 
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值