-
Notifications
You must be signed in to change notification settings - Fork 5
/
RatInMaze.cs
76 lines (70 loc) · 2.57 KB
/
RatInMaze.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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;
}
}
}