Rotate image (square matrix) by 90 deg
Tue, 06 May 2025
1 2 3 4 8 2 1 5 3and you want to move from (0,0) to (2,2). Then you have to travel the following elements:
1 2 3 4 8 2 1 5 3
Path: 1->2->2->3
It is easy to visualize it recursively. If we start from the destination point and move backward. Then
Minimum Path to reach a point = Current Cell value + Minimum of paths from (top, left & top-left diagonal)
/** Print the min sum path in a matrix. if the matrix is
* { {1, 2, 3},
* {4, 8, 2},
* {1, 5, 3} }
*then path from (0,0) to (2,2) will be via 1->2->2->3
*
* (m,n) is the last cell where we want to reach.
*/
int printMinPath(int arr[R][C], int m, int n)
{
if(m<0 || n<0)
return POSOTIVE_INFINITY;
if(n==0 && m==0)
return arr[m][n]; // since array has only positive numbers
else
{
int a = printMinPath(arr, m-1, n);
int b = printMinPath(arr, m, n-1);
int c = printMinPath(arr, m-1, n-1);
return getMinimum(a, b, c) + arr[m][n];
}
}
Function getMinimum(int, int, int); is a simple function which accepts 3 int values and return the minimum of them.
Tue, 06 May 2025
Tue, 06 May 2025
Tue, 06 May 2025
Leave a comment