Question c++
alexpower
-
dindoun Messages postés 1028 Date d'inscription Statut Membre Dernière intervention -
dindoun Messages postés 1028 Date d'inscription Statut Membre Dernière intervention -
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 !!!!
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 !!!!
7 réponses
à 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){...}
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){...}
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)
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question
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;
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;
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).
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).
#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]); }