From ce2530eb87eb2ff22b35e186ba2bfd6d761d5ffa Mon Sep 17 00:00:00 2001 From: anirudherabelly Date: Mon, 15 Oct 2018 13:13:15 -0500 Subject: [PATCH] added other language implementation of backtracking algorithms. --- BackTracking/KnightsTour/KnightsTour.cs | 70 ++++++++++ BackTracking/NQueens/NQueens.cs | 106 +++++++++++++++ .../Permutation/GeneratePermutations.cs | 43 +++++++ BackTracking/RatInAMaze/RatInMaze.cs | 76 +++++++++++ BackTracking/SudokuAlgorithm/sudoku.cs | 121 ++++++++++++++++++ 5 files changed, 416 insertions(+) create mode 100644 BackTracking/KnightsTour/KnightsTour.cs create mode 100644 BackTracking/NQueens/NQueens.cs create mode 100644 BackTracking/Permutation/GeneratePermutations.cs create mode 100644 BackTracking/RatInAMaze/RatInMaze.cs create mode 100644 BackTracking/SudokuAlgorithm/sudoku.cs diff --git a/BackTracking/KnightsTour/KnightsTour.cs b/BackTracking/KnightsTour/KnightsTour.cs new file mode 100644 index 00000000..bc99ab63 --- /dev/null +++ b/BackTracking/KnightsTour/KnightsTour.cs @@ -0,0 +1,70 @@ +/* +move Knight in a chess board such that it covers all the cells atleast once. +*/ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace GFG +{ + class Program + { + static void Main(String[] args) + { + int[,] board = new int[8,8]; + Intialize(board); + int[] rowx = { 2, 1, -1, -2, -2, -1, 1, 2 }; + int[] coly = { 1, 2, 2, 1, -1, -2, -2, -1 }; + if (KnightsTour(board, 0, 0, 1,rowx,coly)) Display(board); + else Console.WriteLine("Solution does not exist"); + } + private static void Intialize(int[,] board) + { + for (int i = 0; i < 8; i++) + { + for (int j = 0; j < 8; j++) + { + board[i, j]=-1; + } + } + board[0, 0] = 0; + } + private static void Display(int[,] board) + { + for(int i = 0; i < 8; i++) + { + for(int j = 0; j < 8; j++) + { + Console.Write(board[i,j]+" "); + } + Console.WriteLine(); + } + } + private static bool KnightsTour(int[,] board, int row, int col,int move,int[] rowx,int[] coly) + { + if (move == 64) return true; + int nextx, nexty; + for(int k = 0; k < 8; k++) + { + nextx = row+rowx[k]; + nexty = col+coly[k]; + if (IsSafe(board, nextx, nexty)) + { + board[nextx, nexty] = move; + if(KnightsTour(board,nextx,nexty,move+1,rowx,coly))return true; + board[nextx, nexty] = -1; + } + } + return false; + } + + private static bool IsSafe(int[,] board, int row, int col) + { + if (row >= 0 && row < 8 && col >= 0 && col < 8 && board[row, col] == -1) return true; + return false; + } + } +} + \ No newline at end of file diff --git a/BackTracking/NQueens/NQueens.cs b/BackTracking/NQueens/NQueens.cs new file mode 100644 index 00000000..44f82da5 --- /dev/null +++ b/BackTracking/NQueens/NQueens.cs @@ -0,0 +1,106 @@ +/* +The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other. +Given an integer n, print all distinct solutions to the n-queens puzzle. +Each solution contains distinct board configurations of the n-queens’ placement, +where the solutions are a permutation of [1,2,3..n] in increasing order, +here the number in the ith place denotes that the ith-column queen is placed in the row with that number. +For eg below figure represents a chessboard [3 1 4 2]. +[0,0,Q,0] +[Q,0,0,0] +[0,0,0,Q] +[0,Q,0,0] +Input: +The first line of input contains an integer T denoting the no of test cases. +Then T test cases follow. Each test case contains an integer n denoting the size of the chessboard. + +Output: +For each test case, output your solutions on one line where each solution is enclosed in square brackets +'[', ']' separated by a space . The solutions are permutations of {1, 2, 3 …, n} in increasing order where +the number in the ith place denotes the ith-column queen is placed in the row with that number, +if no solution exists print -1. + +Constraints: +1<=T<=10 +1<=n<=10 + +Example: +Input +2 +1 +4 +Output: +[1 ] +[2 4 1 3 ] [3 1 4 2 ] +*/ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace GFG +{ + class Program + { + static bool resultFound=false; + static void Main(String[] args) + { + int t = Int32.Parse(Console.ReadLine()); + for(int i = 0; i < t; i++) + { + int n= Int32.Parse(Console.ReadLine()); + int[,] board = new int[n,n]; + Nqueen(board,0,n); + if (!resultFound) Console.Write(-1); + Console.WriteLine(); + } + } + private static void Display(int[,] board, int n) + { + Console.Write("["); + for(int i = 0; i < n; i++) + { + for(int j = 0; j < n; j++) + { + if (board[j, i] == 1)Console.Write(j + 1+" "); + } + } + Console.Write("]"); + } + private static void Nqueen(int[,] board, int col, int n) + { + if (col >= n) + { + resultFound = true; + Display(board,n); + return; + } + for(int i = 0; i < n; i++) + { + if (IsSafe(board, i, col, n)) + { + board[i, col] = 1; + Nqueen(board, col + 1, n); + board[i, col] = 0; + } + } + } + private static bool IsSafe(int[,] board, int row, int col, int n) + { + for(int i = 0; i < col; i++) + { + if (board[row, i] == 1) return false; + } + for(int i=row,j=col;i>=0 && j >=0; i--, j--) + { + if (board[i, j] == 1) return false; + } + for(int i=row,j=col;i= 0; i++, j--) + { + if (board[i, j] == 1) return false; + } + return true; + } + } +} + \ No newline at end of file diff --git a/BackTracking/Permutation/GeneratePermutations.cs b/BackTracking/Permutation/GeneratePermutations.cs new file mode 100644 index 00000000..14b7a50f --- /dev/null +++ b/BackTracking/Permutation/GeneratePermutations.cs @@ -0,0 +1,43 @@ +/* +sb=abc + +output: +abc +acb +bac +bca +cba +cab +*/ +using System; +using System.Text; + +namespace Hackerearth +{ + class Program + { + static void Main(string[] args) + { + StringBuilder sb = new StringBuilder(Console.ReadLine()); + int n = sb.Length; + GeneratePermutations(sb,0,n-1); + } + private static void GeneratePermutations(StringBuilder sb, int l,int r) + { + if (l == r) Console.WriteLine(sb); + for (int i = l; i <= r; i++) + { + swap(sb, l,i); + GeneratePermutations(sb,l+1,r); + swap(sb,l,i); + } + + } + private static void swap(StringBuilder sb, int i, int j) + { + char temp = sb[i]; + sb[i] = sb[j]; + sb[j] = temp; + } + } +} diff --git a/BackTracking/RatInAMaze/RatInMaze.cs b/BackTracking/RatInAMaze/RatInMaze.cs new file mode 100644 index 00000000..850db924 --- /dev/null +++ b/BackTracking/RatInAMaze/RatInMaze.cs @@ -0,0 +1,76 @@ +/* +Consider a rat placed at (0, 0) in a square matrix m[][] of order n and has to reach the destination at (n-1, n-1). +Your task is to complete the function which returns a sorted array of strings denoting all the possible directions +which the rat can take to reach the destination at (n-1, n-1). The directions in which the rat can move are 'U'(up), +'D'(down), 'L' (left), 'R' (right). + +For example +1 0 0 0 +1 1 0 1 +1 1 0 0 +0 1 1 1 + +For the above matrix the rat can reach the destination at (3, 3) from (0, 0) by two paths ie DRDDRR and DDRDRR +when printed in sorted order we get DDRDRR DRDDRR. +*/ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace GFG +{ + class Program + { + static void Main(String[] args) + { + int t = Int32.Parse(Console.ReadLine()); + for (int i = 0; i < t; i++) + { + int n = Int32.Parse(Console.ReadLine()); + int[] rc = Array.ConvertAll(Console.ReadLine().Split(' '),Int32.Parse); + int[,] grid = new int[n,n]; + bool[,] visited = new bool[n,n]; + for(int j = 0; j < n; j++) + { + for(int k = 0; k < n; k++) + { + grid[j, k] = rc[(j*n)+k]; + } + } + if (grid[0, 0] == 1) RatPuzzle(grid,visited, n, 0, 0, ""); + else Console.WriteLine("not possible"); + } + } + private static void RatPuzzle(int[,] grid,bool[,] visited, int n,int row,int col,string s) + { + visited[row, col] = true; + + if (row >= n || col >= n || row <0 || col<0) return ; + if (row == (n - 1) && col == (n - 1)) + { + Console.WriteLine(s+" "); + } + + if ((row+1)= 0 && grid[row, col - 1] == 1 && !visited[row, col - 1]) + { + RatPuzzle(grid, visited, n, row, col - 1, s+"L"); + } + if ((col + 1) < n && grid[row, col + 1] == 1 && !visited[row,col+1]) + { + RatPuzzle(grid, visited, n, row, col + 1, s+"R"); + } + if ((row-1)>=0 && grid[row-1, col] == 1 && !visited[row-1,col]) + { + RatPuzzle(grid,visited, n, row-1, col, s+"U"); + } + visited[row, col] = false; + } + } +} + \ No newline at end of file diff --git a/BackTracking/SudokuAlgorithm/sudoku.cs b/BackTracking/SudokuAlgorithm/sudoku.cs new file mode 100644 index 00000000..991c2ea2 --- /dev/null +++ b/BackTracking/SudokuAlgorithm/sudoku.cs @@ -0,0 +1,121 @@ +/* +use the below testcase to check(making job easy (formatted one)): +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 +*/ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace temp +{ + class Program + { + static void Main(String[] args) + { + int[,] sudoku = new int[9,9]; + for(int i = 0; i < 9; i++) + { + int[] row = Array.ConvertAll(Console.ReadLine().Split(' '),Int32.Parse); + for(int j = 0; j < 9; j++) + { + sudoku[i, j] = row[j]; + } + } + Console.WriteLine("------------------The solution is:-------------------------"); + if (solveSudoku(sudoku)) Display(sudoku); + else Console.WriteLine("No possible solution."); + } + + private static void Display(int[,] sudoku) + { + for(int i = 0; i < 9; i++) + { + for(int j = 0; j < 9; j++) + { + Console.Write(sudoku[i, j] + " "); + } + Console.WriteLine(); + } + } + + private static bool solveSudoku(int[,] sudoku) + { + int[] rc = new int[2]; + if (sudokuSolved(sudoku,rc)) return true; + + for (int num = 1; num < 10; num++) + { + if (isSafe(sudoku, rc[0], rc[1], num)) + { + sudoku[rc[0], rc[1]] = num; + if (solveSudoku(sudoku)) return true; + sudoku[rc[0], rc[1]] = 0; + } + } + return false; + } + + private static bool isSafe(int[,] sudoku, int r, int c, int num) + { + if (notInRow(sudoku, r, num) && notInCol(sudoku, c, num) && notInGrid(sudoku, (r-(r%3)), (c-(c%3)), num)) return true; + return false; + } + + private static bool notInGrid(int[,] sudoku, int r, int c, int num) + { + for(int i = 0; i <3; i++) + { + for(int j = 0; j <3; j++) + { + if (sudoku[i+r, j+c] == num) return false; + } + } + return true; + } + private static bool notInCol(int[,] sudoku, int c, int num) + { + for(int i = 0; i < 9; i++) + { + if (sudoku[i, c] == num) return false; + } + return true; + } + + private static bool notInRow(int[,] sudoku, int r, int num) + { + for (int i = 0; i < 9; i++) + { + if (sudoku[r, i] == num) return false; + } + return true; + } + + private static bool sudokuSolved(int[,] sudoku,int[] rc) + { + for (int i = 0; i < 9; i++) + { + for (int j = 0; j < 9; j++) + { + if (sudoku[i, j] == 0) + { + rc[0] = i; + rc[1] = j; + return false; + } + } + } + return true; + } + } +} + \ No newline at end of file