Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 74 additions & 74 deletions N-Queen-Prob.cpp → C ++/N-Queen-Prob.cpp
Original file line number Diff line number Diff line change
@@ -1,75 +1,75 @@
// PLace N queens on n x n size chessboard in such a manner so that no queen attack each other
#include<iostream>
using namespace std;
bool isSafe(int** arr, int x, int y, int n){
for(int row=0; row<x; row++){
if(arr[row][y] == 1)
return false;
}
int row=x;
int col=y;
while(row>=0 && col>=0){
if(arr[row][col] == 1)
return false;
row--;
col--;
}
row=x;
col=y;
while(row>=0 && col<n){
if(arr[row][col] == 1)
return false;
row--;
col++;
}
return true;
}
bool nQueen(int** arr, int x, int n){
if(x>=n) // Base Condition
return true;
for(int col=0; col<n; col++){
if(isSafe(arr,x,col,n)){
arr[x][col] = 1;
if(nQueen(arr,x+1,n)){
return true;
}
arr[x][col] = 0; // Backtracking
}
}
return false;
}
int main()
{
int n;
cin >>n;
int** arr=new int*[n];
for(int i=0; i<n; i++){
arr[i]=new int[n];
for(int j=0; j<n; j++)
arr[i][j] = 0;
}
if(nQueen(arr,0,n)){
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
cout<<arr[i][j]<<" ";
}cout<<endl;
}
}
return 0;
// PLace N queens on n x n size chessboard in such a manner so that no queen attack each other
#include<iostream>
using namespace std;

bool isSafe(int** arr, int x, int y, int n){

for(int row=0; row<x; row++){
if(arr[row][y] == 1)
return false;
}

int row=x;
int col=y;
while(row>=0 && col>=0){
if(arr[row][col] == 1)
return false;
row--;
col--;
}

row=x;
col=y;
while(row>=0 && col<n){
if(arr[row][col] == 1)
return false;
row--;
col++;
}

return true;
}

bool nQueen(int** arr, int x, int n){

if(x>=n) // Base Condition
return true;

for(int col=0; col<n; col++){
if(isSafe(arr,x,col,n)){
arr[x][col] = 1;

if(nQueen(arr,x+1,n)){
return true;
}

arr[x][col] = 0; // Backtracking
}
}

return false;
}

int main()
{
int n;
cin >>n;

int** arr=new int*[n];
for(int i=0; i<n; i++){
arr[i]=new int[n];
for(int j=0; j<n; j++)
arr[i][j] = 0;
}

if(nQueen(arr,0,n)){
for(int i=0; i<n; i++){

for(int j=0; j<n; j++){
cout<<arr[i][j]<<" ";
}cout<<endl;
}
}

return 0;
}
190 changes: 95 additions & 95 deletions Rat-in-Maze.cpp → C ++/Rat-in-Maze.cpp
Original file line number Diff line number Diff line change
@@ -1,96 +1,96 @@
// Backtracking is an algorithmic-technique for solving recursive problems by trying to built every
// every possible solution incrementally and removing those solutions that fail to satilfy the contraints
// of propblem at any point of time
#include <iostream>
using namespace std;
// x and y are position coordinates
// n is the size of the array/maze
// int** arr for dynamic array
bool isSafe(int **arr, int x, int y, int n)
{
if (x < n && y < n && arr[x][y] == 1)
{
return true;
}
return false;
}
bool ratinMaze(int **arr, int x, int y, int n, int **solArr)
{
if (x == n - 1 && y == n - 1)
{
solArr[x][y] = 1;
return true;
}
if (isSafe(arr, x, y, n))
{
solArr[x][y] = 1;
if (ratinMaze(arr, x + 1, y, n, solArr))
{
return true;
}
if (ratinMaze(arr, x, y + 1, n, solArr))
{
return true;
}
solArr[x][y] = 0; // Backtracking
return 0;
}
return false;
}
int main()
{
int n;
cin >> n;
// Memory allocation of row of 1D array through dynamic
int **arr = new int *[n]; // Dynamic Array
for (int i = 0; i < n; i++)
{
arr[i] = new int[n];
}
// Input of 2D array
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cin >> arr[i][j];
}
}
// Dynamic Memory allocation for solution array
int **solArr = new int *[n]; // Dynamic Array
for (int i = 0; i < n; i++)
{
solArr[i] = new int[n];
for (int j = 0; j < n; j++)
{
solArr[i][j] = 0;
}
}
if (ratinMaze(arr, 0, 0, n, solArr))
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cout << solArr[i][j]<<" ";
}cout <<endl;
}
}
return 0;
}
// 1 0 1 0 1
// 1 1 1 1 1
// 0 1 0 1 0
// 1 0 0 1 1
// Backtracking is an algorithmic-technique for solving recursive problems by trying to built every
// every possible solution incrementally and removing those solutions that fail to satilfy the contraints
// of propblem at any point of time

#include <iostream>
using namespace std;

// x and y are position coordinates
// n is the size of the array/maze
// int** arr for dynamic array
bool isSafe(int **arr, int x, int y, int n)
{
if (x < n && y < n && arr[x][y] == 1)
{
return true;
}
return false;
}

bool ratinMaze(int **arr, int x, int y, int n, int **solArr)
{

if (x == n - 1 && y == n - 1)
{
solArr[x][y] = 1;
return true;
}

if (isSafe(arr, x, y, n))
{
solArr[x][y] = 1;
if (ratinMaze(arr, x + 1, y, n, solArr))
{
return true;
}
if (ratinMaze(arr, x, y + 1, n, solArr))
{
return true;
}
solArr[x][y] = 0; // Backtracking
return 0;
}
return false;
}

int main()
{
int n;
cin >> n;
// Memory allocation of row of 1D array through dynamic
int **arr = new int *[n]; // Dynamic Array
for (int i = 0; i < n; i++)
{
arr[i] = new int[n];
}

// Input of 2D array
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cin >> arr[i][j];
}
}

// Dynamic Memory allocation for solution array
int **solArr = new int *[n]; // Dynamic Array
for (int i = 0; i < n; i++)
{
solArr[i] = new int[n];

for (int j = 0; j < n; j++)
{
solArr[i][j] = 0;
}
}

if (ratinMaze(arr, 0, 0, n, solArr))
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cout << solArr[i][j]<<" ";
}cout <<endl;
}
}

return 0;
}

// 1 0 1 0 1
// 1 1 1 1 1
// 0 1 0 1 0
// 1 0 0 1 1
// 1 1 1 0 1
1 change: 0 additions & 1 deletion Todolist-React
Submodule Todolist-React deleted from 2fb4e6
1 change: 0 additions & 1 deletion University
Submodule University deleted from b7b7f7
Loading