RSS

There exits 2D array. We need to find out whether a given string (”MrBee”) exits in the given matrix.The stirng can be verticalor horizontal or in snake form but not in diagonal.

Fri, Feb 19, 2010

Algorithm

Mr…..
…B….
…ee…
 
For every element, check all its neighbour element to get the match

flag=0;
i=0;
j=1;
for (k=0;k<n;k++) {
      for(l=0;l<n;l++) {
        if(m[k][l] == str) {
    x=k;
    y=l;
    break;
        }
      }
}
 
 
while(str[j] != '\0') {
     if(m[x][y+1] == str[j]) {
        y = y+1;
     } else if(m[x][y-1] == str[j]) {
        y = y-1;
     } else if(m[x+1][y] == str[j]) {
        x = x+1;
     } else if(m[x-1][y] == str[j]) {
        x=x-1;
     } else if(m[x-1][y-1] == str[j])  {
        x=x-1;
        y=y-1;
     } else if(m[x-1][y+1] == str[j])  {
        x = x-1;
        y=y+1;
     } else if(m[x+1][y-1] == str[j])  {
        x=x+1;
        y=y-1;
     } else if(m[x+1][y+1] == str[j])  {
        x=x+1;
        y=y+1;
     } else {
        flag =1;
        break;
     }
     i++;
     j++;
     }
if(flag == 0) {
   printf(”String is Contiguous  !!\n”);
} else {
   printf(”String is not  Contiguous …\n”);
}
Sharing ~ Helping Other:
  • Print
  • email
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • BlinkList
  • DZone
  • Slashdot
  • YahooMyWeb
  • StumbleUpon
  • Live
  • IndianPad
  • DotNetKicks
  • Technorati

Related Posts:

,

This post was written by:

eXclusiveMinds - who has written 500 posts on eXclusiveMinds.


Contact the author

Leave a Reply

You must be logged in to post a comment.