Skip to content

Commit ab9a49e

Browse files
committed
[Algorithm Design and Techniques]
1 parent 964eb43 commit ab9a49e

File tree

4 files changed

+29
-77
lines changed

4 files changed

+29
-77
lines changed

src/js/algorithms/backtracking/rat-in-maze.js

-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
function isSafe(maze, x, y) {
22
const n = maze.length;
3-
// check if x and y are in limits and cell is not blocked
43
if (x >= 0 && y >= 0 && x < n && y < n && maze[x][y] !== 0) {
54
return true;
65
}
@@ -9,28 +8,18 @@ function isSafe(maze, x, y) {
98

109
function findPath(maze, x, y, solution) {
1110
const n = maze.length;
12-
// check if maze[x][y] is feasible to move
1311
if (x === n - 1 && y === n - 1) {
14-
// we have reached
1512
solution[x][y] = 1;
1613
return true;
1714
}
18-
// Check if maze[x][y] is valid
1915
if (isSafe(maze, x, y) === true) {
20-
// mark x,y as part of solution path
2116
solution[x][y] = 1;
22-
/* Move forward in x direction */
2317
if (findPath(maze, x + 1, y, solution)) {
2418
return true;
2519
}
26-
/* If moving in x direction doesn't give
27-
solution then Move down in y direction */
2820
if (findPath(maze, x, y + 1, solution)) {
2921
return true;
3022
}
31-
/* If none of the above movements work then
32-
BACKTRACK: unmark x,y as part of solution
33-
path */
3423
solution[x][y] = 0;
3524
return false;
3625
}
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,49 @@
11
const UNASSIGNED = 0;
22

3-
/* Returns a boolean which indicates whether any assigned entry
4-
in the specified row matches the given number. */
5-
function usedInRow(grid, row, num) {
6-
for (let col = 0; col < grid.length; col++) {
7-
if (grid[row][col] === num) {
3+
function usedInRow(matrix, row, num) {
4+
for (let col = 0; col < matrix.length; col++) {
5+
if (matrix[row][col] === num) {
86
return true;
97
}
108
}
119
return false;
1210
}
13-
/* Returns a boolean which indicates whether any assigned entry
14-
in the specified column matches the given number. */
15-
function usedInCol(grid, col, num) {
16-
for (let row = 0; row < grid.length; row++) {
17-
if (grid[row][col] === num) {
11+
12+
function usedInCol(matrix, col, num) {
13+
for (let row = 0; row < matrix.length; row++) {
14+
if (matrix[row][col] === num) {
1815
return true;
1916
}
2017
}
2118
return false;
2219
}
23-
/* Returns a boolean which indicates whether any assigned entry
24-
within the specified 3x3 box matches the given number. */
25-
function usedInBox(grid, boxStartRow, boxStartCol, num) {
20+
21+
function usedInBox(matrix, boxStartRow, boxStartCol, num) {
2622
for (let row = 0; row < 3; row++) {
2723
for (let col = 0; col < 3; col++) {
28-
if (grid[row + boxStartRow][col + boxStartCol] === num) {
24+
if (matrix[row + boxStartRow][col + boxStartCol] === num) {
2925
return true;
3026
}
3127
}
3228
}
3329
return false;
3430
}
3531

36-
function isSafe(grid, row, col, num) {
37-
/* Check if 'num' is not already placed in current row,
38-
current column and current 3x3 box */
32+
function isSafe(matrix, row, col, num) {
3933
return (
40-
!usedInRow(grid, row, num) &&
41-
!usedInCol(grid, col, num) &&
42-
!usedInBox(grid, row - (row % 3), col - (col % 3), num)
34+
!usedInRow(matrix, row, num) &&
35+
!usedInCol(matrix, col, num) &&
36+
!usedInBox(matrix, row - (row % 3), col - (col % 3), num)
4337
);
4438
}
45-
function solveSudoku(grid) {
39+
function solveSudoku(matrix) {
4640
let row = 0;
4741
let col = 0;
4842
let checkBlankSpaces = false;
49-
// If there is no unassigned location, we are done
50-
for (row = 0; row < grid.length; row++) {
51-
for (col = 0; col < grid[row].length; col++) {
52-
if (grid[row][col] === UNASSIGNED) {
43+
44+
for (row = 0; row < matrix.length; row++) {
45+
for (col = 0; col < matrix[row].length; col++) {
46+
if (matrix[row][col] === UNASSIGNED) {
5347
checkBlankSpaces = true;
5448
break;
5549
}
@@ -60,27 +54,23 @@ function solveSudoku(grid) {
6054
}
6155
if (checkBlankSpaces === false) {
6256
return true;
63-
} // success!
64-
// consider digits 1 to 9
57+
}
58+
6559
for (let num = 1; num <= 9; num++) {
66-
// if looks promising
67-
if (isSafe(grid, row, col, num)) {
68-
// make tentative assignment
69-
grid[row][col] = num;
70-
// return, if success, yay!
71-
if (solveSudoku(grid)) {
60+
if (isSafe(matrix, row, col, num)) {
61+
matrix[row][col] = num;
62+
if (solveSudoku(matrix)) {
7263
return true;
7364
}
74-
// failure, unmake & try again
75-
grid[row][col] = UNASSIGNED;
65+
matrix[row][col] = UNASSIGNED;
7666
}
7767
}
7868
return false;
7969
}
8070

81-
export function sudokuSolver(grid) {
82-
if (solveSudoku(grid) === true) {
83-
return grid;
71+
export function sudokuSolver(matrix) {
72+
if (solveSudoku(matrix) === true) {
73+
return matrix;
8474
}
8575
return 'NO SOLUTION EXISTS!';
8676
}

src/ts/algorithms/backtracking/rat-in-maze.ts

-11
Original file line numberDiff line numberDiff line change
@@ -22,32 +22,22 @@ function findPath(
2222
solution: Array<Array<number>>
2323
) {
2424
const n = maze.length;
25-
// check if maze[x][y] is feasible to move
2625
if (x === n - 1 && y === n - 1) {
27-
// we have reached
2826
solution[x][y] = 1;
2927
return true;
3028
}
3129

32-
// Check if maze[x][y] is valid
3330
if (isSafe(maze, x, y) === true) {
34-
// mark x,y as part of solution path
3531
solution[x][y] = 1;
3632

37-
/* Move forward in x direction */
3833
if (findPath(maze, x + 1, y, solution)) {
3934
return true;
4035
}
4136

42-
/* If moving in x direction doesn't give
43-
solution then Move down in y direction */
4437
if (findPath(maze, x, y + 1, solution)) {
4538
return true;
4639
}
4740

48-
/* If none of the above movements work then
49-
BACKTRACK: unmark x,y as part of solution
50-
path */
5141
solution[x][y] = 0;
5242
return false;
5343
}
@@ -57,7 +47,6 @@ function findPath(
5747

5848
function isSafe(maze: Array<Array<number>>, x: number, y: number) {
5949
const n = maze.length;
60-
// check if x and y are in limits and cell is not blocked
6150
if (x >= 0 && y >= 0 && x < n && y < n && maze[x][y] !== 0) {
6251
return true;
6352
}

src/ts/algorithms/backtracking/sudoku-solver.ts

+1-17
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ function solveSudoku(grid: Array<Array<number>>) {
1313
let col = 0;
1414
let checkBlankSpaces = false;
1515

16-
// If there is no unassigned location, we are done
1716
for (row = 0; row < grid.length; row++) {
1817
for (col = 0; col < grid[row].length; col++) {
1918
if (grid[row][col] === UNASSIGNED) {
@@ -28,30 +27,23 @@ function solveSudoku(grid: Array<Array<number>>) {
2827

2928
if (checkBlankSpaces === false) {
3029
return true;
31-
} // success!
30+
}
3231

33-
// consider digits 1 to 9
3432
for (let num = 1; num <= 9; num++) {
35-
// if looks promising
3633
if (isSafe(grid, row, col, num)) {
37-
// make tentative assignment
3834
grid[row][col] = num;
3935

40-
// return, if success, yay!
4136
if (solveSudoku(grid)) {
4237
return true;
4338
}
4439

45-
// failure, unmake & try again
4640
grid[row][col] = UNASSIGNED;
4741
}
4842
}
4943

5044
return false;
5145
}
5246

53-
/* Returns a boolean which indicates whether any assigned entry
54-
in the specified row matches the given number. */
5547
function usedInRow(grid: Array<Array<number>>, row: number, num: number) {
5648
for (let col = 0; col < grid.length; col++) {
5749
if (grid[row][col] === num) {
@@ -61,8 +53,6 @@ function usedInRow(grid: Array<Array<number>>, row: number, num: number) {
6153
return false;
6254
}
6355

64-
/* Returns a boolean which indicates whether any assigned entry
65-
in the specified column matches the given number. */
6656
function usedInCol(grid: Array<Array<number>>, col: number, num: number) {
6757
for (let row = 0; row < grid.length; row++) {
6858
if (grid[row][col] === num) {
@@ -72,8 +62,6 @@ function usedInCol(grid: Array<Array<number>>, col: number, num: number) {
7262
return false;
7363
}
7464

75-
/* Returns a boolean which indicates whether any assigned entry
76-
within the specified 3x3 box matches the given number. */
7765
function usedInBox(
7866
grid: Array<Array<number>>,
7967
boxStartRow: number,
@@ -90,11 +78,7 @@ function usedInBox(
9078
return false;
9179
}
9280

93-
/* Returns a boolean which indicates whether it will be legal to assign
94-
num to the given row,col location. */
9581
function isSafe(grid: Array<Array<number>>, row: number, col: number, num: number) {
96-
/* Check if 'num' is not already placed in current row,
97-
current column and current 3x3 box */
9882
return (
9983
!usedInRow(grid, row, num) &&
10084
!usedInCol(grid, col, num) &&

0 commit comments

Comments
 (0)