Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. .SP..
  2. XS.S.
  3. .SSX.
  4. ...S.
  5. ....X
  6.  
  7. Flowers are X, P is you. The grid is NxN. Number of flowers is K.
  8. S are stones, fragrances need to travel around stones.
  9. Find the most fragrant flower at your location (P).
  10.  
  11. Fragrance travels on a Manhattan distance, decays linearly:
  12.  
  13. F = F_0 - R d
  14. F_0 is constant for all flowers,
  15. R is a constant decay rate,
  16. d is Manhattan distance.
  17.  
  18. char grid[][];
  19. Pair<int, int> person;
  20. List<Pair<int, int>> flowers;
  21. List<Pair<int, int>> stones;
  22.  
  23. private int[] dx = {0,-1,0,1};
  24. private int[] dy = {1,0,-1,0};
  25. public int[] findMostFragnant(char[][] grid, int[] person, List<int[]> flowers, List<int[]> stones){
  26. if(flower == null || flowers.size() == 0){
  27. return null;
  28. }
  29. Queue<int[][]> q = new LinkedList<>();
  30. q.add(new int[]{person[0], person[1]});
  31. boolean[][] visited = new boolean[grid.length][grid[0].length];
  32. visited[person[0]][person[1]] = true;
  33. while(!q.isEmpty()){
  34. int size = q.size();
  35. for(int i = 0; i < size; i++){
  36. int[] pos = q.poll();
  37. int x = pos[0];
  38. int y = pos[1];
  39. for(int j = 0; j < 4; j++){
  40. int nx = x + dx[j];
  41. int ny = y + dy[j];
  42. if(nx < 0 || ny < 0 || nx >= grid.size || ny >= grid[0].size || grid[nx][ny] = ā€˜Sā€™ || visited[nx][ny]){
  43. continue;
  44. }
  45. if(grid[nx][ny] == ā€˜Xā€™){
  46. return new int[]{nx, ny};
  47. }
  48. visited[nx][ny] = true;
  49. q.add(new int[]{nx, ny});
  50.  
  51.  
  52. }
  53. }
  54. }
  55. return null;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement