Matrix of letters

closed account (23q2T05o)
Hi there, so we have an int n and then we type in the console n words, they have to be on a new line each, then we have an int k, k is from 2 to 5 /we choose/ and then we have a matrix of chars and i gotta generate all the words in this matrix and to find if any of the words from the matrix match with any of the n words /the ones i mentioned in the beg/. also an important thing is that we start from a random position, we cant go to a position we have been to already.we can go to the position on the right, on the left, the upper position, the bottom position and the ones that are on the diagnol /we are allowed to search in all 8 directions/
so the question is how to generate all the possible words, i know how to do the stuff before that
Last edited on
closed account (23q2T05o)
cant use string.h and vectors
Last edited on
explain the position thing better.
why do you want to generate all the words, that would take forever. You should eliminate... if none of the words typed in has an X but the matrix has 10 X's in it, there is a lot of words you don't need to make, or a lot of invalid sections of the grid. Also, what is a 'word' anyway, if the user types in flxbg in the start, is that a 'word' you need to look for?
closed account (23q2T05o)
no i gotta validate the word
and for the position we start from a random postion
lets say we have this matrix

a b c
d f o
l p e

lets say we start from f, we check that we have been to f, we can go to a, b, c, d, o, l, p or e
lets say we go to b, we mark b as a positon we have been to, from b we can go to a, c, d or o
lets say we go to o, we mark o as a position we have been to, we can go to p or e
lets say we go to p, we mark it again, we can go to l, e or d
and so on unitil we have position we havent visited
and like that we gotta generate words and check if they are part of the words that the user has typed and that we have valited them
i hoe i explained it
Example.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <stdio.h>

#define INSIDE(x,n) (x>=0 && x<n)

void generate(char grid[3][3], int x, int y, int depth) {
  if ( depth == 0 ) return;
  if ( !INSIDE(x,3) || !INSIDE(y,3) ) return;
  for ( int r = -1 ; r <= +1 ; r++ ) {
    for ( int c = -1 ; c <= +1 ; c++ ) {
      if ( r == 0 && c == 0 ) continue;
      int rx = x + r;
      int cy = y + c;
      if ( !INSIDE(rx,3) || !INSIDE(cy,3) ) continue;
      printf("(%d) %c leads to %c\n", depth, grid[x][y], grid[rx][cy]);
      generate(grid,rx,cy,depth-1);
    }
  }
}

int main( ) {
  char grid[3][3] = {
    "abc",
    "dfo",
    "lpe"
  };
  generate(grid,1,1,2);
}

$ gcc foo.c
$ ./a.out 
(2) f leads to a
(1) a leads to b
(1) a leads to d
(1) a leads to f
(2) f leads to b
(1) b leads to a
(1) b leads to c
(1) b leads to d
(1) b leads to f
(1) b leads to o
// snipped for brevity


What you need to add
- accumulate the characters as you go to form words
- check said words in your dictionary
- add something to track which letters have been used, so you don't end up with say "fafafafa".

closed account (23q2T05o)
thank youu!
may help..
some programs like this make a fake border around the grid, to know when to stop looking easier, eg if your chars had a '\0' char on the outside edges, when you found one of those, you know its the edge without all the border checking logic.

if you are allowed to optimize at it, a table of letters to locations would really cut a lot of junk.

a b c h
d f o m
l p e q
e z y k

e for example would have 2 table entries, (2,2) and (3,0)
so if your user put in the word epoch you grab your e starting points and search only those.
no need to see if kq or ky will get you there.

f you can't optimize, a cheap way to do it would be to make all combinations you can starting from each letter, all concat into one big string. Search that string for each word, with find(), if its in there, you found it. be sure to break apart groups -- when you start a new chain, put a space into the big string so your search does not find valid results that were not real.
eg "abch abcm abco abcf abfo ..." you probably need to write your own find() because school. Given your limitations, this may not be any easier... it would be if you had full use of c++ tools
Last edited on
Topic archived. No new replies allowed.