antivenom's blog

By antivenom, 9 years ago, In English

Hello everyone.Please help me in this question.I tried implementing some sort of recursive solution.One like maze solving.I am fairly new to recursion and badly trapped in this one.Help me.My solution run endlessly.Here is what I did.Please improve my solution.

  • Vote: I like it
  • +1
  • Vote: I do not like it

| Write comment?
»
9 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Don't go with a way that's not true, this is going to cost alot of time so you can keep track of the index of the original string, so the function will look like:

void backtrack(int x, int y, int i) { ... }

And when you want to go to for example (x+1, y+1) you should check this:

if (x+1 < n && y+1 < n && mat[x+1][y+1]  == final[i])

     backtrack(x+1, y+1, i+1); 

And the same for the 3 remaining calls.

Base case: (i == final.size())

  • »
    »
    9 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Thanks a lot :) Can you tweak my code little bit and make it working...because that will clear the things even better.

    • »
      »
      »
      9 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      If s is global the s.size() == final.size() will follow only once. make s an argument of the function and also you should check the function for every position of the matrix. code. ( why is n 8 instead of 5?)