rename Backtracking -> backtracking (#654)

This commit is contained in:
Christian Clauss
2019-11-28 13:37:04 +01:00
committed by GitHub
parent f8da1b0685
commit ab12ed4a14
5 changed files with 0 additions and 0 deletions

View File

@@ -0,0 +1,81 @@
#include <stdio.h>
// Number of vertices in the graph
#define V 4
void printSolution(int color[]);
/* A utility function to check if the current color assignment
is safe for vertex v */
bool isSafe(int v, bool graph[V][V], int color[], int c)
{
for (int i = 0; i < V; i++)
if (graph[v][i] && c == color[i])
return false;
return true;
}
/* A recursive utility function to solve m coloring problem */
void graphColoring(bool graph[V][V], int m, int color[], int v)
{
/* base case: If all vertices are assigned a color then
return true */
if (v == V)
{
printSolution(color);
return;
}
/* Consider this vertex v and try different colors */
for (int c = 1; c <= m; c++)
{
/* Check if assignment of color c to v is fine*/
if (isSafe(v, graph, color, c))
{
color[v] = c;
/* recur to assign colors to rest of the vertices */
graphColoring(graph, m, color, v + 1);
/* If assigning color c doesn't lead to a solution
then remove it */
color[v] = 0;
}
}
}
/* A utility function to print solution */
void printSolution(int color[])
{
printf(" Following are the assigned colors \n");
for (int i = 0; i < V; i++)
printf(" %d ", color[i]);
printf("\n");
}
// driver program to test above function
int main()
{
/* Create following graph and test whether it is 3 colorable
(3)---(2)
| / |
| / |
| / |
(0)---(1)
*/
bool graph[V][V] = {
{0, 1, 1, 1},
{1, 0, 1, 0},
{1, 1, 0, 1},
{1, 0, 1, 0},
};
int m = 3; // Number of colors
int color[V];
for (int i = 0; i < V; i++)
color[i] = 0;
graphColoring(graph, m, color, 0);
return 0;
}

View File

@@ -0,0 +1,68 @@
#include<bits/stdc++.h>
# define n 8
/**
A knight's tour is a sequence of moves of a knight on a chessboard
such that the knight visits every square only once. If the knight
ends on a square that is one knight's move from the beginning
square (so that it could tour the board again immediately, following
the same path), the tour is closed; otherwise, it is open.
**/
using namespace std;
bool issafe(int x,int y,int sol[n][n])
{
return (x<n && x>=0 && y<n && y>=0 && sol[x][y]==-1);
}
bool solve(int x,int y, int mov, int sol[n][n], int xmov[n], int ymov[n])
{
int k,xnext,ynext;
if(mov == n*n)
return true;
for(k=0;k<8;k++)
{
xnext=x+xmov[k];
ynext=y+ymov[k];
if(issafe(xnext,ynext,sol))
{
sol[xnext][ynext]=mov;
if(solve(xnext,ynext,mov+1,sol,xmov,ymov)==true)
return true;
else
sol[xnext][ynext]=-1;
}
}
return false;
}
int main()
{
//initialize();
int sol[n][n];
int i,j;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
sol[i][j]=-1;
int xmov[8] = { 2, 1, -1, -2, -2, -1, 1, 2 };
int ymov[8] = { 1, 2, 2, 1, -1, -2, -2, -1 };
sol[0][0]=0;
bool flag=solve(0,0,1,sol,xmov,ymov);
if(flag==false)
cout<<"solution doesnot exist \n";
else
{
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
cout<<sol[i][j]<<" ";
cout<<"\n";
}
}
}

77
backtracking/n_queens.cpp Normal file
View File

@@ -0,0 +1,77 @@
#include <iostream>
#define N 4
using namespace std;
void printSolution(int board[N][N])
{
cout << "\n";
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
cout << "" << board[i][j];
cout << "\n";
}
}
bool isSafe(int board[N][N], int row, int col)
{
int i, j;
/* Check this row on left side */
for (i = 0; i < col; i++)
if (board[row][i])
return false;
/* Check upper diagonal on left side */
for (i = row, j = col; i >= 0 && j >= 0; i--, j--)
if (board[i][j])
return false;
/* Check lower diagonal on left side */
for (i = row, j = col; j >= 0 && i < N; i++, j--)
if (board[i][j])
return false;
return true;
}
void solveNQ(int board[N][N], int col)
{
if (col >= N)
{
printSolution(board);
return;
}
/* Consider this column and try placing
this queen in all rows one by one */
for (int i = 0; i < N; i++)
{
/* Check if queen can be placed on
board[i][col] */
if (isSafe(board, i, col))
{
/* Place this queen in board[i][col] */
// cout<<"\n"<<col<<"can place"<<i;
board[i][col] = 1;
/* recur to place rest of the queens */
solveNQ(board, col + 1);
board[i][col] = 0; // BACKTRACK
}
}
}
int main()
{
int board[N][N] = {{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}};
solveNQ(board, 0);
return 0;
}

73
backtracking/rat_maze.cpp Normal file
View File

@@ -0,0 +1,73 @@
/*
A Maze is given as N*N binary matrix of blocks where source block is the upper
left most block i.e., maze[0][0] and destination block is lower rightmost
block i.e., maze[N-1][N-1]. A rat starts from source and has to reach destination.
The rat can move only in two directions: forward and down. In the maze matrix,
0 means the block is dead end and 1 means the block can be used in the path
from source to destination.
*/
#include <iostream>
#define size 4
using namespace std;
int solveMaze(int currposrow, int currposcol, int maze[size][size], int soln[size][size])
{
if ((currposrow == size - 1) && (currposcol == size - 1))
{
soln[currposrow][currposcol] = 1;
for (int i = 0; i < size; ++i)
{
for (int j = 0; j < size; ++j)
{
cout << soln[i][j];
}
cout << endl;
}
return 1;
}
else
{
soln[currposrow][currposcol] = 1;
// if there exist a solution by moving one step ahead in a collumn
if ((currposcol < size - 1) && maze[currposrow][currposcol + 1] == 1 && solveMaze(currposrow, currposcol + 1, maze, soln))
{
return 1;
}
// if there exists a solution by moving one step ahead in a row
if ((currposrow < size - 1) && maze[currposrow + 1][currposcol] == 1 && solveMaze(currposrow + 1, currposcol, maze, soln))
{
return 1;
}
// the backtracking part
soln[currposrow][currposcol] = 0;
return 0;
}
}
int main(int argc, char const *argv[])
{
int maze[size][size] = {
{1, 0, 1, 0},
{1, 0, 1, 1},
{1, 0, 0, 1},
{1, 1, 1, 1}};
int soln[size][size];
for (int i = 0; i < size; ++i)
{
for (int j = 0; j < size; ++j)
{
soln[i][j] = 0;
}
}
int currposrow = 0;
int currposcol = 0;
solveMaze(currposrow, currposcol, maze, soln);
return 0;
}

View File

@@ -0,0 +1,117 @@
#include <iostream>
using namespace std;
///N=9;
int n = 9;
bool isPossible(int mat[][9], int i, int j, int no)
{
///Row or col nahin hona chahiye
for (int x = 0; x < n; x++)
{
if (mat[x][j] == no || mat[i][x] == no)
{
return false;
}
}
/// Subgrid mein nahi hona chahiye
int sx = (i / 3) * 3;
int sy = (j / 3) * 3;
for (int x = sx; x < sx + 3; x++)
{
for (int y = sy; y < sy + 3; y++)
{
if (mat[x][y] == no)
{
return false;
}
}
}
return true;
}
void printMat(int mat[][9])
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cout << mat[i][j] << " ";
if ((j + 1) % 3 == 0)
{
cout << '\t';
}
}
if ((i + 1) % 3 == 0)
{
cout << endl;
}
cout << endl;
}
}
bool solveSudoku(int mat[][9], int i, int j)
{
///Base Case
if (i == 9)
{
///Solve kr chuke hain for 9 rows already
printMat(mat);
return true;
}
///Crossed the last Cell in the row
if (j == 9)
{
return solveSudoku(mat, i + 1, 0);
}
///Blue Cell - Skip
if (mat[i][j] != 0)
{
return solveSudoku(mat, i, j + 1);
}
///White Cell
///Try to place every possible no
for (int no = 1; no <= 9; no++)
{
if (isPossible(mat, i, j, no))
{
///Place the no - assuming solution aa jayega
mat[i][j] = no;
bool aageKiSolveHui = solveSudoku(mat, i, j + 1);
if (aageKiSolveHui)
{
return true;
}
///Nahin solve hui
///loop will place the next no.
}
}
///Sare no try kr liey, kisi se bhi solve nahi hui
mat[i][j] = 0;
return false;
}
int main()
{
int mat[9][9] =
{{5, 3, 0, 0, 7, 0, 0, 0, 0},
{6, 0, 0, 1, 9, 5, 0, 0, 0},
{0, 9, 8, 0, 0, 0, 0, 6, 0},
{8, 0, 0, 0, 6, 0, 0, 0, 3},
{4, 0, 0, 8, 0, 3, 0, 0, 1},
{7, 0, 0, 0, 2, 0, 0, 0, 6},
{0, 6, 0, 0, 0, 0, 2, 8, 0},
{0, 0, 0, 4, 1, 9, 0, 0, 5},
{0, 0, 0, 0, 8, 0, 0, 7, 9}};
printMat(mat);
cout << "Solution " << endl;
solveSudoku(mat, 0, 0);
return 0;
}