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;
}

 

转载于:https://www.cnblogs.com/kwill/archive/2013/04/07/3003258.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值