plz some one explain in details this code :
we have array of 9 cells and 0 present empty locations we want solution of this array (no number will be repeated)
also if there is any way to call this functions in main so as the program runs succesfully plz tell me
one more thing plz is it illegal that function return true or false (what i knew is no thing can be performed after return statement )
Here is the code :
#include <iostream>
using namespace std;
#define empty 0
#define size 9
int r;
bool isEmptyLocation(int grid[size] , int &r);
bool CheckRow(int grid[size], int r, int n);
bool Solution(int grid[size])
{
if (isEmptyLocation(grid, r)==false)
return true;
for (int n = 1; n <= 9; n++)
{
if (CheckRow(grid, r, n)==false)
{
grid[r] = n;
if (Solution(grid))
return true;
}
}
return false;
}
bool isEmptyLocation(int grid[size], int &r)
{
for (r = 0; r < size; r++)
if (grid[r] == empty)
return true ;
return false ;
}
bool CheckRow(int grid[size], int r, int n)
{
for (int r = 0; r < size; r++)
if (grid[r] == n)
return true;
return false;
}
void print(int grid[size])
{
for (int r = 0; r < size; r++)
{
cout<<grid[r]<<" ";
cout<<endl;
}
}
int main()
{
int grid[size] = {0, 0, 0, 5, 0, 0, 0, 0, 0};
if (Solution(grid) == true)
print(grid);
else
cout<<"No solution exists"<<endl;
system("pause");
return 0;
}