0

I got for my school project a task that I dont know how to solve and I'm stuck. I have to run over this labyrinth:

    #T###########
    #.#...R.....#
    #.###.#.###.#
    #...Q.#...#.#
    #.#####C###F#
    #.A.........#
    #B#####E#K#L#
    #.......#.#.#
    ###D#H###.#.#
    #...#...J.P.#
    #G###X#####.#
    #.........N.#
    #############

And in this matrix, I have to find which significant point is whose neighbour using Linked list.

This should be the output of the code :

    A: L K F E C T Q B
    B: H E D T Q A
    C: L K F E A R F
    D: G H E B
    E: H D B L K F C A
    F: L K E C A R C
    G: X N D
    H: X J E D B
    J: X H P K
    K: P J L F E C A
    L: P N K F E C A
    N: X G P L
    P: N L K J
    Q: R T B A
    R: F C Q
    T: Q B A
    X: N G J H


  int rekurzia(int x,int y)
  {
    if ((x < 0) || (x > MAX - 1) || (y < 0) || (y > MAX - 1))
      return false;
    if((matica[x][y] >= 'A') && (matica[x][y] <= 'Z'))
      printf("%c\n", matica[x][y]);
    if (matica[y][x] != '.' ) 
      return false;
    if (rekurzia(x,y-1) == true)
      return true;
    if (rekurzia(x+1,y) == true)
      return true;
    if (rekurzia(x,y+1) == true)
      return true;
    if (rekurzia(x-1,y) == true) 
      return true;  
    return false;
  }

I do something like this. Can somebody help me how I'm supposed to do this ? Thanks !

Rastislav
  • 11
  • 1

1 Answers1

1

This is a super challenging problem for a school project! But looks like Fun!

If I would have to solve this, I would;

  1. Get all the addresses of each alpha character so I know where all the start points are (simply walk the matrix to do that (loop inside a loop)
  2. construct a recursive function (a function that keeps calling itself) and I would then allow the function to;
    • spawn off in 4 vectors (up, down, left, right).
    • if the function encounters a "#" (wall) it would return nothing and stop,
    • if it found a "." corridor it would continue in 4 more vectors,
    • if it encountered a alpha character, return that character.
  3. for each known alpha position call the recursive function and let it "roam".
  4. for extra points you could use parallel programming to simultaneously start each known position :)

gotchas: - watch out for endless calls - don't roam past the boundaries of the matrix

  • There is no obvious need to use recursion here. It should, like always, be avoided like the plague and only be used as the very last resort. – Lundin May 09 '17 at 09:49
  • I would be interested to see how one would solve it with a linear program. I managed to build this in c# with a recursive function and it's pretty compact. my result is technically more correct, as I find the closest points first, which is not the case in the result above. – Paul Wandrag May 09 '17 at 22:15
  • All recursion does is to save the previous location. It can always be replaced with a loop and a LIFO holding the previous location. Or alternatively with a BST that holds a pointer to the parent node. The only time recursion will be "compact" is when the compiler manages to unroll it, which often requires tail recursion. What exactly is "compact", your source code, the machine code or the stack usage? The first one of those three is quite irrelevant. – Lundin May 10 '17 at 06:28