Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- .SP..
- XS.S.
- .SSX.
- ...S.
- ....X
- Flowers are X, P is you. The grid is NxN. Number of flowers is K.
- S are stones, fragrances need to travel around stones.
- Find the most fragrant flower at your location (P).
- Fragrance travels on a Manhattan distance, decays linearly:
- F = F_0 - R d
- F_0 is constant for all flowers,
- R is a constant decay rate,
- d is Manhattan distance.
- char grid[][];
- Pair<int, int> person;
- List<Pair<int, int>> flowers;
- List<Pair<int, int>> stones;
- private int[] dx = {0,-1,0,1};
- private int[] dy = {1,0,-1,0};
- public int[] findMostFragnant(char[][] grid, int[] person, List<int[]> flowers, List<int[]> stones){
- if(flower == null || flowers.size() == 0){
- return null;
- }
- Queue<int[][]> q = new LinkedList<>();
- q.add(new int[]{person[0], person[1]});
- boolean[][] visited = new boolean[grid.length][grid[0].length];
- visited[person[0]][person[1]] = true;
- while(!q.isEmpty()){
- int size = q.size();
- for(int i = 0; i < size; i++){
- int[] pos = q.poll();
- int x = pos[0];
- int y = pos[1];
- for(int j = 0; j < 4; j++){
- int nx = x + dx[j];
- int ny = y + dy[j];
- if(nx < 0 || ny < 0 || nx >= grid.size || ny >= grid[0].size || grid[nx][ny] = āSā || visited[nx][ny]){
- continue;
- }
- if(grid[nx][ny] == āXā){
- return new int[]{nx, ny};
- }
- visited[nx][ny] = true;
- q.add(new int[]{nx, ny});
- }
- }
- }
- return null;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement