Skip to content

Instantly share code, notes, and snippets.

@karthikkondagalla
Created August 27, 2017 05:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karthikkondagalla/90b871583acd80bd747257e35580aaa3 to your computer and use it in GitHub Desktop.
Save karthikkondagalla/90b871583acd80bd747257e35580aaa3 to your computer and use it in GitHub Desktop.
N Queens Program in C++
#include "nqueens.h"
int main()
{
int N;
cout<<"Enter N value :";
while(cin>>N)
{
solveNQueens(N);//function call for the solveNQueens function.
}
return(0);
}
/****************************************************************
FUNCTION: solveNqueens
ARGUMENTS: const unsigned& n
RETURNS: Nothing
NOTES: This function first initiates the board with value false and then checks wheather the solution exists or not.
****************************************************************/
void solveNQueens(const unsigned& n)
{
vector<vector<bool>> board(n, vector<bool>(n));
initBoard(board);//function call for the initBoard function
srand(time(NULL));
cout<<"Size of the chessboard :"<<board.size()<<" x "<<board.size()<<endl;
if(solveNQueensUtil(board, 0, 0))//function call for the solveNQueensUtil function.
{
printBoard(board);//function call for the printBoard function.
}
else
{
cout<<"No solution exists "<<endl;
}
}
/****************************************************************
FUNCTION: solveNQueensUtil
ARGUMENTS: vector<vector<bool>>& board, const unsigned& row,unsigned int col.
RETURNS: boolean value i.e true or false
NOTES: This function places the queens in correct position if at all they exist.
****************************************************************/
bool solveNQueensUtil(vector<vector<bool>>& board, const unsigned& row, unsigned int col)
{
if(row >= board.size()) return true;//If all queens have been successfully placed then return true.
for(unsigned i=0;i<board.size();i++)
{
if(i == 0)
{
if(isSafe(board, row, col))//function call for the isSafe function.
{
board[row][col] = true;
col = RNG(0, board.size()-1);////function call for the RNG function.
if(solveNQueensUtil(board, row+1, col))
return true;
board[row][col] = false;//Backtracking if solution not found.
}
}
if(i != col)
{
if(isSafe(board, row, i))
{
board[row][i] = true;
col = RNG(0, board.size()-1);
if(solveNQueensUtil(board, row+1, col))
return true;
board[row][i] = false;
}
}
}
return false;
}
/****************************************************************
FUNCTION: isSafe
ARGUMENTS: const vector<vector<bool>>& board, const int& row, const int& col
RETURNS: boolean value
NOTES: This function checks if the queen can be placed in that particular place or not.
****************************************************************/
bool isSafe(const vector<vector<bool>>& board, const int& row, const int& col)
{
for(unsigned i=0;i<(unsigned)row;i++)
{
for(unsigned j=0;j<board.size();j++)
{
if(board[i][j] == true)
{
if(abs(row-(int)i) == abs(col-(int)j))
return false;
}
}
if(board[i][col] == true) return false;
}
return true;
}
/****************************************************************
FUNCTION: printBoard
ARGUMENTS: const vector<vector<bool>>& board
RETURNS: None
NOTES: This function prints the 2D vector.
****************************************************************/
void printBoard(const vector<vector<bool>>& board)
{
for(unsigned i=0;i<board.size();i++)
{
for(unsigned j=0;j<board.size();j++)
{
if(board[i][j] == true)
{
cout<<" Q ";
}
else cout<<" . ";
}
cout<<endl;
}
}
/****************************************************************
FUNCTION: RNG
ARGUMENTS: int low, int high
RETURNS: int
NOTES: This function generates a random number between to given values.
****************************************************************/
int RNG(int low, int high)
{
int r=(rand()%(high-low+1))+low;
return r;
}
/****************************************************************
FUNCTION: initBoard
ARGUMENTS: vector<vector<bool>>& board
RETURNS: None
NOTES: This function initiates the 2D vector.
****************************************************************/
void initBoard(vector<vector<bool>>& board)
{
for(unsigned i=0;i<board.size();i++)
{
for(unsigned j=0;j<board.size();j++)
{
board[i][j] = false;
}
}
}
#ifndef H_NQUEENS
#define H_NQUEENS
#include "/home/cs689/common/689.h"
#endif
bool solveNQueensUtil(vector<vector<bool>>& board, const unsigned& row, unsigned int col);
bool isSafe(const vector<vector<bool>>& board, const int& row, const int& col);
void solveNQueens(const unsigned& n);
void initBoard(vector<vector<bool>>& board);
void printBoard(const vector<vector<bool>>& board);
int RNG(int low, int high);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment