mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-04-09 21:48:54 +08:00
Major rework to improve code quality and add automation checks (#805)
* delete secant method - it is identical to regula falsi * document + improvize root finding algorithms * attempt to document gaussian elimination * added file brief * commented doxygen-mainpage, added files-list link * corrected files list link path * files-list link correction - this time works :) * document successive approximations * cleaner equation * updating DIRECTORY.md * documented kmp string search * document brute force string search * document rabin-karp string search * fixed mainpage readme * doxygen v1.8.18 will suppress out the #minipage in the markdown * cpplint correction for header guard style * github action to auto format source code per cpplint standard * updated setting to add 1 space before `private` and `public` keywords * auto rename files and auto format code * added missing "run" for step * corrected asignmemt operation * fixed trim and assign syntax * added git move for renaming bad filenames * added missing pipe for trim * added missing space * use old and new fnames * store old fname using echo * move files only if there is a change in filename * put old filenames in quotes * use double quote for old filename * escape double quotes * remove old_fname * try escape characters and echo" * add file-type to find * cleanup echo * ensure all trim variables are also in quotes * try escape -quote again * remove second escpe quote * use single quote for first check * use carets instead of quotes * put variables in brackets * remove -e from echo * add debug echos * try print0 flag * find command with while instead of for-loop * find command using IFS instead * 🎉 IFS fix worked - escaped quotes for git mv * protetc each word in git mv .. * filename exists in lower cases - renamed * 🎉 git push enabled * updating DIRECTORY.md * git pull & then push * formatting filenamesd7af6fdc8c* formatting source-code ford7af6fdc8c* remove allman break before braces * updating DIRECTORY.md * added missing comma lost in previous commit * orchestrate all workflows * fix yml indentation * force push format changes, add title to DIRECTORY.md * pull before proceeding * reorganize pull commands * use master branches for actions * rename .cc files to .cpp * added class destructor to clean up dynamic memory allocation * rename to awesome workflow * commented whole repo cpplint - added modified files lint check * removed need for cpplint * attempt to use actions/checkout@master * temporary: no dependency on cpplint * formatting filenames153fb7b8a5* formatting source-code for153fb7b8a5* updating DIRECTORY.md * fix diff filename * added comments to the code * added test case * formatting source-code fora850308fba* updating DIRECTORY.md * added machine learning folder * added adaline algorithm * updating DIRECTORY.md * fixed issue [LWG2192](https://cplusplus.github.io/LWG/issue2192) for std::abs on MacOS * add cmath for same bug: [LWG2192](https://cplusplus.github.io/LWG/issue2192) for std::abs on MacOS * formatting source-code forf8925e4822* use STL's inner_product * formatting source-code forf94a330594* added range comments * define activation function * use equal initial weights * change test2 function to predict * activation function not friend * previous commit correction * added option for predict function to return value before applying activation function as optional argument * added test case to classify points lying within a sphere * improve documentation for adaline * formatting source-code for15ec4c3aba* added cmake to geometry folder * added algorithm include for std::max * add namespace - machine_learning * add namespace - statistics * add namespace - sorting * added sorting algos to namespace sorting * added namespace string_search * formatting source-code forfd69530515* added documentation to string_search namespace * feat: Add BFS and DFS algorithms to check for cycle in a directed graph * Remove const references for input of simple types Reason: overhead on access * fix bad code sorry for force push * Use pointer instead of the non-const reference because apparently google says so. * Remove a useless and possibly bad Graph constuctor overload * Explicitely specify type of vector during graph instantiation * updating DIRECTORY.md * find openMP before adding subdirectories * added kohonen self organizing map * updating DIRECTORY.md * remove older files and folders from gh-pages before adding new files * remove chronos library due to inacceptability by cpplint * use c++ specific static_cast instead * initialize radom number generator * updated image links with those from CPP repository * rename computer.... folder to numerical methods * added durand kerner method for root computation for arbitrarily large polynomials * fixed additional comma * fix cpplint errors * updating DIRECTORY.md * convert to function module * update documentation * move openmp to main loop * added two test cases * use INT16_MAX * remove return statement from omp-for loop and use "break" * run tests when no input is provided and skip tests when input polynomial is provided * while loop cannot have break - replaced with continue and check is present in the main while condition * (1) break while loop (2) skip runs on break_loop instead of hard-break * add documentation images * use long double for errors and tolerance checks * make iterator variable i local to threads * add critical secions to omp threads * bugfix: move file writing outside of the parallel loop othersie, there is no gurantee of the order of roots written to file * rename folder to data_structures * updating DIRECTORY.md * fix ambiguous symbol `size` * add data_structures to cmake * docs: enable tree view, add timestamp in footer, try clang assistaed parsing * doxygen - open links in external window * remove invalid parameter from function docs * use HTML5 img tag to resize images * move file to proper folder * fix documentations and cpplint * formatting source-code foraacaf9828c* updating DIRECTORY.md * cpplint: add braces for multiple statement if * add explicit link to badges * remove duplicate line Signed-off-by: Krishna Vedala <7001608+kvedala@users.noreply.github.com> * remove namespace indentation * remove file associations in settings * add author name * enable cmake in subfolders of data_structures * create and link object file * cpp lint fixes and instantiate template classes * cpp lint fixes and instantiate template classes Signed-off-by: Krishna Vedala <7001608+kvedala@users.noreply.github.com> * cpplint - ignore `build/include` Signed-off-by: Krishna Vedala <7001608+kvedala@users.noreply.github.com> * disable redundant gcc compilation in cpplint workflow Signed-off-by: Krishna Vedala <7001608+kvedala@users.noreply.github.com> * template header files contain function codes as well and removed redundant subfolders Signed-off-by: Krishna Vedala <7001608+kvedala@users.noreply.github.com> * updating DIRECTORY.md * remove semicolons after functions in a class Signed-off-by: Krishna Vedala <7001608+kvedala@users.noreply.github.com> * cpplint header guard style Signed-off-by: Krishna Vedala <7001608+kvedala@users.noreply.github.com> * remove semilon Signed-off-by: Krishna Vedala <7001608+kvedala@users.noreply.github.com> * added LU decomposition algorithm Signed-off-by: Krishna Vedala <7001608+kvedala@users.noreply.github.com> * added QR decomposition algorithm Signed-off-by: Krishna Vedala <7001608+kvedala@users.noreply.github.com> * use QR decomposition to find eigen values Signed-off-by: Krishna Vedala <7001608+kvedala@users.noreply.github.com> * updating DIRECTORY.md * use std::rand for thread safety Signed-off-by: Krishna Vedala <7001608+kvedala@users.noreply.github.com> * move srand to main() Signed-off-by: Krishna Vedala <7001608+kvedala@users.noreply.github.com> * cpplint braces correction Signed-off-by: Krishna Vedala <7001608+kvedala@users.noreply.github.com> * updated eigen value documentation Signed-off-by: Krishna Vedala <7001608+kvedala@users.noreply.github.com> * fix matrix shift doc Signed-off-by: Krishna Vedala <7001608+kvedala@users.noreply.github.com> * rename CONTRIBUTION.md to CONTRIBUTING.md #836 * remove 'sort alphabetical order' check * added documentation check * remove extra paranthesis * added gitpod * added gitpod link from README * attempt to add vscode gitpod extensions * update gitpod extensions * add gitpod extensions cmake-tools and git-graph * remove gitpod init and add commands * use init to one time install doxygen, graphviz, cpplint * use gitpod dockerfile * add ninja build system to docker * remove configure task * add github prebuild specs to gitpod * disable gitpod addcommit * update documentation for kohonen_som * added ode solve using forward euler method * added mid-point euler ode solver * fixed itegration step equation * added semi-implicit euler ODE solver * updating DIRECTORY.md * fix cpplint issues - lines 117 and 124 * added documentation to ode group * corrected semi-implicit euler function * updated docs and test cases better structure * replace `free` with `delete` operator * formatting source-code forf55ab50cf2* updating DIRECTORY.md * main function must return * added machine learning group * added kohonen som topology algorithm * fix graph image path * updating DIRECTORY.md * fix braces * use snprintf instead of sprintf * use static_cast * hardcode character buffer size * fix machine learning groups in documentation * fix missing namespace function * replace kvedala fork references to TheAlgorithms * fix bug in counting_sort Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Anmol3299 <mittalanmol22@gmail.com>
This commit is contained in:
@@ -7,8 +7,7 @@ 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;
|
||||
@@ -16,22 +15,18 @@ bool isSafe(int v, bool graph[V][V], int color[], int c)
|
||||
}
|
||||
|
||||
/* A recursive utility function to solve m coloring problem */
|
||||
void graphColoring(bool graph[V][V], int m, int color[], int v)
|
||||
{
|
||||
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)
|
||||
{
|
||||
if (v == V) {
|
||||
printSolution(color);
|
||||
return;
|
||||
}
|
||||
|
||||
/* Consider this vertex v and try different colors */
|
||||
for (int c = 1; c <= m; c++)
|
||||
{
|
||||
for (int c = 1; c <= m; c++) {
|
||||
/* Check if assignment of color c to v is fine*/
|
||||
if (isSafe(v, graph, color, c))
|
||||
{
|
||||
if (isSafe(v, graph, color, c)) {
|
||||
color[v] = c;
|
||||
|
||||
/* recur to assign colors to rest of the vertices */
|
||||
@@ -45,17 +40,14 @@ void graphColoring(bool graph[V][V], int m, int color[], int v)
|
||||
}
|
||||
|
||||
/* A utility function to print solution */
|
||||
void printSolution(int color[])
|
||||
{
|
||||
void printSolution(int color[]) {
|
||||
printf(" Following are the assigned colors \n");
|
||||
for (int i = 0; i < V; i++)
|
||||
printf(" %d ", color[i]);
|
||||
for (int i = 0; i < V; i++) printf(" %d ", color[i]);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
// driver program to test above function
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
/* Create following graph and test whether it is 3 colorable
|
||||
(3)---(2)
|
||||
| / |
|
||||
@@ -69,12 +61,11 @@ int main()
|
||||
{1, 1, 0, 1},
|
||||
{1, 0, 1, 0},
|
||||
};
|
||||
int m = 3; // Number of colors
|
||||
int m = 3; // Number of colors
|
||||
|
||||
int color[V];
|
||||
|
||||
for (int i = 0; i < V; i++)
|
||||
color[i] = 0;
|
||||
for (int i = 0; i < V; i++) color[i] = 0;
|
||||
|
||||
graphColoring(graph, m, color, 0);
|
||||
return 0;
|
||||
|
||||
@@ -1,68 +1,60 @@
|
||||
#include <iostream>
|
||||
# define n 8
|
||||
#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
|
||||
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);
|
||||
using std::cin;
|
||||
using std::cout;
|
||||
|
||||
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;
|
||||
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)
|
||||
if (mov == n * n)
|
||||
return true;
|
||||
|
||||
for(k=0;k<8;k++)
|
||||
{
|
||||
xnext=x+xmov[k];
|
||||
ynext=y+ymov[k];
|
||||
for (k = 0; k < 8; k++) {
|
||||
xnext = x + xmov[k];
|
||||
ynext = y + ymov[k];
|
||||
|
||||
if(issafe(xnext,ynext,sol))
|
||||
{
|
||||
sol[xnext][ynext]=mov;
|
||||
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;
|
||||
}
|
||||
if (solve(xnext, ynext, mov + 1, sol, xmov, ymov) == true)
|
||||
return true;
|
||||
else
|
||||
sol[xnext][ynext] = -1;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
//initialize();
|
||||
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 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;
|
||||
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";
|
||||
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";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ int minimax(int depth, int node_index, bool is_max, vector<int> scores,
|
||||
}
|
||||
|
||||
int main() {
|
||||
vector<int> scores = { 90, 23, 6, 33, 21, 65, 123, 34423 };
|
||||
vector<int> scores = {90, 23, 6, 33, 21, 65, 123, 34423};
|
||||
int height = log2(scores.size());
|
||||
|
||||
cout << "Optimal value: " << minimax(0, 0, true, scores, height) << endl;
|
||||
|
||||
@@ -2,19 +2,15 @@
|
||||
#define N 4
|
||||
using namespace std;
|
||||
|
||||
void printSolution(int board[N][N])
|
||||
{
|
||||
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];
|
||||
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)
|
||||
{
|
||||
bool isSafe(int board[N][N], int row, int col) {
|
||||
int i, j;
|
||||
|
||||
/* Check this row on left side */
|
||||
@@ -35,23 +31,18 @@ bool isSafe(int board[N][N], int row, int col)
|
||||
return true;
|
||||
}
|
||||
|
||||
void solveNQ(int board[N][N], int col)
|
||||
{
|
||||
|
||||
if (col >= N)
|
||||
{
|
||||
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++)
|
||||
{
|
||||
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;
|
||||
board[i][col] = 1;
|
||||
@@ -59,18 +50,13 @@ void solveNQ(int board[N][N], int col)
|
||||
/* recur to place rest of the queens */
|
||||
solveNQ(board, col + 1);
|
||||
|
||||
board[i][col] = 0; // BACKTRACK
|
||||
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}};
|
||||
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;
|
||||
|
||||
@@ -1,18 +1,16 @@
|
||||
#include <iostream>
|
||||
#define n 4
|
||||
#define inc_loop(var, start, stop) for (int var=start; var <= stop; var++)
|
||||
#define dec_loop(var, start, stop) for (int var=start; var >= stop; var--)
|
||||
#define inc_loop(var, start, stop) for (int var = start; var <= stop; var++)
|
||||
#define dec_loop(var, start, stop) for (int var = start; var >= stop; var--)
|
||||
void PrintSol(int Board[n][n]) {
|
||||
inc_loop(i, 0, n-1) {
|
||||
inc_loop(j, 0, n-1)
|
||||
std::cout << Board[i][j] << " ";
|
||||
inc_loop(i, 0, n - 1) {
|
||||
inc_loop(j, 0, n - 1) std::cout << Board[i][j] << " ";
|
||||
std::cout << std::endl;
|
||||
}
|
||||
std::cout << std::endl;
|
||||
if (n%2 == 0 || (n%2 == 1 && Board[n/2+1][0] != 1)) {
|
||||
inc_loop(i, 0, n-1) {
|
||||
dec_loop(j, n-1, 0)
|
||||
std::cout << Board[i][j] << " ";
|
||||
if (n % 2 == 0 || (n % 2 == 1 && Board[n / 2 + 1][0] != 1)) {
|
||||
inc_loop(i, 0, n - 1) {
|
||||
dec_loop(j, n - 1, 0) std::cout << Board[i][j] << " ";
|
||||
std::cout << std::endl;
|
||||
}
|
||||
std::cout << std::endl;
|
||||
@@ -21,7 +19,7 @@ void PrintSol(int Board[n][n]) {
|
||||
|
||||
bool CanIMove(int Board[n][n], int row, int col) {
|
||||
/// check in the row
|
||||
inc_loop(i, 0, col-1) {
|
||||
inc_loop(i, 0, col - 1) {
|
||||
if (Board[row][i] == 1)
|
||||
return false;
|
||||
}
|
||||
@@ -43,7 +41,7 @@ void NQueenSol(int Board[n][n], int col) {
|
||||
PrintSol(Board);
|
||||
return;
|
||||
}
|
||||
inc_loop(i, 0, n-1) {
|
||||
inc_loop(i, 0, n - 1) {
|
||||
if (CanIMove(Board, i, col)) {
|
||||
Board[i][col] = 1;
|
||||
NQueenSol(Board, col + 1);
|
||||
@@ -54,8 +52,8 @@ void NQueenSol(int Board[n][n], int col) {
|
||||
|
||||
int main() {
|
||||
int Board[n][n] = {0};
|
||||
if (n%2 == 0) {
|
||||
inc_loop(i, 0, n/2-1) {
|
||||
if (n % 2 == 0) {
|
||||
inc_loop(i, 0, n / 2 - 1) {
|
||||
if (CanIMove(Board, i, 0)) {
|
||||
Board[i][0] = 1;
|
||||
NQueenSol(Board, 1);
|
||||
@@ -63,7 +61,7 @@ int main() {
|
||||
}
|
||||
}
|
||||
} else {
|
||||
inc_loop(i, 0, n/2) {
|
||||
inc_loop(i, 0, n / 2) {
|
||||
if (CanIMove(Board, i, 0)) {
|
||||
Board[i][0] = 1;
|
||||
NQueenSol(Board, 1);
|
||||
|
||||
@@ -1,73 +1,62 @@
|
||||
/*
|
||||
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.
|
||||
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;
|
||||
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 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;
|
||||
}
|
||||
// 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;
|
||||
}
|
||||
// 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 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];
|
||||
int soln[size][size];
|
||||
|
||||
for (int i = 0; i < size; ++i)
|
||||
{
|
||||
for (int j = 0; j < size; ++j)
|
||||
{
|
||||
soln[i][j] = 0;
|
||||
}
|
||||
}
|
||||
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;
|
||||
int currposrow = 0;
|
||||
int currposcol = 0;
|
||||
solveMaze(currposrow, currposcol, maze, soln);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1,15 +1,12 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
///N=9;
|
||||
/// 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)
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -18,12 +15,9 @@ bool isPossible(int mat[][9], int i, int j, int no)
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -31,83 +25,63 @@ bool isPossible(int mat[][9], int i, int j, int no)
|
||||
|
||||
return true;
|
||||
}
|
||||
void printMat(int mat[][9])
|
||||
{
|
||||
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
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)
|
||||
{
|
||||
if ((j + 1) % 3 == 0) {
|
||||
cout << '\t';
|
||||
}
|
||||
}
|
||||
if ((i + 1) % 3 == 0)
|
||||
{
|
||||
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
|
||||
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)
|
||||
{
|
||||
/// Crossed the last Cell in the row
|
||||
if (j == 9) {
|
||||
return solveSudoku(mat, i + 1, 0);
|
||||
}
|
||||
|
||||
///Blue Cell - Skip
|
||||
if (mat[i][j] != 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
|
||||
/// 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)
|
||||
{
|
||||
if (aageKiSolveHui) {
|
||||
return true;
|
||||
}
|
||||
///Nahin solve hui
|
||||
///loop will place the next no.
|
||||
/// Nahin solve hui
|
||||
/// loop will place the next no.
|
||||
}
|
||||
}
|
||||
///Sare no try kr liey, kisi se bhi solve nahi hui
|
||||
/// 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}};
|
||||
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;
|
||||
|
||||
Reference in New Issue
Block a user