使用类在c++中求解数独

sudoku solving in c++ using class

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

这个数独解决程序正在编译中,但出现错误"分割错误"请帮助我解决这个问题,并澄清为什么我会出现分割错误,因为我已经把每件事都写得很好。提前感谢

#include<iostream>
#include<math.h>
class sudoku
{
public:
    sudoku();
    void initializeSudokuGrid();
    void printSudokuGrid();
    bool solveSudoku();
    bool findEmptyGridSlot(int &row, int &col);
    bool canPlaceNum(int row, int col, int num);
    bool numAlreadyInRow(int row, int num);
    bool numAlreadyInCol(int col, int num);
    bool numAlreadyInBox(int smallGridRow, int smallGridCol, int num);
    int grid[9][9];
};
sudoku::sudoku()
{
    for (int i = 0; i < 9; i++)
    {
        for (int j = 0; j < 9; j++)
        {
            grid[i][j] = 0;
        }
    }
    std::cout << "n all the grid locations are initialise to zero";
}
void sudoku::initializeSudokuGrid()
{
    char x = 'y';
    while (x == 'y')
    {
        int row, col, var;
        std::cout
                << "n enter the row,column and integer in the box(that is 1-9 numbers n";
        std::cin >> row;
        std::cin >> col;
        std::cin >> var;
        grid[row][col] = var;
        std::cout
                << "n are there any slots that u want to enter the numbers into the boxs enter y else enter n n";
        std::cin >> x;
    }
}
void sudoku::printSudokuGrid()
{
    std::cout << "n";
    for (int i = 0; i < 9; i++)
    {
        for (int j = 0; j < 9; j++)
        {
            std::cout << grid[i][j] << " ";
        }
        std::cout << "n";
    }
}
bool sudoku::solveSudoku()
{
    int row, col;
    if (findEmptyGridSlot(row, col))
    {
        for (int num = 1; num <= 9; num++)
        {
            if (canPlaceNum(row, col, num))
            {
                grid[row][col] = num;
                if (solveSudoku()) //recursive call
                    return true;
                grid[row][col] = 0;
            }
        }
        return false; //backtrack
    }
    else
        return true; //there are no empty slots
}
bool sudoku::numAlreadyInRow(int row, int num)
{
    for (int i = 0; i < 9; i++)
    {
        if (num != 0 && grid[row][i] == num)
            return true;
    }
    return false;
}
bool sudoku::numAlreadyInCol(int col, int num)
{
    for (int i = 0; i < 9; i++)
    {
        if (num != 0 && grid[i][col] == num)
            return true;
    }
    return false;
}
bool sudoku::canPlaceNum(int row, int col, int num)
{
    if (!numAlreadyInRow(row, num))
    {
        if (!numAlreadyInCol(col, num))
        {
            int smallGridRow = row - row % 3;
            int smallGridCol = col - col % 3;
            if (!numAlreadyInBox(smallGridRow, smallGridCol, num))
            {
                return true;
            }
        }
    }
    return false;
}
bool sudoku::numAlreadyInBox(int smallGridRow, int smallGridCol, int num)
{
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            if (grid[i + smallGridRow][j + smallGridCol] == num)
                return true;
        }
    }
    return false;
}
bool sudoku::findEmptyGridSlot(int &row, int &col)
{
    for (int row = 0; row < 9; row++)
    {
        for (int col = 0; col < 9; col++)
        {
            if (grid[row][col] == 0)
                return true;
        }
    }
    return false;
}
int main()
{
    sudoku s;
    s.printSudokuGrid();
    s.initializeSudokuGrid();
    s.printSudokuGrid();
    std::cout << "n after solving the problem n";
    if (s.solveSudoku())
        s.printSudokuGrid();
    else
        std::cout << "n solution doesnt exist for this type of solution n";
    return 0;
}

您的findEmptyGridSlot函数中有一个错误

bool sudoku::findEmptyGridSlot(int &row, int &col)
{
    for(int row=0;row<9;row++)
    {
        for(int col=0;col<9;col++)
        {
...

您在循环中声明了新的int变量rowcol,但您可能打算使用作为参数传递的变量。

删除int关键字以使用参数,而不是声明新变量:

for(row=0;row<9;row++)
{
    for(col=0;col<9;col++)
    {
相关文章:
  • 没有找到相关文章