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

问题描述 投票:0回答:1

我具有检查数独板所有行并在无效时输出和退出的功能。

(有效行在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

    }

  }

}

谢谢。

c multithreading sudoku
1个回答
0
投票

似乎“检查部分”是内部内循环!它不应该是[[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 }
BTW:您可以像这样减少很多内循环代码:

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

© www.soinside.com 2019 - 2024. All rights reserved.