Chercher un mot dans une matrice

Fermé
balkiss - 28 nov. 2013 à 21:23
balkiss91 Messages postés 13 Date d'inscription dimanche 30 décembre 2012 Statut Membre Dernière intervention 8 décembre 2013 - 30 nov. 2013 à 14:58
Bonsoir, je veux chercher une chaine du carractére dans un matrice qui est déja remplit aleatoirement chaque fois je selection une chaine de carractére qui est dans un fichier puis je parcours matrice pour trouver cet chaine svp aidez moi ..merci d'avance

5 réponses

fabouf Messages postés 1267 Date d'inscription samedi 20 octobre 2007 Statut Membre Dernière intervention 24 décembre 2015 271
Modifié par irongege le 16/12/2013 à 14:15
42
0
fabouf Messages postés 1267 Date d'inscription samedi 20 octobre 2007 Statut Membre Dernière intervention 24 décembre 2015 271
Modifié par irongege le 16/12/2013 à 14:15
0
balkiss91 Messages postés 13 Date d'inscription dimanche 30 décembre 2012 Statut Membre Dernière intervention 8 décembre 2013
28 nov. 2013 à 22:09
ça c'est une reponse merci b1
0
fiddy Messages postés 11069 Date d'inscription samedi 5 mai 2007 Statut Contributeur Dernière intervention 23 avril 2022 1 835
29 nov. 2013 à 07:22
Bonjour,
Montre-nous ton code pour quon puisse t'aider. Notre réponse dépendra de ton implémentation
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
balkiss91 Messages postés 13 Date d'inscription dimanche 30 décembre 2012 Statut Membre Dernière intervention 8 décembre 2013
Modifié par balkiss91 le 30/11/2013 à 15:23
bonjour , voici le code lorsque j'execuse ne marche pas
#include <stdio.h>
#include <string.h>

#define MAX_LIGNES   15
#define MAX_COLONNES 15
#define MAX_LETTRES  15
#define MAX_MOTS     50


void remplirGrille(char [][MAX_COLONNES],char[][MAX_COLONNES],int *, int *);
void afficherGrille(char [][MAX_COLONNES], int , int );
void litMots(char [][MAX_LETTRES+1],int * );
void afficheMots(char [][MAX_LETTRES+1],int );
void chercheMot(char [], char [][MAX_COLONNES], char [][MAX_COLONNES], int , int );
void afficheSolution(char [][MAX_COLONNES], int , int );



void main()
{
   char grille[MAX_LIGNES][MAX_COLONNES];
   char marquer[MAX_LIGNES][MAX_COLONNES];

   char tabDeMots[MAX_MOTS][MAX_LETTRES+1];


   int nbLignes = 0, nbColonnes = 0, nbMots = 0;
   int i;

   remplirGrille(grille,marquer, &nbLignes, &nbColonnes);

   if(nbLignes == 0 || nbColonnes == 0)
    return;

   afficherGrille(grille, nbLignes, nbColonnes);

   litMots(tabDeMots, &nbMots);

   if(nbMots == 0)
    return;

   afficheMots(tabDeMots, nbMots);


   for(i = 0; i < nbMots; i++)
    chercheMot(tabDeMots[i], grille, marquer, nbLignes, nbColonnes);

   afficheSolution(marquer, nbLignes, nbColonnes);
}



void remplirGrille(char grille[][MAX_COLONNES],char marquer[][MAX_COLONNES],int *nbLn, int *nbCol)
{
     int nL=0, nC=0;
  int i, j;

  FILE * fGrille = fopen("dict.txt","r");

  if(fGrille !=NULL)
  {
   fscanf(fGrille,"%d %d\n",&nL,&nC);

   for(i=0;i<nL;i++)
    for(j=0;j<nC;j++)
    {
     fscanf(fGrille,"%c",&grille[i][j]);
     marquer[i][j] = grille[i][j];
    }
   fclose(fGrille);
  }

  *nbLn = nL;
  *nbCol = nC;
}

void litMots(char tabMots[][MAX_LETTRES+1],int * nbMots)
{
 int n = 0,i;

 FILE * fMots = fopen("mots.txt","r");

 if(fMots)
 {
  fscanf(fMots,"%d\n",&n);

  for(i = 0; i<n;i++)
  {
   fscanf(fMots," %s", tabMots[i]);

  }

     fclose(fMots);
 }
 *nbMots = n;
}


void afficherGrille(char grille[][MAX_COLONNES], int nbLignes, int nbColonnes)
{
 int i,j;

 printf("La grille\n\n");
   for(i=0;i<nbLignes;i++)
   {
    for(j=0;j<nbColonnes;j++)
      printf("%c",grille[i][j]);

    printf("\n");
   }

}


void afficheMots(char tabDeMots[][MAX_LETTRES+1],int  nbMots)
{

 int i;

 printf("\n\nLes mots cherches :\n\n");

 for(i = 0; i<nbMots; i++)
  printf("%s\n",tabDeMots[i]);

}

void trouveLesPas( int d, int *h, int *v)
{
 int pasH=0, pasV=0;

 if (d == 1 || d == 2 || d == 8)
      pasH = 1;
 else
  if(d >= 4 && 6 >= d)
   pasH = -1;

 if(2 <= d && d <= 4)
  pasV = 1;
 else
  if( 6<=d && d<=8)
   pasV = -1;

 *h = pasH;
 *v = pasV;
}

int esTuLa(char mot[], char grille[][MAX_COLONNES],int longueur, int i, int j, int pasH, int pasV)
{
 int k;

    for(k = 0; k < longueur; k++, i+=pasV, j+=pasH)
  if(mot[k] != grille[i][j])
      return 0;

 return 1;

}

int cherchePosition(char mot[], char grille[][MAX_COLONNES], int i, int j, int nbLn, int nbCol)
{
   int direction = 1, trouve = 0;

   int pasH, pasV, longueur = strlen(mot);

   while (direction <= 8 && !trouve)
   {

       trouveLesPas(direction, &pasH, &pasV);
       if( pasH > 0 && j + longueur > nbCol)
     direction++;
    else if(pasH < 0 && j - longueur + 1 < 0)
     direction ++;
    else if(pasV > 0 && i + longueur > nbLn)
        direction++;
    else if(pasV < 0 && i - longueur + 1 < 0)
     direction++;
    else
    {
     trouve = esTuLa(mot, grille, longueur, i,j, pasH, pasV);
           if(!trouve)
      direction++;
    }
   }
   if(trouve)
   {
    return direction;
   }
   else
    return 0;

}


void chercheMot(char mot[], char grille[][MAX_COLONNES], char marquer[][MAX_COLONNES], int nbLn, int nbCol)
{

/*   les directions :

  0 Le mot ne commence pas à cette position
  1 Horizontale vers la droite
  2 diagonale vers le bas et la droite
  3 verticale vers le bas
  4 diagonale vers le bas et la gauche
  5 horizontale vers la gauche
  6 diagonale vers le haut et la gauche
  7 verticale vers le haut
  8 diagonale vers le haut et la droite
*/

 int i = 0, j = 0, k, direction = 0;
 int pasH=0, pasV=0;
    int longueur = strlen(mot);

 while( direction == 0 )
 {

  if(mot[0] == grille[i][j])
   direction = cherchePosition(mot, grille, i, j, nbLn, nbCol);

  if(direction == 0)
   if(j%nbCol == 0 && j>0)
    i++, j=0;
   else
    j++;
   if(i>=nbLn)
    return;
 }


    trouveLesPas(direction, &pasH, &pasV);

 for(k=0; k < longueur; k++, i+=pasV, j+=pasH)
  marquer[i][j] = '-';



}


void afficheSolution(char marquer[][MAX_COLONNES], int nbLn , int nbCol)
{
 int i,j;

 printf("\nLa solution est : ");
 for(i = 0; i < nbLn; i++)
 { for(j = 0; j < nbCol; j++)
   if(marquer[i][j] != '-')
    printf("%c", marquer[i][j]);

 }

 printf("\n\n");
}
0