mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-04-23 18:12:47 +08:00
style: format code
This commit is contained in:
@@ -1,48 +1,47 @@
|
||||
#include<stdio.h>
|
||||
|
||||
#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)
|
||||
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);
|
||||
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);
|
||||
|
||||
|
||||
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;
|
||||
color[v] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/* A utility function to print solution */
|
||||
@@ -50,10 +49,10 @@ void printSolution(int color[])
|
||||
{
|
||||
printf(" Following are the assigned colors \n");
|
||||
for (int i = 0; i < V; i++)
|
||||
printf(" %d ", color[i]);
|
||||
printf(" %d ", color[i]);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
|
||||
// driver program to test above function
|
||||
int main()
|
||||
{
|
||||
@@ -64,18 +63,19 @@ int main()
|
||||
| / |
|
||||
(0)---(1)
|
||||
*/
|
||||
bool graph[V][V] = {{0, 1, 1, 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];
|
||||
int color[V];
|
||||
|
||||
for (int i = 0; i < V; i++)
|
||||
for (int i = 0; i < V; i++)
|
||||
color[i] = 0;
|
||||
|
||||
|
||||
graphColoring(graph, m, color, 0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1,82 +1,77 @@
|
||||
#include<iostream>
|
||||
#include <iostream>
|
||||
#define N 4
|
||||
using namespace std;
|
||||
|
||||
void printSolution(int board[N][N])
|
||||
{
|
||||
cout<<"\n";
|
||||
cout << "\n";
|
||||
for (int i = 0; i < N; i++)
|
||||
{
|
||||
for (int j = 0; j < N; j++)
|
||||
cout<<""<<board[i][j];
|
||||
cout<<"\n";
|
||||
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--)
|
||||
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--)
|
||||
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){
|
||||
|
||||
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) )
|
||||
if (isSafe(board, i, col))
|
||||
{
|
||||
/* Place this queen in board[i][col] */
|
||||
// cout<<"\n"<<col<<"can place"<<i;
|
||||
// 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);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -11,64 +11,63 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
int solveMaze(int currposrow,int currposcol,int maze[size][size],int soln[size][size])
|
||||
int solveMaze(int currposrow, int currposcol, int maze[size][size], int soln[size][size])
|
||||
{
|
||||
if((currposrow==size-1) && (currposcol==size-1))
|
||||
if ((currposrow == size - 1) && (currposcol == size - 1))
|
||||
{
|
||||
soln[currposrow][currposcol]=1;
|
||||
for(int i=0;i<size;++i)
|
||||
soln[currposrow][currposcol] = 1;
|
||||
for (int i = 0; i < size; ++i)
|
||||
{
|
||||
for (int j=0;j<size;++j)
|
||||
for (int j = 0; j < size; ++j)
|
||||
{
|
||||
cout<<soln[i][j];
|
||||
cout << soln[i][j];
|
||||
}
|
||||
cout<<endl;
|
||||
cout << endl;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
soln[currposrow][currposcol]=1;
|
||||
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))
|
||||
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))
|
||||
if ((currposrow < size - 1) && maze[currposrow + 1][currposcol] == 1 && solveMaze(currposrow + 1, currposcol, maze, soln))
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
// the backtracking part
|
||||
soln[currposrow][currposcol]=0;
|
||||
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 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 i = 0; i < size; ++i)
|
||||
{
|
||||
for (int j = 0; j < size; ++j)
|
||||
{
|
||||
for (int j=0;j<size;++j)
|
||||
{
|
||||
soln[i][j]=0;
|
||||
}
|
||||
soln[i][j] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
int currposrow=0;
|
||||
int currposcol=0;
|
||||
solveMaze(currposrow,currposcol,maze,soln);
|
||||
int currposrow = 0;
|
||||
int currposcol = 0;
|
||||
solveMaze(currposrow, currposcol, maze, soln);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1,24 +1,29 @@
|
||||
#include<iostream>
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
///N=9;
|
||||
int n=9;
|
||||
int n = 9;
|
||||
|
||||
|
||||
bool isPossible(int mat[][9],int i,int j,int no){
|
||||
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){
|
||||
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;
|
||||
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){
|
||||
for (int x = sx; x < sx + 3; x++)
|
||||
{
|
||||
for (int y = sy; y < sy + 3; y++)
|
||||
{
|
||||
if (mat[x][y] == no)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -26,49 +31,59 @@ bool isPossible(int mat[][9],int i,int j,int no){
|
||||
|
||||
return true;
|
||||
}
|
||||
void printMat(int mat[][9]){
|
||||
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';
|
||||
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;
|
||||
if ((i + 1) % 3 == 0)
|
||||
{
|
||||
cout << endl;
|
||||
}
|
||||
cout<<endl;
|
||||
cout << endl;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool solveSudoku(int mat[][9],int i,int j){
|
||||
bool solveSudoku(int mat[][9], int i, int j)
|
||||
{
|
||||
///Base Case
|
||||
if(i==9){
|
||||
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);
|
||||
if (j == 9)
|
||||
{
|
||||
return solveSudoku(mat, i + 1, 0);
|
||||
}
|
||||
|
||||
///Blue Cell - Skip
|
||||
if(mat[i][j]!=0){
|
||||
return solveSudoku(mat,i,j+1);
|
||||
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)){
|
||||
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){
|
||||
bool aageKiSolveHui = solveSudoku(mat, i, j + 1);
|
||||
if (aageKiSolveHui)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
///Nahin solve hui
|
||||
@@ -80,23 +95,23 @@ bool solveSudoku(int mat[][9],int i,int j){
|
||||
return false;
|
||||
}
|
||||
|
||||
int main(){
|
||||
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}};
|
||||
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);
|
||||
printMat(mat);
|
||||
cout << "Solution " << endl;
|
||||
solveSudoku(mat, 0, 0);
|
||||
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user