本文共 1654 字,大约阅读时间需要 5 分钟。
import java.util.LinkedList;import java.util.Queue;import java.util.Scanner;public class Main { int n, m, t; Node [][]map; int [][]start; int [][]end; int[][][]flag; int [][]dir = { {1,0},{-1,0},{0,1},{0,-1}}; Queueque; public static void main(String[] args) { new Main().run(); } public void run(){ Scanner in = new Scanner(System.in); que = new LinkedList<>(); int n = in.nextInt(); int m = in.nextInt(); int t = in.nextInt(); map = new Node[n+1][m+1]; for(int i = 1; i <= n; i++){ for(int j = 1; j <= m; j++ ){ map[i][j] = new Node(i, j, 0); } } flag = new int[102][102][305]; start = new int[n+1][m+1]; end = new int[n+1][m+1]; for(int i = 0; i < t; i++ ){ int x = in.nextInt(); int y = in.nextInt(); int a = in.nextInt(); int b = in.nextInt(); start[x][y] = a; end[x][y] = b; } flag[1][1][0] = 1; que.offer(map[1][1]); while(!que.isEmpty()){ Node node = que.poll(); if(node.x == n && node.y == m){ System.out.println(node.step); break; } for(int i = 0; i < 4; i++ ){ int xx = node.x + dir[i][0]; int yy = node.y + dir[i][1]; int step = node.step + 1; if(step > 300) break;// if(xx == n && yy == m){// System.out.println(step);// tag = 0;// break;// }// if(tag == 0)// break; if(flag[xx][yy][step] == 0 && (xx>0&&yy>0&&xx<=n&&yy<=m)&&(step < start[xx][yy] || step > end[xx][yy])){ flag[xx][yy][step] = 1; que.offer(new Node(xx, yy, step)); } } } }}class Node{ int x, y; int step; public Node(int x, int y, int step){ this.x = x; this.y = y; this.step = step; } }
flag三维数组 表示此位置此时刻不能同时被入队
此外:注释的部分不对 也不知道为啥。。。理论上一样的
转载地址:http://nlimi.baihongyu.com/