Click here to Skip to main content
15,896,912 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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;
}
Posted
Updated 21-Nov-20 11:32am
Comments
Richard MacCutchan 21-Nov-20 9:35am    
Sorry, this site does not provide free code conversion. If your code has a problem then please explain in proper details, and show where in the code the problem lies.
PIEBALDconsult 21-Nov-20 9:55am    
Yes, someone can, but, no, no one will.
Patrice T 21-Nov-20 10:41am    
Give exact error message.

1 solution

Try:
C++
int maze[][4] = {{1,1,0,1},{0,1,1,1},{0,1,0,1},{0,1,1,1}};
int visited[][4] = {{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};

But that's not going to be the end of your problems ...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900