Guest User

Untitled

a guest
Apr 25th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | None | 0 0
  1. #include <algorithm>
  2. #include <cmath>
  3. #include <vector>
  4. #include <iostream>
  5. #include <random>
  6.  
  7. using namespace std;
  8.  
  9.  
  10. #define MATRIX_SIZE 1000
  11.  
  12. void printPath() {
  13.  
  14. }
  15.  
  16. int check(int i, int j, vector<vector<int> > matrix, vector<vector<int> > length) {
  17. if (length[i][j] == -1) {
  18. int currentValue = matrix[i][j];
  19.  
  20. int len = 0;
  21. if ((i > 0) && (matrix[i-1][j] > currentValue)) { // check up
  22. len = max(len, check(i-1, j, matrix, length));
  23. }
  24. if ((i + 1 < matrix.size()) && (matrix[i+1][j] > currentValue)) { // check left
  25. len = max(len, check(i+1, j, matrix, length));
  26. }
  27. if ((j > 0) && (matrix[i][j-1] > currentValue)) { // check down
  28. len = max(len, check(i, j-1, matrix, length));
  29. }
  30. if ((j+1 < matrix[i].size()) && (matrix[i][j+1] > currentValue)) { // check right
  31. len = max(len, check(i, j+1, matrix, length));
  32. }
  33. length[i][j] = len + 1; // Include current cell
  34. }
  35. return length[i][j];
  36. }
  37.  
  38. int longestPath(vector<vector<int> > matrix) {
  39. int n = matrix[0].size();
  40. int m = matrix.size();
  41. vector<vector<int> > length(m, vector<int>(n,-1));
  42.  
  43. int len = 0;
  44. for (int i = 0; i < m; i++) {
  45. for (int j = 0; j < n; j++) {
  46. len = max(len, check(i, j, matrix, length));
  47. }
  48. }
  49. return len;
  50. }
  51.  
  52.  
  53. int main() {
  54. // Specify the number of rows and columns of the matrix
  55. int n = MATRIX_SIZE;
  56. int m = MATRIX_SIZE;
  57.  
  58. // Declare random number generator
  59. mt19937 gen(10);
  60. uniform_int_distribution<> dis(1, 1000000);
  61.  
  62. // Fill matrix
  63. vector<vector<int> > matrix;
  64. for(int i = 0; i < m; i++) {
  65. vector<int> row;
  66. for(int j = 0; j < n; j++) {
  67. row.push_back(0);
  68. }
  69. matrix.push_back(row);
  70. }
  71.  
  72. // Apply random number generator to create matrix
  73. for(int i = 0; i < m; i++) {
  74. for(int j = 0; j < n; j++) {
  75. matrix[i][j] = dis(gen);
  76. }
  77. }
  78.  
  79. // Print matrix to see contents
  80. // for(int i = 0; i < m; i++) {
  81. // for(int j = 0; j < n; j++) {
  82. // cout << matrix[i][j] << " ";
  83. // }
  84. // cout << endl;
  85. // }
  86. // cout << endl;
  87.  
  88. int result = longestPath(matrix);
  89. cout << result << endl;
  90.  
  91.  
  92.  
  93.  
  94.  
  95. }
Add Comment
Please, Sign In to add comment