逻辑检查未按预期输出。 (数独检查器)C

Logic check not outputting as expected. (Sudoku checker) C

我有一个函数可以检查数独板的所有行,如果无效则输出并退出。

(有效行在 9 平方行上有 1-9)

我已经盯着我的逻辑 30 分钟了,不明白为什么我知道有效的板一直吐出无效。

为了让它更容易阅读,这里是有问题的部分...

void* checkRow(void* p){

  int check[9] = {0};
  parameters* temp = (parameters*) p;
  int tempRow = temp -> row;
  int tempCol = temp -> col;

  for (int i = 0; i < SIZE; i++){ // looping to find each # 1-9

    for (int j = 0; j < SIZE; j++){

      if (board[i][j] == 1)
          check[0] = 1;
      if (board[i][j] == 2)
          check[1] = 1;
      if (board[i][j] == 3)
          check[2] = 1;
      if (board[i][j] == 4)
          check[3] = 1;
      if (board[i][j] == 5)
          check[4] = 1;
      if (board[i][j] == 6)
          check[5] = 1;
      if (board[i][j] == 7)
          check[6] = 1;
      if (board[i][j] == 8)
          check[7] = 1;
      if (board[i][j] == 9) // changing value to 1 if found
          check[8] = 1;

      int k = 0;
      while(k < 9){
          if (check[k] == 0){
          printf("invalid solution"); // should only print if 1-9 isn't found right?
          exit(0);
          }
      k++;
      }

      memset(check, 0, sizeof(check)); // resetting array to zero

    }

  }

}

这里都有,以防万一。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>

#define SIZE 9

typedef struct{ // referenced from assignment page
  int row;
  int col;
} parameters;

int board[SIZE][SIZE]; // global variable holding board

pthread_t tRow, tCol, t1, t2, t3, t4, t5, t6, t7, t8, t9;

void setThreads();
void* checkRow(void* p);

int main(int argc, char **argv){ // referenced project 1. Supplies command line input for file

  FILE *fp;

  fp = fopen(argv[1], "r");

  if (fp == NULL) // validity check
    exit(1);

  for (int i = 0; i < SIZE; i++){
      for(int j = 0; j < SIZE; j++){
          fscanf(fp, "%d", &board[i][j]);
      }
  }

  setThreads();
  printf("rows check out");

  return 0;

}

void setThreads(){

  parameters *rowcolparameter = (parameters *) malloc(sizeof(parameters));
  rowcolparameter -> row = 0;
  rowcolparameter -> col = 0;

  parameters *square1 = (parameters *) malloc(sizeof(parameters));
  square1 -> row = 0;
  square1 -> col = 0;

  parameters *square2 = (parameters *) malloc(sizeof(parameters));
  square2 -> row = 0;
  square2 -> col = 3;

  parameters *square3 = (parameters *) malloc(sizeof(parameters));
  square3 -> row = 0;
  square3 -> col = 6;

  parameters *square4 = (parameters *) malloc(sizeof(parameters));
  square4 -> row = 3;
  square4 -> col = 0;

  parameters *square5 = (parameters *) malloc(sizeof(parameters));
  square5 -> row = 3;
  square5 -> col = 3;

  parameters *square6 = (parameters *) malloc(sizeof(parameters));
  square6 -> row = 3;
  square6 -> col = 6;

  parameters *square7 = (parameters *) malloc(sizeof(parameters));
  square7 -> row = 6;
  square7 -> col = 0;

  parameters *square8 = (parameters *) malloc(sizeof(parameters));
  square8 -> row = 6;
  square8 -> col = 3;

  parameters *square9 = (parameters *) malloc(sizeof(parameters));
  square9 -> row = 6;
  square9 -> col = 6;

  pthread_create(&tRow, NULL, checkRow, rowcolparameter);

  pthread_join(tRow, NULL);

}

void* checkRow(void* p){

  int check[9] = {0};
  parameters* temp = (parameters*) p;
  int tempRow = temp -> row;
  int tempCol = temp -> col;

  for (int i = 0; i < SIZE; i++){ // looping to find each # 1-9

    for (int j = 0; j < SIZE; j++){

      if (board[i][j] == 1)
          check[0] = 1;
      if (board[i][j] == 2)
          check[1] = 1;
      if (board[i][j] == 3)
          check[2] = 1;
      if (board[i][j] == 4)
          check[3] = 1;
      if (board[i][j] == 5)
          check[4] = 1;
      if (board[i][j] == 6)
          check[5] = 1;
      if (board[i][j] == 7)
          check[6] = 1;
      if (board[i][j] == 8)
          check[7] = 1;
      if (board[i][j] == 9)
          check[8] = 1;

    int k = 0;
    while(k < 9){
    if (check[k] == 0){
      printf("invalid solution"); // it should only say invalid if 1-9 wasn't found right?
      exit(0);
    }
    k++;
      }

      memset(check, 0, sizeof(check)); // resetting array to 0

    }

  }

}

谢谢。

看来 "check part" 是 inside 内循环!不应该是outside内循环吗。喜欢:

  for (int i = 0; i < SIZE; i++){ // looping to find each # 1-9

    for (int j = 0; j < SIZE; j++){

      if (board[i][j] == 1)
          check[0] = 1;
      if (board[i][j] == 2)
          check[1] = 1;
      if (board[i][j] == 3)
          check[2] = 1;
      if (board[i][j] == 4)
          check[3] = 1;
      if (board[i][j] == 5)
          check[4] = 1;
      if (board[i][j] == 6)
          check[5] = 1;
      if (board[i][j] == 7)
          check[6] = 1;
      if (board[i][j] == 8)
          check[7] = 1;
      if (board[i][j] == 9) // changing value to 1 if found
          check[8] = 1;
    }  // End the inner loop

    // Now do the check
    int k = 0;
    while(k < 9){
       if (check[k] == 0){
         printf("invalid solution"); // should only print if 1-9 isn't found right?
         exit(0);
       }
       k++;
    }

    memset(check, 0, sizeof(check)); // resetting array to zero

  }

顺便说一句:你可以像这样减少很多内循环代码:

  // inner loop
  for (int j = 0; j < SIZE; j++){
      if (board[i][j] < 1 || board[i][j] > 9) exit(1); // Illegal board value

      check[board[i][j] - 1] = 1;  // Mark value as found
  }