find the longest snake

You are given a 2-D array with same number of rows and columns. You have to determine the longest snake in the array. The property to find the snake is the difference between the adjacent(left, right, up or down) should be either 1 or -1. If there are more than one snakes with maximum length, the output should print both of them. 

Example--> 

The given array elements are as follows: 

4 7 9 8 
5 6 5 4 
6 7 8 5 
10 9 7 6 

The longest snakes are 7->6->5->4->5->6->7->6->7->6->5->4

#include<iostream>
#include<math.h>

using namespace std;

int array[][4] = {{4,    7,    9,    8 },
              {5,    6,    5,    4 },
              {6,    7,    8,    5 },
              {10,    9,    7,    6 }};
int processed[][4] = {{0,    0,    0,    0 },
              {0,    0,    0,    0 },
              {0,    0,    0,    0 },
              {0,    0,    0,    0 }};
int in_stack[][4] = {{0,    0,    0,    0 },
              {0,    0,    0,    0 },
              {0,    0,    0,    0 },
              {0,    0,    0,    0 }};
int** solution;
int record[4*4];

void dfs(int i, int j, int* n, int* len, int count)
{
     if(processed[i][j] == 1)
                        return;
                        
     processed[i][j] = 1;
     in_stack[i][j] = 1;
     record[count++] = array[i][j];
     

     if(i+1 < 4)
     if(!in_stack[i+1][j] && (abs(array[i+1][j] - array[i][j]) == 1))
                          dfs(i+1, j, n, len, count);
     if(j+1 < 4)
     if(!in_stack[i][j+1] && (abs(array[i][j+1] - array[i][j]) == 1))
                          dfs(i, j+1, n, len, count);
     
     if(i-1 >= 0)
     if(!in_stack[i-1][j] && (abs(array[i-1][j] - array[i][j]) == 1))
                          dfs(i-1, j, n, len, count);
     
     if(j-1 >= 0)
     if(!in_stack[i][j-1] && (abs(array[i][j-1] - array[i][j]) == 1))
                          dfs(i, j-1, n, len, count);
     
     if(count > *len)
     {            
              if((*n != 0) && solution != NULL)
              {
                           if(*n > 1)
                           {
                               for(int i = 0; i < *n; i++)
                                       delete [] solution[i];
                           }
                           delete solution;
              }
              
              *len = count;
              *n = 1;             
              solution = new int*;
              *solution = new int[count];
              for(int i = 0; i < count; i++)
                      (*solution)[i] = record[i];     
     }
     if(count == *len)
     {
              int** temp = new int*[*n+1];
              for(int i = 0; i < *n; i++)
              {
                      temp[i] = new int[*len];
                      for(int j = 0; j < *len; j++)
                              temp[i][j] = solution[i][j];
              }
              temp[*n] = new int[*len];
              for(int j = 0; j < *len; j++)
                      temp[*n][j] = record[j];
                      
              if((*n != 0) && solution != NULL)
              {
                           if(*n > 1)
                           {
                               for(int i = 0; i < *n; i++)
                                       delete [] solution[i];
                           }
                           delete solution;
              }
              solution = temp;
              
     }            
     
     in_stack[i][j] = 0;
     count--;
}


void find_longest_snake(int* n, int* len)
{
     *n = 0;
     *len = 0;
     
     for(int i = 0; i < 4; i++)
        for(int j = 0; j < 4; j++)
        {
                if(processed[i][j] == 0)
                                   dfs(i, j , n, len, 0);
        }
        return;
}

int main()
{
    int n, len;
    find_longest_snake(&n, &len);
    cout << n << " " << len << endl;
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < len; j++)
                cout << solution[i][j] << " ";
        cout << endl;
    }
    
    return 0;
}

 

posted @ 2013-04-07 00:20  一只会思考的猪  阅读(232)  评论(0编辑  收藏  举报