File tree Expand file tree Collapse file tree 1 file changed +44
-0
lines changed
2684-maximum-number-of-moves-in-a-grid Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Pair {
2
+ int x ;
3
+ int y ;
4
+ int steps ;
5
+ Pair (int x ,int y ,int steps ){
6
+ this .x =x ;
7
+ this .y =y ;
8
+ this .steps =steps ;
9
+ }
10
+ }
11
+ class Solution {
12
+ public int maxMoves (int [][] grid ) {
13
+ int max =0 ;
14
+ for (int i =0 ;i <grid .length ;i ++){
15
+ int cnt =bfs (grid ,i );
16
+ max =Math .max (max ,cnt );
17
+ }
18
+ return max ;
19
+ }
20
+ public int bfs (int [][] grid ,int row ){
21
+ Queue <Pair > qu =new LinkedList <>();
22
+ qu .add (new Pair (row ,0 ,0 ));
23
+ boolean [][] vis =new boolean [grid .length ][grid [0 ].length ];
24
+ vis [row ][0 ]=true ;
25
+ int steps =0 ;
26
+ while (!qu .isEmpty ()){
27
+ Pair p =qu .poll ();
28
+ int x =p .x ;
29
+ int y =p .y ;
30
+ steps =p .steps ;
31
+ int diri []={-1 ,0 ,1 };
32
+ int dirj []={1 ,1 ,1 };
33
+ for (int i =0 ;i <3 ;i ++){
34
+ int curri =x +diri [i ];
35
+ int currj =y +dirj [i ];
36
+ if (curri >=0 && curri <grid .length && currj >=0 && currj <grid [0 ].length && !vis [curri ][currj ] && grid [curri ][currj ]>grid [x ][y ]){
37
+ qu .add (new Pair (curri ,currj ,steps +1 ));
38
+ vis [curri ][currj ]=true ;
39
+ }
40
+ }
41
+ }
42
+ return steps ;
43
+ }
44
+ }
You can’t perform that action at this time.
0 commit comments