Find maximum length Snake sequence in C++

C++Server Side ProgrammingProgramming

Concept

With respect of a given grid of numbers, determine maximum length Snake sequence and display it. It has been observed that if multiple snake sequences exist with the maximum length, display any one of them.

Actually, a snake sequence is made up of adjacent numbers in the grid so that for each number, the number on the right or the number below it is either +1 or -1 its value. Here, for instance, if we are at location (a, b) in the grid, we can either move right i.e. (a, b+1) if that number is ± 1 or move down i.e. (a+1, b) if that number is ± 1.

For example,

10, 7, 6, 3
9, 8, 7, 6
8, 4, 2, 7
2, 2, 2, 8

In above grid, the maximum snake sequence is: (10, 9, 8, 7, 6, 7, 8)

Below figure shows all possible paths −

10  7  →6   3
↓   ↓   ↓
9 → 8 → 7→ 6
↓↓
8 4 2 7
↓
2 2 2 8

Method

Here, the concept is to implement Dynamic Programming. With respect of each cell of the matrix, we keep longest length of a snake which ends in current cell. Now the longest length snake sequence will have maximum value. Here, the longest value cell will correspond to tail of the snake. For printing the snake, we require to backtrack from tail all the way back to snake’s head. Assume T[a][b] represent maximum length of a snake which ends at cell (a, b), then for given matrix M, the Dynamic Programming relation is defined as −

T[0][0] = 0
T[a][b] = max(T[a][b], T[a][b – 1] + 1) if M[a][b] = M[a][b – 1] ± 1
T[a][b] = max(T[a][b], T[a – 1][b] + 1) if M[a][b] = M[a – 1][b] ± 1

Example

Live Demo

// C++ program to find maximum length
// Snake sequence and print it
#include <bits/stdc++.h>
using namespace std;
#define M 4
#define N 4
struct Point{
int X, Y;
};
// Shows function to find maximum length Snake sequence path
// (a, b) corresponds to tail of the snake
list<Point> findPath(int grid1[M][N], int mat1[M][N],
int a, int b){
list<Point> path1;
Point pt1 = {a, b};
path1.push_front(pt1);
while (grid1[a][b] != 0){
if (a > 0 &&
grid1[a][b] - 1 == grid1[a - 1][b]){
pt1 = {a - 1, b};
path1.push_front(pt1);
a--;
}
else if (b > 0 &&
grid1[a][b] - 1 == grid1[a][b - 1]){
pt1 = {a, b - 1};
path1.push_front(pt1);
b--;
}
}
return path1;
}
// Shows function to find maximum length Snake sequence
void findSnakeSequence(int mat1[M][N]){
// Shows table to store results of subproblems
int lookup1[M][N];
// Used to initialize by 0
memset(lookup1, 0, sizeof lookup1);
// Used to store maximum length of Snake sequence
int max_len1 = 0;
// Used to store cordinates to snake's tail
int max_row1 = 0;
int max_col1 = 0;
// Used to fill the table in bottom-up fashion
for (int a = 0; a < M; a++){
for (int b = 0; b < N; b++){
// Perform except for (0, 0) cell
if (a || b){
// look above
if (a > 0 &&
abs(mat1[a - 1][b] - mat1[a][b]) == 1){
lookup1[a][b] = max(lookup1[a][b],
lookup1[a - 1][b] + 1);
if (max_len1 < lookup1[a][b]){
max_len1 = lookup1[a][b];
max_row1 = a, max_col1 = b;
}
}
// look left
if (b > 0 &&
abs(mat1[a][b - 1] - mat1[a][b]) == 1){
lookup1[a][b] = max(lookup1[a][b],
lookup1[a][b - 1] + 1);
if (max_len1 < lookup1[a][b]){
max_len1 = lookup1[a][b];
max_row1 = a, max_col1 = b;
}
}
}
}
}
cout << "Maximum length of Snake sequence is: "
<< max_len1 << endl;
// Determine maximum length Snake sequence path
list<Point> path1 = findPath(lookup1, mat1, max_row1,
max_col1);
cout << "Snake sequence is:";
for (auto it = path1.begin(); it != path1.end(); it++)
cout << endl << mat1[it->X][it->Y] << " ("<< it->X << ", " << it->Y << ")" ;}
// Driver code
int main(){
int mat1[M][N] ={{10, 7, 6, 3},{9, 8, 7, 6},{8, 4, 2, 7},{2, 2, 2, 8},};
findSnakeSequence(mat1);
return 0;
}

Output

Maximum length of Snake sequence is: 6
Snake sequence is:
10 (0, 0)
9 (1, 0)
8 (1, 1)
7 (1, 2)
6 (1, 3)
7 (2, 3)
8 (3, 3)
Published on 25-Jul-2020 08:40:16