Блог пользователя antivenom

Автор antivenom, 9 лет назад, По-английски

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.

  • Проголосовать: нравится
  • +1
  • Проголосовать: не нравится

»
9 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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 лет назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

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

    • »
      »
      »
      9 лет назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      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?)