-5
这个数独求解程序正在编译,但得到错误“分段错误”,请帮助我解决这个问题,并澄清为什么我得到了分割错误,因为我已经写了每一件事情,请事先提问数独使用类C++解决
#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;
}
显然,你没有写的一切:
取出
int
关键字,而不是使用声明的新变量的参数。你尝试调试吗?还要修复缩进。代码不可读。 – khajvah 2015-02-06 11:54:23这不是你应该在这里提问的方式。阅读[this](http://stackoverflow.com/help/mcve)寻求帮助。 – emlai 2015-02-06 11:56:08
你有输出吗?你做了一些调试吗?请说明你已经做出了什么努力来解决问题,然后有人可以帮助你 – xmoex 2015-02-06 11:56:09