MCPcopy Create free account
hub / github.com/Rohit91singh9/Coding-DP-DSA / demolitionRobot

Method demolitionRobot

DemolitionRobot.java:12–53  ·  view source on GitHub ↗
(int[][]grid)

Source from the content-addressed store, hash-verified

10 */
11
12public static int demolitionRobot(int[][]grid) {
13
14//Write your code
15
16int n = grid.length;
17int m = grid[0].length;
18
19Queue<int[]> q = new LinkedList<>();
20boolean[][] visited = new boolean [n][m];
21
22int minD = Integer.MAX_VALUE;
23
24int[][] directions = {{0,1}, {1,0}, {0,-1}, {-1, 0}};
25
26q.add(new int []{0,0});
27visited[0][0] =true;
28while(!q.isEmpty())
29{
30 int[] cur = q.remove();
31 for(int[] d : directions)
32 {
33 int nX = cur[0]+d[0];
34 int nY = cur[1]+d[1];
35
36 if(nX<0|| nY<0 || nX>=n || nY>=m || grid[nX][nY]==0)
37 continue;
38
39 if(grid[nX][nY]==9)
40 minD = Math.min(minD, grid[cur[0]][cur[1]]);
41
42 if(grid[nX][nY]==1 && !visited[nX][nY])
43 {
44 grid[nX][nY]= grid[cur[0]][cur[1]]+1;
45 visited[nX][nY]= true;
46 q.add(new int[]{nX, nY});
47 }
48 }
49}
50
51return minD;
52
53}
54
55
56

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected