'you are given matrix graph problem find the minimum level required to reach the end
in this problem, we are given a 2d matrix that contains values as 0,1 and 2 and 3
where 2 denotes the start position which is always the bottom left position of the matrix
3 denotes the end position.
2(start position) and 3(end position) are present only once in the matrix
find the minimum difficulty level by which we can reach the end position from start. where the difficulty level is defined as the jump required from one position to another only in a vertical direction from the matrix position containing 1 to another position containing 1. You can move in a horizontal direction if there is 1
Eg: Figure representing the s start pos and g as end position
Eg:
inputs
5 8 row -col of matrix
1 1 1 1 0 0 0 0
0 0 0 3 0 1 1 1
1 1 1 0 0 1 0 0
0 0 0 0 0 0 1 0
2 1 1 1 1 1 1 1
answer = 2
as from mat[4][0] it will jump to position mat[2][0] and then to mat[0][0] then move in right till m[0][3] and then in downward direction to reach mat[1][3].
we can move left, right, and up and down only. In the case of up and down, we can take any length jump from mat[i][j] to mat[i+k][j] where k is the length of jump but mat[i][j] = 1
Eg - 2 :
9 13
0 1 1 1 1 1 1 1 1 1 1 1 1
1 1 0 0 0 0 0 0 0 0 0 1 1
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
1 1 0 0 0 0 0 0 0 0 0 1 3
0 0 0 0 0 0 0 0 0 0 0 0 0
1 1 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
2 1 1 1 1 1 1 1 1 1 1 1 1
ans = 3
1<=row<=50 1<=col<=50
#include <iostream>
using namespace std;
int n, m, map[10][10], visited[10][10], minDifficulty = 11;
// two moves - horizontal and vertical
void climbRock(int i, int j, int maxClimbTillNow) {
if(i >= n || i < 0)
return;
else if(j >= m || j < 0)
return;
else if(map[i][j] == 3) {
// cout << "Destination Reached." << endl;
if(maxClimbTillNow < minDifficulty)
minDifficulty = maxClimbTillNow;
} else if(map[i][j] == 0)
return;
else if(visited[i][j] != 0)
return;
else {
visited[i][j] = 1;
// vertical move up
int up = i+1, t1 = 0, down = i-1;
while(up != n && map[up][j] == 0)
up++;
// cout << "Climbing Up " << i << " to " << up << " and maxClimbTillNow " << maxClimbTillNow << endl;
if(up != n && visited[up][j] == 0 && map[up][j] != 0) {
t1 = up - i;
if(maxClimbTillNow > t1)
t1 = maxClimbTillNow;
// cout << "Climbing Up (" << i << ", " << j << ") to (" << up << ", " << j << ") = " << map[up][j] <<" and maxClimbTillNow " << t1 << endl;
climbRock(up, j, t1);
}
//move down
while(down != -1 && map[down][j] == 0)
down--;
// cout << "Climbing Up " << i << " to " << up << " and maxClimbTillNow " << maxClimbTillNow << endl;
if(down != -1 && visited[down][j] == 0 && map[down][j] != 0) {
t1 = i - down;
if(maxClimbTillNow > t1)
t1 = maxClimbTillNow;
// cout << "Climbing Down (" << i << ", " << j << ") to " << down << " and maxClimbTillNow "<< t1 << endl;
climbRock(down, j, t1);
}
//horizontal move
if((j>=0&&j<m) && (map[i][j+1] == 1 || map[i][j+1] == 3) && (visited[i][j+1] == 0)) {
climbRock(i, j+1, maxClimbTillNow);
}
if((j>=0&&j<m) && (map[i][j-1] == 1 || map[i][j-1] == 3) && (visited[i][j-1] == 0)) {
climbRock(i, j-1, maxClimbTillNow);
}
visited[i][j] = 0;
}
}
int main() {
int i, j;
cin >> n >> m;
for(i=0;i<n;i++) {
for(j=0;j<m;j++)
map[i][j] = visited[i][j] = 0;
}
for(i=0;i<n;i++) {
for(j=0;j<m;j++)
cin >> map[i][j];
}
climbRock(n-1, 0, 0);
cout << minDifficulty << endl;
return 0;
}
can anybody optimize it as it is not working for row>15 and col>15
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
