using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Recursion
{
class Program
{
static void Main(string[] args)
{
int[,] maze = {{1,1,0,1 },
{0,1,1,1 },
{0,1,0,1 },
{0,1,1,1 } };
int[,] visited = {{0,0,0,0 },
{0,0,0,0 },
{0,0,0,0 },
{0,0,0,0 }};
visited[0, 0] = 1;
FindPathInMaze(maze, visited, 0, 0, 3, 3, 1);
}
static int[] Path_Row = { 0, 0, 1, -1 };
static int[] Path_Col = { 1, -1, 0, 0 };
public static void FindPathInMaze(int[,] maze, int[,] visited, int row, int col, int desRow, int desCol, int move)
{
if ((row == desRow) && (col == desCol))
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
Console.Write($"{visited[i, j]}, ");
}
Console.WriteLine();
}
Console.WriteLine("//////////////////////////////////////////////////");
}
else
{
for (int index = 0; index < Path_Row.Length; index++)
{
int rowNew = row + Path_Row[index];
int colNew = col + Path_Col[index];
if (CanWeMove(maze, visited, rowNew, colNew))
{
move++;
visited[rowNew, colNew] = move;
FindPathInMaze(maze, visited, rowNew, colNew, desRow, desCol, move);
move--;
visited[rowNew, colNew] = 0;
}
}
}
}
static bool CanWeMove(int[,] maze, int[,] visited, int rowNew, int colNew)
{
if ((rowNew >= 0) && (rowNew < 4) && (colNew >= 0) && (colNew < 4) && (maze[rowNew, colNew] == 1) && (visited[rowNew, colNew] == 0))
{
return true;
}
return false;
}
}
}
What I have tried:
#include<iostream>
I tried to convert It but it is giving me error when initializing
using namespace std;
int [,] maze = {{1,1,0,1},{0,1,1,1},{0,1,0,1},{0,1,1,1}};
int [,] visited = {{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0}};
static int[] Path_Row= {0, 0, 1, -1};
static int[] Path_Col= {1,-1, 0, 0};
void FindPath(int[,] maze,int[,] visited,int row,int col,int desRow,int desCol,int move)
{
if((row==desRow && (col==desCol)))
{
for(int i=0 ; i< 4; i++)
{
for(int j=0; j<4 ;j++)
{
cout<<""<<visited[i,j]<<endl;
}
}
cout<<"**************************************"<<endl;
}
else
{
for(int index =0; idex <Path_Row.Length; index++)
{
int rowNew = row +Path_Row[index];
int colNew = col +Path_Col[index];
if(CanMove(maze , visited , rowNew, colNew))
{
move++;
visited[rowNew,colNew] = move;
FindPath(maze,visited,rowNew,colNew,desRow,desCol,move);
move--;
visited[rowNew,colNew] = 0;
}
}
}
}
bool CanMove(int[,] maze, int[,] visited, int rowNew, int colNew)
{
if((rowNew>=0) && (rowNew<3) && (colNew>=0) && (colNew < 3) (maze[rowNew,colNew]==1) && (visited[rowNew,colNew]==0))
{
return true;
}
return false;
}