(int[][]grid)
| 10 | */ |
| 11 | |
| 12 | public static int demolitionRobot(int[][]grid) { |
| 13 | |
| 14 | //Write your code |
| 15 | |
| 16 | int n = grid.length; |
| 17 | int m = grid[0].length; |
| 18 | |
| 19 | Queue<int[]> q = new LinkedList<>(); |
| 20 | boolean[][] visited = new boolean [n][m]; |
| 21 | |
| 22 | int minD = Integer.MAX_VALUE; |
| 23 | |
| 24 | int[][] directions = {{0,1}, {1,0}, {0,-1}, {-1, 0}}; |
| 25 | |
| 26 | q.add(new int []{0,0}); |
| 27 | visited[0][0] =true; |
| 28 | while(!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 | |
| 51 | return minD; |
| 52 | |
| 53 | } |
| 54 | |
| 55 | |
| 56 |
nothing calls this directly
no outgoing calls
no test coverage detected