Skip to content

Commit

Permalink
added other language implementation of backtracking algorithms.
Browse files Browse the repository at this point in the history
  • Loading branch information
anirudherabelly committed Oct 15, 2018
1 parent a27972e commit ce2530e
Show file tree
Hide file tree
Showing 5 changed files with 416 additions and 0 deletions.
70 changes: 70 additions & 0 deletions BackTracking/KnightsTour/KnightsTour.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}

106 changes: 106 additions & 0 deletions BackTracking/NQueens/NQueens.cs
Original file line number Diff line number Diff line change
@@ -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<n && j >= 0; i++, j--)
{
if (board[i, j] == 1) return false;
}
return true;
}
}
}

43 changes: 43 additions & 0 deletions BackTracking/Permutation/GeneratePermutations.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
76 changes: 76 additions & 0 deletions BackTracking/RatInAMaze/RatInMaze.cs
Original file line number Diff line number Diff line change
@@ -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)<n && grid[row + 1, col] == 1 && !visited[row+1,col])
{
RatPuzzle(grid, visited, n, row + 1, col, s+"D");
}
if ((col - 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;
}
}
}

Loading

0 comments on commit ce2530e

Please sign in to comment.