Chercher un mot dans une matrice

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

  1. ça c'est une reponse merci b1
    0
    1. Contributeur
      Bonjour,
      Montre-nous ton code pour quon puisse t'aider. Notre réponse dépendra de ton implémentation
      0
      1. 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