Test Shorten

import java.util.*;

class UserSolution
{

	class Node implements Comparator<Node> {

		int id;
		int x;
		int y;
		int dist;

		public Node() {}

		public Node(int id, int dist) {
			this.id = id;
			this.dist = dist;
		}

		public Node(int id, int x, int y) {
			this.id = id; // -1 白色, -2 灰色(障碍物)
			this.x = x;
			this.y = y;
		}

		public Node(int id, int x, int y, int dist) {
			this.id = id;
			this.x = x;
			this.y = y;
			this.dist = dist;
		}

		@Override
		public int compare(Node o1, Node o2) {
			return o1.dist - o2.dist;
		}
	}


	int n;
	int[][] graph;
	HashMap<Integer, Node> nodeMap = new HashMap<>();
	HashMap<Integer, ArrayList<Node>> adjTable = new HashMap<>();
	int range;

	int[] dx = new int[]{1, -1, 0, 0};
	int[] dy = new int[]{0, 0, 1, -1};

	void init(int N, int mRange, int mMap[][])
	{
		this.n = N;
		this.graph = new int[n][n];
		nodeMap.clear();
		adjTable.clear();
		this.range = mRange;
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				if (mMap[i][j] == 0) {
					graph[i][j] = -1;
					continue;
				}
				if (mMap[i][j] == 1) {
					graph[i][j] = -2;
				}
			}
		}
	}

	void add(int mID, int mRow, int mCol)
	{
		Node insertNode = new Node(mID, mRow, mCol);
		nodeMap.put(mID, insertNode);
		graph[mRow][mCol] = mID;

		adjTable.put(mID, new ArrayList<>());

		update(insertNode);

	}

	private void update(Node updateNode) {

		ArrayDeque<Node> queue = new ArrayDeque<>();
		boolean[][] vis = new boolean[n][n];

		queue.add(new Node(updateNode.id, updateNode.x, updateNode.y, 0));
		vis[updateNode.x][updateNode.y] = true;

		while (!queue.isEmpty()) {

			Node startNode = queue.poll();

			for (int i = 0; i < 4; i++) {

				int newX = startNode.x + dx[i];
				int newY = startNode.y + dy[i];
				int dist = startNode.dist + 1;
				if (!isOut(newX, newY)) {
					continue;
				}
				int id = graph[newX][newY];


				if (isOut(newX, newY) && !vis[newX][newY] && id > -2 && dist <= range) {
					vis[newX][newY] = true;

					queue.add(new Node(id, newX, newY, dist));

					if (id >= 0) {
						adjTable.get(updateNode.id).add(new Node(id, newX, newY, dist));
						adjTable.get(id).add(new Node(updateNode.id, updateNode.x, updateNode.y, dist));
					}
				}

			}

		}


	}

	private boolean isOut(int x, int y) {
		return x >= 0 && y >= 0 && x < n && y < n;
	}


	int distance(int mFrom, int mTo)
	{

		Node sourceNode = nodeMap.get(mFrom);
		Node targetNode = nodeMap.get(mTo);
		if (sourceNode == null || targetNode == null) {
			return -1;
		}

		PriorityQueue<Node> queue = new PriorityQueue<>(new Node());
		boolean[] vis = new boolean[205];
		vis[mFrom] = true;

		queue.add(new Node(sourceNode.id,0));

		while (!queue.isEmpty()) {

			Node startNode = queue.poll();
			vis[startNode.id] = true;
			if (startNode.id == mTo) {
				return startNode.dist;
			}


			for (Node node : adjTable.get(startNode.id)) {
				if (!vis[node.id]) {
					queue.add(new Node(node.id, node.dist + startNode.dist));
				}
			}
		}

			return -1;
	}
}

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值