Untitled
unknown
plain_text
a year ago
1.7 kB
3
Indexable
static final int INFI = -1; static final int ENEMY = 0; static final int MAX_N = 202; static Queue<NodeCs> qt; static int[][] map = new int[MAX_N][MAX_N]; static int n, m, p, q, s, t; static int[] dx = { -1, -1, 1, 1 }; static int[] dy = { -1, 1, 1, -1 }; public static void main(String[] args) throws Exception { int T = sc.nextInt(); qt = new Queue<NodeCs>(); for (int tc = 1; tc <= T; tc++) { n = sc.nextInt(); m = sc.nextInt(); // start p = sc.nextInt(); p = n - p; q = sc.nextInt() - 1; // end s = sc.nextInt(); s = n - s; t = sc.nextInt() - 1; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { map[i][j] = INFI; } } for (int en = 0; en < m; en++) { int x = sc.nextInt(); int y = sc.nextInt() - 1; map[n - x][y] = ENEMY; } map[p][q] = 1; System.out.println("Case #" + tc); if (map[s][t] == INFI) { System.out.println(-1); } else { System.out.println(map[s][t] - 1); } } } private static void bfs() { qt.reset(); qt.enQueue(new NodeCs(p, q)); NodeCs tmp; while (!qt.isEmpty()) { tmp = qt.peek(); qt.deQueue(); for (int i = 0; i < dx.length; i++) { int xx = tmp.x + dx[i]; int yy = tmp.y + dy[i]; while (isSafe(xx, yy)) { if (map[xx][yy] == INFI) { map[xx][yy] = map[tmp.x][tmp.y] + 1; qt.enQueue(new NodeCs(xx, yy)); } xx += dx[i]; yy += dy[i]; } } } } private static boolean isSafe(int x, int y) { if (x >= 0 && x < map.length && y >= 0 && y < map.length) { if (map[x][y] != ENEMY) { return true; } } return false; } }
Editor is loading...
Leave a Comment