Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.HashMap;
- import static java.lang.Integer.MAX_VALUE;
- public class Grid {
- private boolean[][] grid;
- private int x;
- private int y;
- public Grid()
- {
- grid = new boolean[20001][20001];
- x = 5000;
- y = 5000;
- }
- public void move(char direction, int magnitude)
- {
- for(int i = 0; i < magnitude; i++)
- {
- if(direction == "U".charAt(0))
- {
- y++;
- grid[x][y] = true;
- }else if(direction == "D".charAt(0))
- {
- y--;
- grid[x][y] = true;
- }else if(direction == "R".charAt(0))
- {
- x++;
- grid[x][y] = true;
- }else if(direction == "L".charAt(0))
- {
- x--;
- grid[x][y] = true;
- }
- }
- }
- public int findMin(Grid grid2)
- {
- int distance = MAX_VALUE;
- for(int i = 0; i < grid.length; i++)
- {
- for(int z = 0; z < grid[i].length; z++)
- {
- if(grid[i][z] && grid2.value(i, z))
- {
- if(findDistance(i, z) < distance)
- {
- distance = findDistance(i, z);
- }
- }
- }
- }
- return distance;
- }
- private boolean value(int i, int z)
- {
- return grid[i][z];
- }
- private int findDistance(int x, int y)
- {
- return Math.abs(x - 5000) + Math.abs(y - 5000);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement