Skip to content

Commit a809eb4

Browse files
Merge pull request #576 from anirudherabelly/master
added other language implementation of backtracking algorithms.
2 parents f31b10f + ce2530e commit a809eb4

File tree

5 files changed

+416
-0
lines changed

5 files changed

+416
-0
lines changed
+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
move Knight in a chess board such that it covers all the cells atleast once.
3+
*/
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Text;
8+
using System.Threading.Tasks;
9+
10+
namespace GFG
11+
{
12+
class Program
13+
{
14+
static void Main(String[] args)
15+
{
16+
int[,] board = new int[8,8];
17+
Intialize(board);
18+
int[] rowx = { 2, 1, -1, -2, -2, -1, 1, 2 };
19+
int[] coly = { 1, 2, 2, 1, -1, -2, -2, -1 };
20+
if (KnightsTour(board, 0, 0, 1,rowx,coly)) Display(board);
21+
else Console.WriteLine("Solution does not exist");
22+
}
23+
private static void Intialize(int[,] board)
24+
{
25+
for (int i = 0; i < 8; i++)
26+
{
27+
for (int j = 0; j < 8; j++)
28+
{
29+
board[i, j]=-1;
30+
}
31+
}
32+
board[0, 0] = 0;
33+
}
34+
private static void Display(int[,] board)
35+
{
36+
for(int i = 0; i < 8; i++)
37+
{
38+
for(int j = 0; j < 8; j++)
39+
{
40+
Console.Write(board[i,j]+" ");
41+
}
42+
Console.WriteLine();
43+
}
44+
}
45+
private static bool KnightsTour(int[,] board, int row, int col,int move,int[] rowx,int[] coly)
46+
{
47+
if (move == 64) return true;
48+
int nextx, nexty;
49+
for(int k = 0; k < 8; k++)
50+
{
51+
nextx = row+rowx[k];
52+
nexty = col+coly[k];
53+
if (IsSafe(board, nextx, nexty))
54+
{
55+
board[nextx, nexty] = move;
56+
if(KnightsTour(board,nextx,nexty,move+1,rowx,coly))return true;
57+
board[nextx, nexty] = -1;
58+
}
59+
}
60+
return false;
61+
}
62+
63+
private static bool IsSafe(int[,] board, int row, int col)
64+
{
65+
if (row >= 0 && row < 8 && col >= 0 && col < 8 && board[row, col] == -1) return true;
66+
return false;
67+
}
68+
}
69+
}
70+

BackTracking/NQueens/NQueens.cs

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other.
3+
Given an integer n, print all distinct solutions to the n-queens puzzle.
4+
Each solution contains distinct board configurations of the n-queens’ placement,
5+
where the solutions are a permutation of [1,2,3..n] in increasing order,
6+
here the number in the ith place denotes that the ith-column queen is placed in the row with that number.
7+
For eg below figure represents a chessboard [3 1 4 2].
8+
[0,0,Q,0]
9+
[Q,0,0,0]
10+
[0,0,0,Q]
11+
[0,Q,0,0]
12+
Input:
13+
The first line of input contains an integer T denoting the no of test cases.
14+
Then T test cases follow. Each test case contains an integer n denoting the size of the chessboard.
15+
16+
Output:
17+
For each test case, output your solutions on one line where each solution is enclosed in square brackets
18+
'[', ']' separated by a space . The solutions are permutations of {1, 2, 3 …, n} in increasing order where
19+
the number in the ith place denotes the ith-column queen is placed in the row with that number,
20+
if no solution exists print -1.
21+
22+
Constraints:
23+
1<=T<=10
24+
1<=n<=10
25+
26+
Example:
27+
Input
28+
2
29+
1
30+
4
31+
Output:
32+
[1 ]
33+
[2 4 1 3 ] [3 1 4 2 ]
34+
*/
35+
using System;
36+
using System.Collections.Generic;
37+
using System.Linq;
38+
using System.Text;
39+
using System.Threading.Tasks;
40+
41+
namespace GFG
42+
{
43+
class Program
44+
{
45+
static bool resultFound=false;
46+
static void Main(String[] args)
47+
{
48+
int t = Int32.Parse(Console.ReadLine());
49+
for(int i = 0; i < t; i++)
50+
{
51+
int n= Int32.Parse(Console.ReadLine());
52+
int[,] board = new int[n,n];
53+
Nqueen(board,0,n);
54+
if (!resultFound) Console.Write(-1);
55+
Console.WriteLine();
56+
}
57+
}
58+
private static void Display(int[,] board, int n)
59+
{
60+
Console.Write("[");
61+
for(int i = 0; i < n; i++)
62+
{
63+
for(int j = 0; j < n; j++)
64+
{
65+
if (board[j, i] == 1)Console.Write(j + 1+" ");
66+
}
67+
}
68+
Console.Write("]");
69+
}
70+
private static void Nqueen(int[,] board, int col, int n)
71+
{
72+
if (col >= n)
73+
{
74+
resultFound = true;
75+
Display(board,n);
76+
return;
77+
}
78+
for(int i = 0; i < n; i++)
79+
{
80+
if (IsSafe(board, i, col, n))
81+
{
82+
board[i, col] = 1;
83+
Nqueen(board, col + 1, n);
84+
board[i, col] = 0;
85+
}
86+
}
87+
}
88+
private static bool IsSafe(int[,] board, int row, int col, int n)
89+
{
90+
for(int i = 0; i < col; i++)
91+
{
92+
if (board[row, i] == 1) return false;
93+
}
94+
for(int i=row,j=col;i>=0 && j >=0; i--, j--)
95+
{
96+
if (board[i, j] == 1) return false;
97+
}
98+
for(int i=row,j=col;i<n && j >= 0; i++, j--)
99+
{
100+
if (board[i, j] == 1) return false;
101+
}
102+
return true;
103+
}
104+
}
105+
}
106+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
sb=abc
3+
4+
output:
5+
abc
6+
acb
7+
bac
8+
bca
9+
cba
10+
cab
11+
*/
12+
using System;
13+
using System.Text;
14+
15+
namespace Hackerearth
16+
{
17+
class Program
18+
{
19+
static void Main(string[] args)
20+
{
21+
StringBuilder sb = new StringBuilder(Console.ReadLine());
22+
int n = sb.Length;
23+
GeneratePermutations(sb,0,n-1);
24+
}
25+
private static void GeneratePermutations(StringBuilder sb, int l,int r)
26+
{
27+
if (l == r) Console.WriteLine(sb);
28+
for (int i = l; i <= r; i++)
29+
{
30+
swap(sb, l,i);
31+
GeneratePermutations(sb,l+1,r);
32+
swap(sb,l,i);
33+
}
34+
35+
}
36+
private static void swap(StringBuilder sb, int i, int j)
37+
{
38+
char temp = sb[i];
39+
sb[i] = sb[j];
40+
sb[j] = temp;
41+
}
42+
}
43+
}

BackTracking/RatInAMaze/RatInMaze.cs

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
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).
3+
Your task is to complete the function which returns a sorted array of strings denoting all the possible directions
4+
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),
5+
'D'(down), 'L' (left), 'R' (right).
6+
7+
For example
8+
1 0 0 0
9+
1 1 0 1
10+
1 1 0 0
11+
0 1 1 1
12+
13+
For the above matrix the rat can reach the destination at (3, 3) from (0, 0) by two paths ie DRDDRR and DDRDRR
14+
when printed in sorted order we get DDRDRR DRDDRR.
15+
*/
16+
using System;
17+
using System.Collections.Generic;
18+
using System.Linq;
19+
using System.Text;
20+
using System.Threading.Tasks;
21+
22+
namespace GFG
23+
{
24+
class Program
25+
{
26+
static void Main(String[] args)
27+
{
28+
int t = Int32.Parse(Console.ReadLine());
29+
for (int i = 0; i < t; i++)
30+
{
31+
int n = Int32.Parse(Console.ReadLine());
32+
int[] rc = Array.ConvertAll(Console.ReadLine().Split(' '),Int32.Parse);
33+
int[,] grid = new int[n,n];
34+
bool[,] visited = new bool[n,n];
35+
for(int j = 0; j < n; j++)
36+
{
37+
for(int k = 0; k < n; k++)
38+
{
39+
grid[j, k] = rc[(j*n)+k];
40+
}
41+
}
42+
if (grid[0, 0] == 1) RatPuzzle(grid,visited, n, 0, 0, "");
43+
else Console.WriteLine("not possible");
44+
}
45+
}
46+
private static void RatPuzzle(int[,] grid,bool[,] visited, int n,int row,int col,string s)
47+
{
48+
visited[row, col] = true;
49+
50+
if (row >= n || col >= n || row <0 || col<0) return ;
51+
if (row == (n - 1) && col == (n - 1))
52+
{
53+
Console.WriteLine(s+" ");
54+
}
55+
56+
if ((row+1)<n && grid[row + 1, col] == 1 && !visited[row+1,col])
57+
{
58+
RatPuzzle(grid, visited, n, row + 1, col, s+"D");
59+
}
60+
if ((col - 1) >= 0 && grid[row, col - 1] == 1 && !visited[row, col - 1])
61+
{
62+
RatPuzzle(grid, visited, n, row, col - 1, s+"L");
63+
}
64+
if ((col + 1) < n && grid[row, col + 1] == 1 && !visited[row,col+1])
65+
{
66+
RatPuzzle(grid, visited, n, row, col + 1, s+"R");
67+
}
68+
if ((row-1)>=0 && grid[row-1, col] == 1 && !visited[row-1,col])
69+
{
70+
RatPuzzle(grid,visited, n, row-1, col, s+"U");
71+
}
72+
visited[row, col] = false;
73+
}
74+
}
75+
}
76+

0 commit comments

Comments
 (0)