Logi c check не выводит как положено. (Судоку чекер) C - PullRequest
0 голосов
/ 11 февраля 2020

У меня есть функция, которая проверяет все строки на доске судоку, выводит и выходит, если она недействительна.

(допустимая строка имеет 1-9 в строке 9 квадратов)

I я смотрю на мою логику c в течение 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

    }

  }

}

Спасибо.

1 Ответ

0 голосов
/ 11 февраля 2020

Кажется, что "проверочная часть" - это внутри внутри l oop! Разве это не должно быть снаружи внутреннего l oop. Например:

  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

  }

Кстати: вы можете уменьшить внутренний код l oop примерно так:

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