I am trying to solve the following question using BackTracking in C but I don't know how to continue from here...
The question is:
Chris is planning to travel in a country with N cities. He will get help from a matrix NxN that the cell (I,J) represents the length of the road from city I to City J. The length of the road from City A to city B is not the same when compared to the road from City B to City A. The Road From the same source city back to it (directly) is 0. Chris noticed that the shortest road From A to B isn't alway the direct one between both Cities. You will need to help Chris Finding the shortest path. Write a function that checks for the shortest map given a matrix NxN which stores the values of the roads lengths. Note: N is defined as 4.
Example:
The Shortest path from 0 to 1 is going to City 0 then 3 then 1 if given the following matrix:
0 5 2 2
1 0 1 1
1 2 0 1
1 1 2 0
Her's my code:
int ShortestPath (int SourceCity, int DestinationCity, int Distance [][N], bool Chosen[][N])
{
int Path=0;
if (SourceCity==DestinationCity)
{
Distance[SourceCity][DestinationCity]=true;
return 0;
}
for (int i=0;i<N;i++)
{
for (int j=0;j<N;j++)
{
Path += Distance[i][j];
if (!Chosen[i][j])
{
Chosen[i][j] = true;
ShortestPath(i, DestinationCity, Distance, Chosen);
}
}
}
if (Path>=Distance[SourceCity][DestinationCity])
{
Chosen[SourceCity,DestinationCity]=false;
return Distance[SourceCity][DestinationCity];
}
}
Note: the matrix chosen indicated wether I chose a specific road or not (Its initial values are all false)