POJ3435 Sudoku Checker

问题链接:POJ3435 Sudoku Checker。入门练习题,用C语言编写程序。

题意简述:输入n,然后输入(n×n)×(n×n)的二维数组,0表示可以是任意值,编写程序检查这些数据是否满足数独的初始状态。

问题分析:需要做的检查有:值范围检查,行、列和块的值重复检查。

程序中编写函数getblock()用于将行和列的值转换为块的值。

AC的C语言程序如下:

/* POJ3435 Sudoku Checker */

#include <stdio.h>
#include <memory.h>

#define MAXN 10+1

int row[MAXN * MAXN][MAXN * MAXN];
int col[MAXN * MAXN][MAXN * MAXN];
int block[MAXN * MAXN][MAXN * MAXN];

/* 行列下标转换为块下标 */
int getblock(int row, int col, int n)
{
    return (row / n) * n + col / n;
}

int main(void)
{
    int n, okflag, val, square, b, i, j;

    while(scanf("%d", &n) != EOF) {
        memset(row, 0, sizeof(row));
        memset(col, 0, sizeof(col));
        memset(block, 0, sizeof(block));

        okflag = 1;
        square = n * n;
        for(i=0; i<square; i++)
            for(j=0; j<square; j++) {
                scanf("%d", &val);

                if(val > square)
                    okflag = 0;
                else if(val) {
                    b = getblock(i, j, n);
                    if(row[i][val -1] || col[j][val - 1] || block[b][val - 1])
                        okflag = 0;

                    row[i][val - 1] = 1;
                    col[j][val - 1] = 1;
                    block[b][val - 1] = 1;
                }
            }

        printf("%s\n", okflag ? "CORRECT" : "INCORRECT");
    }

    return 0;
}
时间: 2024-10-14 18:06:47

POJ3435 Sudoku Checker的相关文章

POJ 3435 Sudoku Checker

Description The puzzle game of Sudoku is played on a board of N2 × N2 cells. The cells are grouped in N × N squares of N × N cells each. Each cell is either empty or contains a number between 1 and N2. The sudoku position is correct when numbers in e

ACdream 1195 Sudoku Checker (暴力)

Sudoku Checker Time Limit: 2000/1000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Others) SubmitStatus 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, a

【POJ3435】Sudoku Checker,注意:这不是数独!!!!

题意:给一个边长n*n的正方形数阵,然后问每一行.列.阵当前有值的数有没有重,你看一下两个样例就知道了. 题解:你想我告诉你怎么做?--如果这都不会,你该有多水啊.直接一byte一byte扒代码吧. 代码: #include <cstdio> #include <cstring> using namespace std; #define N 105 int map[N][N],visit[N],n,m; int main() { // freopen("test.in&q

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

Sudoku Checker Time Limit: 2000/1000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Others) SubmitStatisticNext Problem Problem Description Sudoku is a popular single player game. The objective is to fill a 9x9 matrix with digits so that each colu

LeetCode37 Sudoku Solver

题目: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by the character '.'. You may assume that there will be only one unique solution. A sudoku puzzle... ...and its solution numbers marked in red.  (Hard)

*Sudoku Solver

Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by the character '.'. You may assume that there will be only one unique solution. A sudoku puzzle... ...and its solution numbers marked in red. public clas

Valid Sudoku

Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be partially filled, where empty cells are filled with the character '.'. A partially filled sudoku which is valid. Note:A valid Sudoku board (partially

[LeetCode]Valid Sudoku

检测数独是否合格. 思路: 填充一遍就知道是否合格. 基本暴力搜索的思想. 1 /*************************************************************************************************** 2 Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. 3 The Sudoku board could be parti

[C++模板]Clang3.6版本的Checker

Clang3.6版本的Checker 一,Clang源码如下: template <typename CHECK1, typename CHECK2=check::_VoidCheck,           typename CHECK3=check::_VoidCheck, typename CHECK4=check::_VoidCheck,           typename CHECK5=check::_VoidCheck, typename CHECK6=check::_VoidChe