【问题标题】:Abort Trap Error: 6 - where is it coming from? - Sudoku solver中止陷阱错误:6 - 它来自哪里? - 数独求解器
【发布时间】:2018-02-22 19:46:00
【问题描述】:

有一个数独求解器方法,可以在测试数字是否不正确后检查行/列位置的解和回溯。它正在打印出No Solution,然后是Abort Trap Error: 6。我正在使用一个应该返回 TRUE 的测试用例。 Row 和 Col 最初作为 0 传入参数

可能是什么问题?我想这可能是我增加行和列的方式。它读取单行文件并转换为 2D 网格

这里是测试输入:4.....8.5.3...........7......2.....6.....8.4... ...1.......6.3.7.5..2......1.4......

#include <stdio.h>
#include <stdlib.h>
#define BLANK '.'
 #define TRUE 1
#define FALSE 0
#define BLANK_SPACE '.'



int findBlankSpace(int grid2D[9][9]);
 int valid(int grid2D[9][9], int rowIndex, int colIndex, int num);

 void prettyPrint(int grid2D[9][9]);
 int readLine(int grid2D[9][9]);
 int findSolution(int grid2D[9][9],int row, int col);
 int UsedInRow(int grid2D[9][9], int row, int num);
 int UsedInCol(int grid2D[9][9], int col, int num);
 int UsedInBox(int grid2D[9][9], int boxStartRow, int boxStartCol, int num);

int findSolution(int grid2D[9][9], int row, int col)
{

  row = 0;

  if (findBlankSpace(grid2D) == FALSE)
    return TRUE;

  if (row == 9) {
    row = 0;
  }

  for (int num = 1; num <= 9; num++)
  {
    if (valid(grid2D, row, col, num))
    {
        grid2D[row][col] = num;

        if (findSolution(grid2D, row, ++col))
        {
            return TRUE;
        }
    }

    grid2D[row][col] = 0;
    row++;
   }
  return FALSE;
}

int findBlankSpace(int grid2D[9][9])
{
  int row;
  int col;
     for (row = 0; row < 9; row++)
       for (col = 0; col < 9; col++)
          if (grid2D[row][col] == 0)
            return TRUE;
    return FALSE;
}
int UsedInRow(int grid2D[9][9], int row, int num)
{
for (int col = 0; col < 9; col++)
    if (grid2D[row][col] == num)
        return TRUE;
return FALSE;
}


 int UsedInCol(int grid2D[9][9], int col, int num)
{
   for (int row = 0; row < 9; row++)
      if (grid2D[row][col] == num)
          return TRUE;
    return FALSE;
 }

 int UsedInBox(int grid2D[9][9], int row1, int col1, int    num)
 {
    for (int row = 0; row < 3; row++)
      for (int col = 0; col < 3; col++)
          if (grid2D[row+row1][col+col1] == num)
              return TRUE;
    return FALSE;
   }

int valid(int grid2D[9][9], int row, int col, int num)
{
return !UsedInRow(grid2D, row, num) &&
       !UsedInCol(grid2D, col, num) &&
       !UsedInBox(grid2D, row - row % 3, col - col % 3, num); 

}

int readLine(int grid2D[9][9])
{
    int c;
    int row = 0;
    int col = 0;

    while((c = getchar()) != EOF)
    {
       if(c == '.')
       {
           grid2D[row][col] = 0;
       } 
       else
       {
           grid2D[row][col] = c - '0';
           col++;
       }

       if(col%9 == 0)
       {
           row++;
           col = 0;
       }
  }

return 0;
 }
 void prettyPrint(int grid2D[9][9])
{
int count = 0;
int row;
int col;

printf("solution:\n");
/* Use nested for-loops to iterate through 2-D array */
for(row = 1; row <= 9; row++)
{
    for(col = 1; col<=9; col++)
    {
        /* After every 3rd character, print out "|", forming the board   */
        if((col%3==0)&&(col%9!=0))
        {
            printf("%d| ", grid2D[row - 1][col - 1]);
        }
        else
            printf("%d ", grid2D[row - 1][col - 1]);
        count++;
    }
    /* After every 3rd row, print out horizontal separations */
    if((row%3==0)&&(row!=9))
    {
        /* Advance to the next line prior to printing separation to give space */
        printf("\n");
        printf("------+-------+------");
      }
      printf("\n");
   }
}

int main()
{

   int grid2D[9][9];

   readLine(grid2D);

   if(findSolution(grid2D, 0, 0)==TRUE)
   {
    prettyPrint(grid2D);
   }
   else
   {
    printf("No Solution Found!\n");
   }

return 0;
}

【问题讨论】:

  • @EugeneSh。我刚刚添加了主要方法。如果findSolution() 为真,它只会打印出答案
  • readline 函数在看到 col 时不会增加 col 而且您可能应该验证您确实完成了填充网格,并且没有运行超过网格。换句话说,请确保您获得了准确的 81 个字符。验证这些数字是否真的是数字不会有什么坏处。
  • 您的输入示例是 82 个字符长,而不是 91 个。
  • 是的,现在您需要回到上一个问题中的代码。除了不要使用BLANK_SPACE 宏,因为您使用0 表示空格,而不是.
  • 谢谢@user3386109!看起来修复了它。它只是搞砸了几个数字,我会调查其余的

标签: c algorithm recursion backtracking sudoku


【解决方案1】:

在递归函数中:findSolution(),最终'col'参数将是9(或更大)!发生这种情况时,代码将访问数组中当前行的边界之外。这会导致未定义的行为,并可能导致段错误事件。

【讨论】:

    猜你喜欢
    • 2014-12-13
    • 2017-05-11
    • 1970-01-01
    • 2019-03-25
    • 2016-10-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多