Created
November 5, 2015 18:45
-
-
Save ashwath10110/28e8ac151a71f98d4f25 to your computer and use it in GitHub Desktop.
Simple Backtracking C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class RatInMaze | |
{ | |
public int[,] board; | |
public int[,] sol; | |
public int desx; | |
public int desy; | |
public int N { get; set; } | |
public RatInMaze(int N, int desx, int desy) | |
{ | |
this.desx = desx; | |
this.desy = desy; | |
this.N = N; | |
//Initialise Board | |
board = HelperFunctions<int>.setMatrixDefaultValue(board, N, N, -1); | |
sol = HelperFunctions<int>.setMatrixDefaultValue(sol, N, N, 0); | |
//Solve Board | |
if (SolveMaze()) | |
HelperFunctions<int>.PrintMatrix(sol, N, N); | |
} | |
public bool SolveMaze() | |
{ | |
var temp = getMaze(); | |
board = temp; | |
return SolveMazeUtil(0, 0); | |
} | |
public bool isSafe(int x, int y) | |
{ | |
if (x >= 0 && x < N && y >= 0 && y < N && board[x, y] == 1) | |
return true; | |
return false; | |
} | |
public bool isDestination(int x, int y) | |
{ | |
if (x == desx && y == desy) | |
return true; | |
else | |
return false; | |
} | |
public bool SolveMazeUtil(int x, int y) | |
{ | |
HelperFunctions<int>.PrintMatrix(sol, N, N); | |
Console.WriteLine(); | |
Console.WriteLine(); | |
if (isDestination(x, y)) | |
{ | |
sol[x, y] = 1; | |
return true; | |
} | |
if (isSafe(x, y)) | |
{ | |
sol[x, y] = 1; | |
if (SolveMazeUtil(x + 1, y) == true) | |
return true; | |
if (SolveMazeUtil(x, y + 1) == true) | |
return true; | |
sol[x, y] = 0; | |
return false; | |
} | |
return false; | |
} | |
public int[,] getMaze() | |
{ | |
int[,] board = new int[4, 4]; | |
board = HelperFunctions<int>.setMatrixDefaultValue(board, 4, 4, 0); | |
board[0, 0] = 1; | |
board[1, 0] = 1; | |
board[1, 1] = 1; | |
board[1, 3] = 1; | |
board[2, 1] = 1; | |
board[3, 0] = 1; | |
board[3, 1] = 1; | |
board[3, 2] = 1; | |
board[3, 3] = 1; | |
return board; | |
} | |
} | |
public class NQueens | |
{ | |
public int[,] board; | |
public int N { get; set; } | |
public NQueens(int N) | |
{ | |
this.N = N; | |
board = HelperFunctions<int>.setMatrixDefaultValue(board, N, N, 0); | |
if (Solve()) | |
print(N, N); | |
} | |
public bool isSafe(int row, int col) | |
{ | |
int i, j; | |
/* Check this row on left side */ | |
for (i = 0; i < col; i++) | |
{ | |
if (board[row, i] == 1) | |
return false; | |
} | |
/* Check upper diagonal on left side */ | |
for (i = row, j = col; i >= 0 && j >= 0; i--, j--) | |
{ | |
if (board[i, j] == 1) | |
return false; | |
} | |
/* Check lower diagonal on left side */ | |
for (i = row, j = col; j >= 0 && i < N; i++, j--) | |
{ | |
if (board[i, j] == 1) | |
return false; | |
} | |
return true; | |
} | |
public void print(int M, int N) | |
{ | |
for (int i = 0; i < M; i++) | |
{ | |
for (int j = 0; j < N; j++) | |
{ | |
if (board[i, j] == 0) | |
Console.Write(". "); | |
else | |
Console.Write(board[i, j] + " "); | |
} | |
Console.WriteLine(); | |
} | |
} | |
bool solveNQUtil(int col) | |
{ | |
if (col >= N) | |
{ | |
return true; | |
} | |
for (int i = 0; i < N; i++) | |
{ | |
if (isSafe(i, col)) | |
{ | |
board[i, col] = 1; | |
if (solveNQUtil(col + 1)) | |
{ | |
return true; | |
} | |
board[i, col] = 0; | |
} | |
} | |
return false; | |
} | |
public bool Solve() | |
{ | |
if (solveNQUtil(0) == true) | |
{ | |
Console.WriteLine("Exists for " + N); | |
return true; | |
} | |
Console.WriteLine("Not Exists for " + N); | |
return false; | |
} | |
} | |
public class Sudoku | |
{ | |
public int[,] grid; | |
public int N; | |
public static int UNASSIGNED = 0; | |
public Sudoku() | |
{ | |
this.N = 9; | |
grid = getBoard(); | |
if (SolveSudoku()) | |
HelperFunctions<int>.PrintMatrix(grid, N, N); | |
} | |
public bool FindUnassignedLocation(ref int x, ref int y) | |
{ | |
for (int row = 0; row < N; row++) | |
for (int col = 0; col < N; col++) | |
if (grid[row, col] == UNASSIGNED) | |
{ | |
x = row; | |
y = col; | |
return true; | |
} | |
return false; | |
} | |
public bool SolveSudoku() | |
{ | |
int row = 0, col = 0; | |
if (!FindUnassignedLocation(ref row, ref col)) | |
return true; // success! | |
for (int num = 1; num <= 9; num++) | |
{ | |
if (isSafe(row, col, num)) | |
{ | |
grid[row, col] = num; | |
if (SolveSudoku()) | |
return true; | |
grid[row, col] = UNASSIGNED; | |
} | |
} | |
return false; | |
} | |
public bool UsedInRow(int row, int num) | |
{ | |
for (int col = 0; col < N; col++) | |
if (grid[row, col] == num) | |
return true; | |
return false; | |
} | |
public bool UsedInCol(int col, int num) | |
{ | |
for (int row = 0; row < N; row++) | |
if (grid[row, col] == num) | |
return true; | |
return false; | |
} | |
public bool UsedInBox(int boxStartRow, int boxStartCol, int num) | |
{ | |
for (int row = 0; row < 3; row++) | |
for (int col = 0; col < 3; col++) | |
if (grid[row + boxStartRow, col + boxStartCol] == num) | |
return true; | |
return false; | |
} | |
public bool isSafe(int row, int col, int num) | |
{ | |
return !UsedInRow(row, num) && | |
!UsedInCol(col, num) && | |
!UsedInBox(row - row % 3, col - col % 3, num); | |
} | |
public int[,] getBoard() | |
{ | |
int[,] board = new int[9, 9] | |
{ | |
{3, 0, 6, 5, 0, 8, 4, 0, 0}, | |
{5, 2, 0, 0, 0, 0, 0, 0, 0}, | |
{0, 8, 7, 0, 0, 0, 0, 3, 1}, | |
{0, 0, 3, 0, 1, 0, 0, 8, 0}, | |
{9, 0, 0, 8, 6, 3, 0, 0, 5}, | |
{0, 5, 0, 0, 9, 0, 6, 0, 0}, | |
{1, 3, 0, 0, 0, 0, 2, 5, 0}, | |
{0, 0, 0, 0, 0, 0, 0, 7, 4}, | |
{0, 0, 5, 2, 0, 6, 3, 0, 0} | |
}; | |
return board; | |
} | |
} | |
public class KnightsTour | |
{ | |
public int[,] board; | |
public int N { get; set; } | |
public KnightsTour(int N) | |
{ | |
this.N = N; | |
//Initialise Board | |
board = HelperFunctions<int>.setMatrixDefaultValue(board, N, N, -1); | |
//Solve Board | |
if(Solve()) | |
HelperFunctions<int>.PrintMatrix(board, N, N); | |
} | |
public bool isMovePossible(int x, int y) | |
{ | |
if (x >= 0 && x < N && y >= 0 && y < N && board[x, y] == -1) | |
return true; | |
else | |
return false; | |
} | |
bool solveUtil(int x, int y, int[] XList, int[] YList, int stepCounter) | |
{ | |
if (stepCounter == N * N) | |
return true; | |
for (int i = 0; i < 8; i++) | |
{ | |
int Next_X = x + XList[i]; | |
int Next_Y = y + YList[i]; | |
if (isMovePossible(Next_X, Next_Y)) | |
{ | |
board[Next_X, Next_Y] = stepCounter; | |
if (solveUtil(Next_X, Next_Y, XList, YList, stepCounter + 1)) | |
{ | |
return true; | |
} | |
else | |
{ | |
board[Next_X, Next_Y] = -1; | |
} | |
} | |
} | |
return false; | |
} | |
public bool Solve() | |
{ | |
int[] xMove = { 2, 1, -1, -2, -2, -1, 1, 2 }; | |
int[] yMove = { 1, 2, 2, 1, -1, -2, -2, -1 }; | |
board[0, 0] = 0; | |
int stepCounter = 1; | |
if (solveUtil(0, 0, xMove, yMove, stepCounter) == false) | |
{ | |
Console.WriteLine("Not Exists for "+N); | |
return false; | |
} | |
else | |
{ | |
return true; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
nice sumple backtracking bro good whishes