Advertisement
Guest User

Untitled

a guest
Jan 18th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.90 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3. import java.util.stream.IntStream;
  4.  
  5. //https://www.geeksforgeeks.org/intstream-range-java/
  6.  
  7. /* In this problem, we want to check if our self driving car can navigate through a particular path through the city.
  8.  
  9. A city grid will be provided as a list of strings, where each character in the string represents a cell. Characters can be ‘X’, which represents an obstacle, ‘.’ which represents an allowed location, and ‘P’ for the starting position of the car. There are no empty starting cells. Assume the grid is valid.
  10.  
  11. Here is an example input:
  12. [
  13. "XXXXXXXX",
  14. "X......X",
  15. "X.XXXX.X",
  16. "XP.....X",
  17. "XXXXXXXX",
  18. ]
  19.  
  20. A list of commands will be provided, where each command is encoded in a string. The first character of the string will be one of U,D,L, or R, representing up, down, left or right. A space will follow, followed by a number representing the number of cells to move in that direction.
  21.  
  22. For example:
  23. [
  24. "R 5",
  25. "U 2",
  26. "L 5",
  27. "D 1",
  28. ]
  29.  
  30. Please implement a solution that receives a city grid and list of commands and returns True if at the end all dots in the city grid are consumed or False otherwise.
  31.  
  32. For example, with the grid and commands mentioned above, your solution should return True since the car would traverse all the dots.
  33.  
  34. If the car would hit an obstacle or go outside the grid, your solution should eagerly return False.
  35.  
  36. The resulting code should be algorithmically correct and work with all possible edge cases. Code must compile. Looking up documentation during the interview is allowed.
  37.  
  38. */
  39.  
  40.  
  41. /*
  42. * To execute Java, please define "static void main" on a class
  43. * named Solution.
  44. *
  45. * If you need more classes, simply define them inline.
  46. */
  47.  
  48. class Solution {
  49.  
  50. public static void main(String[] args) {
  51. String[] testGrid = new String [] {
  52. "XXXXXXXX",
  53. "X......X",
  54. "X.XXXX.X",
  55. "XP.....X",
  56. "XXXXXXXX"
  57. };
  58. String[] commands = new String [] {
  59. "R 5",
  60. "U 2",
  61. "L 5",
  62. "D 1",
  63. };
  64.  
  65. System.out.println(findPath(testGrid, commands));
  66. }
  67.  
  68. public static boolean findPath(String[] input, String[] commands) {
  69.  
  70. int dimX, dimY;
  71. dimX = input[0].length();
  72. dimY = input.length;
  73.  
  74. char[][] grid = new char[input.length][input[0].length()];
  75.  
  76. int startX = 0, startY = 0;
  77.  
  78. for (int i = 0; i < grid.length; i++) {
  79. for (int j = 0; j < grid[i].length; j++) {
  80. if (grid[i][j] == 'P') {
  81. startY = i;
  82. startX = j;
  83. }
  84. }
  85. }
  86.  
  87. for (String command : commands) {
  88. char dir = command.split(" ")[0].toCharArray()[0];
  89. int dist = Character.getNumericValue(command.split(" ")[1].toCharArray()[0]);
  90.  
  91. // starting from startX, startY, traverse dist chars in direction, checking that each spot is traversible (. or '-')
  92. int[] operation = new int[2];
  93.  
  94. switch (dir) {
  95. case 'R': {
  96. operation = new int[] { startX + dist, startY };
  97. break;
  98. }
  99. case 'L': {
  100. operation = new int[] { startX - dist, startY };
  101. break;
  102. }
  103. case 'U': {
  104. operation = new int[] { startX, startY + dist };
  105. break;
  106. }
  107. case 'D': {
  108. operation = new int[] { startX, startY - dist };
  109. break;
  110. }
  111. }
  112.  
  113. System.out.println(String.format("Command: %s, operation: %d, %d", dir, operation[0], operation[1]));
  114.  
  115. // check if we're out of bounds
  116. if (operation[1] >= dimY || operation[1] < 0 || operation[0] >= dimX || operation[0] < 0) {
  117. System.out.println("Out of bounds");
  118. return false;
  119. }
  120.  
  121. // check if the path taken traverses any obstacles, and set '.' to '-'
  122. int[] range = IntStream.range(startX, operation[0]).toArray();
  123. for (int i : range) {
  124. if (grid[startY][i] != '.' || grid[startY][i] != '-' || grid[startY][i] != 'P') {
  125. System.out.println(String.format("grid %d %d is not . or -", startY, i));
  126. return false;
  127. } else {
  128. grid[startY][i] = '-';
  129. }
  130. }
  131.  
  132. range = IntStream.range(startY, operation[1]).toArray();
  133. for (int i : range) {
  134. if (grid[i][startX] != '.' || grid[i][startX] != '-' || grid[i][startX] != 'P') {
  135. return false;
  136. } else {
  137. grid[i][startX] = '-';
  138. }
  139. }
  140.  
  141. // check to make sure there are no dots left that were untraversed
  142.  
  143. for (int i = 0; i < dimY; i++) {
  144. for (int j = 0; j < dimX; j++) {
  145. if (i == '.') {
  146. return false;
  147. }
  148. }
  149. }
  150. }
  151. return true;
  152. }
  153. }
  154.  
  155.  
  156.  
  157. /*
  158. Your previous C++ content is preserved below:
  159.  
  160. Hello Michael.
  161. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement