C++数独求解器

C++ Sudoku Solver

本文关键字:C++      更新时间:2023-10-16

需要帮助找出readPuzzleprintPuzzle函数的问题。 SetOfSmallInts是包含这些数字{1,2,3,4,5,6,7,8,9}或任意组合的集合。 singletonSet(s)集合中存储单个数字 s。无论我在程序上运行什么输入,它都只会输出 1。即使所有输入都是"-",输出也是 81 个 1。有什么建议吗?主题中的拼图类型组成一个SetofSmallInts [9][9]数组。

//==============================================================
//                      readPuzzle
//==============================================================
// Reads in puzzle p from the standard input.
//==============================================================
void readPuzzle(Puzzle p)
{
   int i, j;
   SetOfSmallInts s;
   s = rangeSet(1, 9);
   char n;
   for(i = 0; i < 9; i++)
   {
      for(j = 0; j < 9; j++) 
      {
       n = scanf("%c", &n);  
       if (n == '-')
       {
         p[i][j]= s;
       }
       else if(n==1 || n==2 || n==3 || n==4 || n==5 || n==6 ||
               n==7 || n==8 || n==9)
      {
         p[i][j]= singletonSet(n);
      }
      }
   }      
}

//==============================================================
//                      printPuzzle
//==============================================================
// Prints in puzzle p.
//==============================================================
void printPuzzle(Puzzle p)
{
   int i, j, s;
   SetOfSmallInts x;
   for(i = 0; i < 9; i++)
   {
      for(j = 0; j < 9; j++)
      {
         x = p[i][j];
         if(isEmpty(x)) 
         {
            printf("%i ", 0);
         }
         else if(isSingleton(x)) //returns true if x hold Singleton set
         {
            s = smallest(x); // returns the smallest member of s
            printf("%i ", s);
         }
         else
         {
            printf("%c ", '-');
         }
      }
   }
}  
//==============================================================
//                      showPuzzle
//==============================================================
// Shows in puzzle p in a format that can be used for debugging
//==============================================================

//==============================================================
//                   main
//==============================================================
int main(int argc, char** argv)
{
   Puzzle p;
   readPuzzle(p);
   printPuzzle(p);
   return 0;
}

n = scanf("%c", &n);

scanf 返回扫描的项目数。 你只是想要

scanf("%c", &n);

你只需要scanf("%c", &n)

N也是一个字符

所以

else if(n==1 || n==2 || n==3 || n==4 || n==5 || n==6 ||
               n==7 || n==8 || n==9)

需要做n == '1'

else if( n >= '1' && n <= '9')

那么你可能需要

p[i][j]= singletonSet(n - '0');
相关文章:
  • 没有找到相关文章