Uploaded by Ismail Amr

Lab1

advertisement
Ain Shams University – Faculty of Engineering
Senior2 - Computers and Systems
CSE 332: Design and Analysis of Algorithms
LAB 1
Name: Ismail Amr Ismail Hamza
ID: 1806072
Q1- Place n queens on an n × n chessboard so that no two queens attack each other by being in the
same column, row, or diagonal.
Pseudo Code
We solve this problem by backtracking as we start with one possible move out of many available
moves. We then try to solve the problem. If we can solve the problem with the selected move, then
we will print the solution. Else we will back­track and select some other move and try to solve it.
We start by placing a queen in a position and trying to rule out the possibility of it being under attack.
We place one queen in each row/column. If we see that the queen is under attack at its chosen
position, we try the next position.
If a queen is under attack at all the positions in a row, we backtrack and change the position of the
queen placed prior to the current position.
We repeat this process of placing a queen and backtracking until all the N queens are placed
successfully.
to check if the position of the queen is safe,
bool isSafe(int board[N][N], int row, int col)
{
Int i,j;
for (i = 0; i < col; i++)
if (board[row][i]==1) // if there exists a queen on the same row
return false;
for (i = row, j = col; j >= 0 && i < N; i++, j­­) //if there exists a queen on the same diagonal
if (board[i][j])
return false;
1
for (i = row, j = col; i >= 0 && j >= 0; i­­, j­­) //if there exists a queen on the other diagonal
if (board[i][j])
return false;
return true; // the position is safe
Implementation of the Algorithm
// AlgorithmLabs.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#define N 4
#include <iostream>
using namespace std;
//function to print the solution
void printSolution(int board[N][N])
{
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++)
cout << board[i][j];
cout<<"\n";
}
}
// function to check whether the position is safe or not
bool isSafe(int board[N][N], int row, int col)
{
int i, j;
for (i = 0; i < col; i++)
if (board[row][i])
return false;
for (i = row, j = col; i >= 0 && j >= 0; i--, j--)
if (board[i][j])
return false;
for (i = row, j = col; j >= 0 && i < N; i++, j--)
if (board[i][j])
return false;
return true;
}
2
// The function that solves the problem using backtracking
bool solveNQueen(int board[N][N], int col)
{
if (col >= N)
return true;
for (int i = 0; i < N; i++) {
//if it is safe to place the queen at position i,col -> place it
if (isSafe(board, i, col)) {
board[i][col] = 1;
if (solveNQueen(board, col + 1))
return true;
//backtrack if the above condition is false
board[i][col] = 0; // BACKTRACK
}
}
return false;
}
int main()
{
int board[N][N] = { { 0, 0, 0, 0 },
{ 0, 0, 0, 0 },
{ 0, 0, 0, 0 },
{ 0, 0, 0, 0 } };
if (solveNQueen(board, 0) == false) {
cout << "Solution does not exist" << endl;
return 0;
}
printSolution(board);
return true;
return 0;
}
3
Time Complexity: O(N!)
Code Output
4
Download