-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1631.pathwithmineffort
65 lines (59 loc) · 1.54 KB
/
1631.pathwithmineffort
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
class Solution {
static class Pair{
int dist;
int i;
int j;
Pair(int k,int l,int s)
{
this.dist=k;
this.i=l;
this.j=s;
}
}
public int minimumEffortPath(int[][] height) {
int n=height.length;
int m=height[0].length;
Queue<Pair>qu=new LinkedList<>();
qu.add(new Pair(0,0,0));
int[][] res=new int[n][m];
for(int p=0;p<n;p++)
{
for(int j=0;j<m;j++)
{
res[p][j]=Integer.MAX_VALUE;
}
}
res[0][0]=0;
while(!qu.isEmpty())
{
Pair p=qu.poll();
int d=p.dist;
int r=p.i;
int c=p.j;
int row[]={-1,1,0,0};
int col[]={0,0,-1,1};
for(int l=0;l<4;l++)
{
int nr=r+row[l];
int nc=c+col[l];
if(nr>=0 && nr<n && nc>=0 && nc<m){
int minus=Math.abs(height[nr][nc]-height[r][c]);
int pi=Math.max(minus,d);
if(pi<res[nr][nc])
{
res[nr][nc]=pi;
qu.add(new Pair(pi,nr,nc));
}
}
}
}
for(int p=0;p<n;p++)
{
for(int k=0;k<m;k++){
System.out.print(res[p][k]+" ");
}
System.out.println();
}
return res[n-1][m-1];
}
}