We want to have the village that this person has passed to get from start to the end.
1/the user should enter the number of villages and translate the path to a matrix
we have a matrix dynamic allocation
a b c d e f
are the villages
if there is a bridge between two villages 1
and if not 0
Note: The user is the one who fill the matrix and the leters not included
*************.************************************************************************************************** What I chose to do but I think that we can use recursive too
******************the loop********************
The loop start line by line
If it gets a 0
then i++
else if it get 1
it print the location and then it jump to other line
Example 1:
It start from a[0][0]
to b[0][1]
then jump the pointer jump to b[1][0]
and
we set b
to 0
in the two cases. So we dont have a repetition of the same case
In the previous example it should print the Coordinates of a and b
and c and d
In example 2 it should print the Coordinates of a c b d f
here is my try
void permut(int *x, int *y)
{
int c;
c = *x;
*x = *y;
*y = c;
}
main()
{
int x, y;
//creation
cout << "donner la talle de matrice (ligne puis colone) : \n";
cin >> x >> y;
int **grid = new int *[y];
for (int i = 0; i < y; ++i)
{
grid[i] = new int[x];
}
for (int i = 0; i < x; i++)
{
for (int j = 0; j < y; j++)
{
cout << "t[" << i << "][" << j << "] : ";
cin >> grid[i][j];
}
}
for (int i = 0; i < x; i++)
{
cout << "\n";
for (int j = 0; j < y; j++)
{
cout << grid[i][j] << "\t";
}
cout << "\n";
}
cout << "\n\n\n";
do
{
int i , j;
if (grid[i][j] == 1)
{
cout << i << " " << j << "\n";
permut(&i, &j);
grid[i][j] = 0;
grid[j][j] = 0;
continue;
}
else if (grid[i][j] == 0)
{
i++;
continue;
}
else
break;
}while (1);
//delete
for (int i = 0; i < y; ++i)
{
delete[] grid[i];
}
delete[] grid;
system("pause");
return 0;
}