recursion - Find text path through character matrix with recursive algorithm -


i'm trying solve question: http://www.spoj.com/problems/allizwel/

find whether there path in given matrix makes sentence “all izz well”.

there path cell neighbouring cells.
neighbour may share edge or corner.

input specification:
first line consists of integer t representing number of test cases.
first line of each test case consists of 2 integers r , c representing number of rows , number of columns in matrix.

output specification:
each test case print “yes” if there path makes sentence “allizzwell”.
else print “no”.

for sample test cases, open link.

my code:

#include <iostream> #include <map> #include <vector> #include <string> #include <utility> #include <algorithm> #include <stack> #include <queue> #include <climits> #include <set> using namespace std;  char matrix[101][101]; bool var; int r,c; bool check (string str,int pos, bool visited[101][101],int i, int j); int main (void) {     int t,i,j;     cin>>t;     bool ans;     while (t != 0)     {         int r,c,flag=0;         cin>>r>>c;         ( = 0; < r; i++ )         {             ( j = 0; j < c; j++ )             {                 cin>>matrix[i][j];             }          }         string str = "allizzwell";         int pos = 1;         ( = 0; < r; i++ )         {             ( j = 0; j < c; j++ )             {                 bool visited[101][101];                 ( = 0; < 101; i++ )                     ( j = 0; j < 101; j++ )                         visited[i][j] = false;                 visited[i][j] = true;                 if (matrix[i][j] == 'a') // possible starting positions                 ans = check(str,pos,visited,i,j);                  if (ans == true)                 {                     cout<<"yes\n";                     flag = 1;                     break;                 }                 if (flag == 1)                     break;             }         }         if (flag == 0)             cout<<"no\n";         t--;     }     return 0; }  bool check (string str,int pos, bool visited[101][101],int i, int j) // checking possible test cases {     bool result = false;     if (pos == str.length() + 1)         return true;     if (i+1 < r && visited[i+1][j] != true && matrix[i+1][j] == str[pos])     {         visited[i+1][j] = true;         result = result || check(str,pos+1,visited,i+1,j);         if (result == false)             visited[i+1][j] = false;     }     else if (i-1 >= 0 && visited[i-1][j] != true && matrix[i-1][j] == str[pos])     {         visited[i-1][j] = true;         result = result || check(str,pos+1,visited,i-1,j);         if (result == false)             visited[i-1][j] = true;     }     else if (j+1 < c && visited[i][j+1] != true && matrix[i][j+1] == str[pos])     {         visited[i][j+1] = true;         result = result || check(str,pos+1,visited,i,j+1);         if (result == false)             visited[i][j+1] = true;     }     else if (j-1 >= 0 && visited[i][j-1] != true && matrix[i][j-1] == str[pos])     {         visited[i][j-1] = true;         result = result || check(str,pos+1,visited,i,j-1);         if (result == false)             visited[i][j-1] = true;     }     else if (i+1 < r && j+1 < c && visited[i+1][j+1] != true && matrix[i+1][j+1] == str[pos])     {         visited[i+1][j+1] = true;         result = result || check(str,pos+1,visited,i+1,j+1);         if (result == false)             visited[i+1][j+1] = true;     }     else if (i+1 < r && j-1 >= 0 && visited[i+1][j-1] != true && matrix[i+1][j-1] == str[pos])     {         visited[i+1][j-1] = true;         result = result || check(str,pos+1,visited,i+1,j-1);         if (result == false)             visited[i+1][j-1] = true;     }     else if (i-1 >= 0 && j+1 < c && visited[i-1][j+1] != true && matrix[i-1][j+1] == str[pos])     {         visited[i-1][j+1] = true;         result = result || check(str,pos+1,visited,i-1,j+1);         if (result == false)             visited[i-1][j+1] = true;     }     else if (i-1 >= 0 && j-1 >= 0 && visited[i-1][j-1]!= true && matrix[i-1][j-1] == str[pos])     {         visited[i-1][j-1] = true;         result = result || check(str,pos+1,visited,i-1,j-1);         if (result == false)             visited[i-1][j-1] = true;     }     return false; } 

the code quite self-explanatory: trying possible cases.

i getting wa in third test case, i.e.

2 9 a.l.z.e.. .l.i.w.l. 

i tried debugging couldn't narrow down problem.

the main problems are:

  1. using , j in loop clearing visited (as outer loops)
  2. using r , c global variables in check, writing them local variables in main
  3. always returning false check (instead of result)
  4. only trying first choice in check (turn "else if" "if")
  5. not clearing value in ans
  6. only breaking out of inner loop, not both , j loop
  7. terminating search when pos gets str.length()+1 instead of str.length()

it helps put print statements in recursive functions these, try them out on simple example, , see whether sequence of calls matches expectations.


Comments

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -