using System;
using System.Collections.Generic;
namespace Algorithm
{
class MainClass
{
static readonly int MAX = 5;
static void getallpath(int[,] matrix, int n, int row,
int col, List<string> ans,
string cur)
{
if (row >= n || col >= n || row < 0 || col < 0
|| matrix[row, col] == 0)
{
return;
}
if (row == n - 1 && col == n - 1)
{
ans.Add(cur);
return;
}
matrix[row, col] = 0;
getallpath(matrix, n, row - 1, col, ans, cur + "U");
getallpath(matrix, n, row, col + 1, ans, cur + "R");
getallpath(matrix, n, row, col - 1, ans, cur + "L");
getallpath(matrix, n, row + 1, col, ans, cur + "D");
matrix[row, col] = 1;
return;
}
static List<string> findPath(int[,] matrix, int n)
{
List<string> ans = new List<string>();
getallpath(matrix, n, 0, 0, ans, "");
return ans;
}
public static void Main(string[] args)
{
int[,] m = { { 1, 0, 0, 0, 0 },
{ 1, 1, 1, 1, 1 },
{ 1, 1, 1, 0, 1 },
{ 0, 0, 0, 0, 1 },
{ 0, 0, 0, 0, 1 } };
int n = m.GetLength(0);
List<string> ans = findPath(m, n);
foreach (string i in ans)
{
Console.Write(i + " ");
}
}
}
}