Advertisement
Guest User

Sequence in Matrix

a guest
Feb 20th, 2019
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let sizeOfMatrix = gets().split(' ').map(Number);
  2. let row = sizeOfMatrix[0];
  3. let col = sizeOfMatrix[1];
  4. let matrix = [];
  5.  
  6.  
  7. for(let i = 0;i < row;i ++){
  8.     let current = gets().split(' ');
  9.     matrix[i] = [];
  10.     for(let j = 0; j < col;j ++){
  11.         matrix[i].push(current[j]);
  12.     }
  13. }
  14.  
  15.  
  16. if(row == 0 || col == 0){
  17.     lineResult = 0;
  18.     columnResult = 0;
  19.     firstDiagonal = 0;
  20.     secondDiagonal = 0;
  21.    
  22. }
  23. console.table(matrix);
  24. //check line.
  25. let lineResult = 1;
  26. for(let i = 0;i <= row - 1;i ++){
  27.     let line = 1;
  28.     for(let j = 0;j < col - 1;j ++){
  29.         if(matrix[i][j] === matrix[i][j + 1]){
  30.             line ++;
  31.             if(line > lineResult){
  32.                 lineResult = line ;
  33.             }
  34.         }
  35.         else{
  36.             line = 1;
  37.         }
  38.     }
  39. }
  40. //check column.
  41. let columnResult = 1;
  42. for(let i = 0;i <= row - 1;i ++){
  43.     let column = 1;
  44.     for(let j = 0;j < col - 1; j ++){
  45.         if(matrix[j][i] === matrix[j + 1][i]){
  46.             column ++;
  47.             if(column > columnResult){
  48.                 columnResult = column;
  49.             }
  50.         }
  51.         else{
  52.             column = 1;
  53.         }
  54.     }
  55. }
  56. //check first diagonal.
  57. let firstDiagonal = 1;
  58. for(let i = 0;i < row - 1;i++){
  59.     let diagonal1 = 1;
  60.     for(let j = 0;j < col - 1;j ++){
  61.         if(matrix[i][j] === matrix[i + 1][j + 1]){
  62.             diagonal1 ++;
  63.             if(diagonal1 > firstDiagonal){
  64.                 firstDiagonal  = diagonal1;
  65.             }
  66.         }
  67.         else{
  68.             diagonal1 = 1;
  69.         }
  70.         i ++;
  71.     }
  72. }
  73.  
  74. //check second diagonal.
  75. let secondDiagonal = 1;
  76. for(let i = 0;i < row - 1;i++){
  77.     let diagonal2 = 1;
  78.     for(let j = col - 1;j >= 1;j--){
  79.         if(matrix[i][j] === matrix[i + 1][j - 1]){
  80.             diagonal2 ++;
  81.             if(diagonal2 > secondDiagonal){
  82.                 secondDiagonal = diagonal2;
  83.             }
  84.         }
  85.         else{
  86.             diagonal2 = 1;
  87.         }
  88.         i++;
  89.     }
  90. }
  91.  
  92.  
  93. print(Math.max(lineResult,columnResult,firstDiagonal,secondDiagonal));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement