Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
834 views
in Technique[技术] by (71.8m points)

check column and row in sodoku- C programming

I'm trying to check if number (from 1 to n)

appears more than once in

row or column in sodoku board

I wrote a code that goes over each row and column and checks the numbers between 1 to n (every time checking number at a time after finishing with row 0 (for example) goes to the next one (if it wasn't true in the row before)

my code is working just if there is a number

appearing more than once

in row 0 (if there is in other rows it returns nothing!)

and for the column it doesn't return any thing

this is a matrix that I use for the test (matrix with solution and I change numbers in rows or columns):

7 1 0 0 0 0 6 0 9
2 0 0 0 0 3 0 0 0
0 0 0 1 5 0 0 0 8
0 0 7 0 0 0 0 9 0
0 0 6 0 0 0 7 0 0
0 2 0 0 0 0 4 0 0
1 0 0 0 2 9 0 0 0
0 0 0 3 0 0 0 0 4
9 0 5 0 0 0 0 8 6

and this is my code:

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>


#define MAX4USED 10


int n; //size of the sodoku board

bool CheckAppearingInRow(int matrix [n][n]) {
    int used[MAX4USED] = {0};
    int index = 1;
    int row = 0;
    while (index <= n) {
        for(int j = 0; j < n; j++) {
            if(matrix[row][j] == index ) {
                used[index] +=1;
            }
        }
        for(int i = 0; i <= n ; i++) {
            if (used[i] > 1) {
                return true;
            }
            used[i] = 0;
        }
        index += 1;
    }
    row += 1;
    while (row < n) {
        CheckAppearingInRow(matrix);
    }
    return false;
}



bool CheckAppearingInColumn(int matrix [n][n]) {
    int used[MAX4USED] = {0};
    int index = 1;
    int col = 0;
    while (index <= n) {
        for(int i = 0; i < n; i++) {
            if(matrix[i][col] == index ) {
                used[index] +=1;
            }
        }
        for(int i = 0; i <= n ; i++) {
            if (used[i] > 1) {
                return true;
            }
            used[i] = 0;
        }
        index += 1;
    }
    col += 1;
    while(col < n){
         CheckAppearingInColumn(matrix);
    }
    return false;
}


int main() {
    printf("Please enter your sodoku dimension:");
    scanf("%d", &n);
    int a[n][n];
    printf("
Insert your sodoku board
");
    printf("Instruction: Enter 0 for blank
");
    for(int i=0; i<n; i++){
       for(int j=0; j<n; j++) {
         scanf("%d", &a[i][j]);
       }
    }
    if (CheckAppearingInRow(a)== true || CheckAppearingInColumn(a) == true) {
        printf("
No solution!
");
    } 
    return 0;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...