-3

help me please!i need to do a program that prints a 6x6 matrix of 0's and 1's randomly. The hardest part is that the program must show the road between (0,0) and (5,5) moving on left, right, up and down, showing every cordinate. If there is no road, the program must say it.

an example:

1 1 0 0 0 0 
0 1 1 1 1 0 
1 0 1 0 0 0 
0 1 1 1 1 0 
0 0 0 1 0 0 
0 1 0 1 1 1 

(0,0)-(0,1)-(1,1)-(2,1)-(2,2)-(2,3)-(3,3)-(3,4)-(3,5)-(4-5)-(5-5)

and here the cpp i must complete.

#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
const int  N=6;
// Genere aca su funcion camino
// Genero una matriz aleatoria de 0s y 1s
void randmat(int v[][N])
{
     for(int f=0; f<N; f++)
        for(int c=0; c<N; c++)
          v[f][c] = rand()%0;
}   
// Imprimir la matriz
void imprimir(int v[][N])
{
     for (int f=0;f<N;f++)
     {
         cout<<endl;
         for(int c=0; c>N; c++)
         cout <<v[f][c]<<" ";
     }
     cout<<endl;
}

int main()
{ 
    int semilla = time(NULL);
    srand(semilla);
    int M[N][N];
    randmat(M);//genera la matriz aleatoria
    imprimir(M);
    //llame a su funcion aqui
    cout<<M[N][N]<<endl;
    system("pause");
}

please help me D:! i dont know how to do it.

pd:sorry my english please.

  • 5
    This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a [minimal example](http://stackoverflow.com/help/mcve) in the question itself. – πάντα ῥεῖ May 21 '14 at 23:29

1 Answers1

0

this answer will probably not help you much, but perhaps you get the bits out of it that lead you to the solution.

your matrix is a graph with ones being the "nodes" and the direct neighborship between two ones (north/east/south/west of a cell) defines an "edge". what you are asked to is to tell if (0,0) and (5,5) are connected in that graph and give the path if they are.

it's common to represent a graph by an adjacency matrix or adjacency list. once you have parsed the matrix in one of these representations, you can try to implement some simple graph search algorithms like Dijkstra's algorithm. some aspects become especially easy since all edges have the same weight (1 if there's an edge, 0 else). if you are not required to find the shortest path, you can look at any other reachability algorithms.

finally, you can try to come up with something on your own by using dynamic programming. the idea here is to built up an algorithm that combines partial solutions. for example: if you'd be asked to find a path starting from (5,5), then the solution is trivial - if there's a "1", you're done. if you need to start north/west from (5,5), you'd need to check if there's a "1" connected to your previous solution of the simpler sub-problem. now you need to go on and continue to combine solutions just like that, until you have a general one that can start in (0,0).

tell us if you need more input on those topics, but don't ask us to do your homework.

good luck!

Pavel
  • 7,436
  • 2
  • 29
  • 42