Question c++

alexpower -  
dindoun Messages postés 1047 Statut Membre -
Bonjour, voici un problème rédigé en anglais,,,,est-ce que quelqu'un pourrait m'aider a y trouver une solutioin ??

We have a 2D integer array filled with the values 0 and 1.
0 means ground and 1 means wall.
We can only walk up, down, right or left.
You’re allowed to modify the array to hold temporary values.

We’d like to know if a path exists from (0,0) to (width-1, height-1)

Create a function to do this, using one of the following prototypes:

C++: boolean findZeroArraySize(const int *array, int width, int height);

Input:
array – Pointer to the 2D array
width – Width of the 2D array
height – Height of the 2D array
Output:
Return value – true if a path exists, false otherwise

For example, given the array:

001011110
100001100
010000011
110001000
111011100

The function would return true.

Merci d'avance :) j'ai hâte de voir comment s'u prendre !!!!
Configuration: Windows Vista
Firefox 3.0.3

7 réponses

  1. dindoun Messages postés 1047 Statut Membre 135
     
    l algo marche
    je vois pas le pb des bords alors que le deuxième test est censé le régler

    1
  2. Char Snipeur Messages postés 10112 Date d'inscription   Statut Contributeur Dernière intervention   1 331
     
    à la place de ((tableau[i+1][j]==0)&&(i+1<height)) essaie ((i+1<height)&&(tableau[i+1][j]==0))
    car si i== height-1 i+1== height et donc le test tableau[i+1][j]==0 doit générer une erreur.
    Si ça ne fonctionne toujours pas (mais normalement si la première condition est fausse la deuxième n'est pas évalué), il faudra couper le if en deux :
    if(i+1<height)if(tableau[i+1][j]==0){...}
    1
  3. dindoun Messages postés 1047 Statut Membre 135
     
    tout à fait
    1
  4. toto
     
    par contre les tests des indices tableau[i+1][j]==0)&&(i+1<height) n'est pas top sur les bords (c'est le cas de le dire)
    0
  5. Vous n’avez pas trouvé la réponse que vous recherchez ?

    Posez votre question
  6. dindoun Messages postés 1047 Statut Membre 135
     
    solution=false;
    tableau[0][0]=2;
    int temp;
    temp=1;
    while (temp !=0){
    temp=0;
    for (int i = 0; i<height;i++){for (int j = 0; j<width;j++){

    if ( tableau[i][j]==2 ) {

    if{ tableau[i+1][j]==0){ tableau[i+1][j]=2;temp++;}
    if{ tableau[i-1][j]==0){ tableau[i-1][j]=2;temp++;}
    if{ tableau[i1][j-1]==0){ tableau[i][j-1]=2;temp++;}
    if{ tableau[i1][j+1]==0){ tableau[i][j+1]=2;temp++;}

    }

    }}

    }
    resultat=false;
    if ( tableau[height][width]==2 ) { resultat=true;}
    return resultat;
    -1
  7. Utilisateur anonyme
     
    Salut,
    Avec google translate :

    Nous avons un tableau 2D rempli avec les valeurs 0 et 1.
    0 signifie terrain et 1 signifie mur.
    Nous ne pouvons que marcher haut, bas, gauche ou la droite.
    Vous êtes autorisé à modifier le tableau de tenir des valeurs temporaires.

    Nous aimerions savoir si un chemin existe de (0,0) à (largeur-1, hauteur-1)

    Créer une fonction pour ce faire, utilisez l'un des prototypes suivants:

    C: boolean findZeroArraySize (const int * array, int largeur, hauteur int);

    Input:
    array - Pointeur vers le tableau 2D
    largeur - Largeur de la gamme 2D
    hauteur - Hauteur de la série 2D
    Output:
    Valeur de retour - si un vrai chemin d'accès existe, faux sinon

    Par exemple, étant donné le tableau:

    001011110
    100001100
    010000011
    110001000
    111011100

    La fonction retourne 1 (vrai).
    -1
  8. dindoun Messages postés 1047 Statut Membre 135
     
    #include <stdio.h>
    
    
    int main(void){
    
    
    int width,height;
    width=9;
    height=5;
    
    int temp,i,j;
    //int tableau[width][height]={{0,0},{0,0}};
    int tableau[5][9]={
    {0,0,1,0,1,1,1,1,0},
    {1,0,0,0,0,1,1,0,0},
    {0,1,0,0,0,0,0,1,1},
    {1,1,0,0,0,1,0,0,0},
    {1,1,1,0,1,1,1,0,0}
    };
    //int tab1[width]={0,0,1,0,1,1,1,1,0 }
    
    for (i = 0; i<height;i++){for ( j = 0; j<width;j++){
    	printf("%d",tableau[i][j]);}
    	printf("\n");
    	}	
    	
    
    
    
    //tableau[0][0]=2;
    
    tableau[0][0]=2;
    
    
    temp=1;
    while (temp !=0){
    	temp=0;
    	for (i = 0; i<height;i++){for ( j = 0; j<width;j++){
    
    		if ( tableau[i][j]==2 ) {
    
    			if ((tableau[i+1][j]==0)&&(i+1<height)) { tableau[i+1][j]=2;temp++;}
    			if ((tableau[i-1][j]==0)&&(i-1>=0)) { tableau[i-1][j]=2;temp++;}
    			if ((tableau[i][j-1]==0)&&(j-1>=0)) { tableau[i][j-1]=2;temp++;}
    			if ((tableau[i][j+1]==0)&&(j+1<width)) { tableau[i][j+1]=2;temp++;}
    
    			}
    
    		}}
    for (i = 0; i<height;i++){for ( j = 0; j<width;j++){
    	printf("%d",tableau[i][j]);}
    	printf("\n");
    	}	
    		printf(" \n");
    
    	}
    
    
    printf("%d \n",tableau[height-1][width-1]);
    
    
    
    
    
    }
    
    -1
    1. Char Snipeur Messages postés 10112 Date d'inscription   Statut Contributeur Dernière intervention   1 331
       
      Tu tente la méthode de la tache d'huile ?
      ton algo semble pas mal.
      0