Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- void create2Matrix(int** mtx,int** mtxWay,int rows)
- {
- for (int i = 0; i < rows; i++)
- {
- int colls;
- cin >> colls;
- mtx[i] = new int[colls + 1] {0};
- mtx[i][0] = colls;
- mtxWay[i] = new int[colls + 1] {0};
- mtxWay[i][0] = colls;
- for (int j = 1; j < colls + 1; j++)
- {
- cin >> mtx[i][j];
- mtxWay[i][j]=0;
- }
- }
- }
- void deleteMatrix(int** matrix, int rows)
- {
- for (int i = 0; i < rows; i++)
- {
- delete[] matrix[i];
- }
- delete[] matrix;
- }
- void printMatrix(int** matrix, int rows)
- {
- for (int i = 0; i < rows; i++)
- {
- int sizeOfRow = matrix[i][0];
- for (int j = 1; j < sizeOfRow + 1; j++)
- {
- cout << matrix[i][j] << " ";
- }
- cout << endl;
- }
- }
- int findWay(int** matrix,int **mtxWay, int rows, int x, int y)
- {
- int way=1,maxLen=1;
- for (int i = x - 1; i <= x + 1; i++)
- {
- if (i < 0 || i >= rows)
- {
- continue;
- }
- for (int j = y - 1; j <= y + 1; j++)
- {
- if (j <1 || j > matrix[i][0])
- {
- continue;
- }
- if (matrix[i][j] > matrix[x][y] )
- {
- if(mtxWay[i][j]>0) way=1+mtxWay[i][j];
- else way=1+findWay(matrix,mtxWay,rows,i,j);
- if(way>maxLen)maxLen=way;
- }
- }
- }
- mtxWay[x][y]=maxLen;
- return maxLen;
- }
- unsigned int findLongestLine2(int** matrix,int** mtxWay, int rows)
- {
- int maxi=0;
- for (int i = 0; i < rows; i++)
- {
- int sizeOfRow = matrix[i][0];
- for (int j = 1; j < sizeOfRow + 1; j++)
- {
- int br=findWay(matrix,mtxWay,rows,i,j);
- if(br>maxi)maxi=br;
- }
- }
- return maxi;
- }
- int main()
- {
- int rows;
- cin >> rows;
- int length = 1, maxLength = 0;
- int **mtx=new int*[rows];
- int**mtxWay=new int*[rows];//тази матрица е за да запазим път от даден елемент и да е то изчисляваме втори път
- create2Matrix(mtx,mtxWay,rows);
- // printMatrix(mtx, rows);
- // printMatrix(mtxWay, rows);
- cout << findLongestLine2(mtx,mtxWay, rows)<<endl;
- //printMatrix(mtxWay, rows);
- deleteMatrix(mtx, rows);
- deleteMatrix(mtxWay, rows);
- /*
- 4
- 3 4 8 9
- 2 2 10
- 5 3 11 7 0 4
- 3 1 3 12
- */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement