Untitled
unknown
plain_text
2 years ago
5.4 kB
2
Indexable
import java.util.Scanner; class MyQueue { private int maxSize; private int[] array; private int input; private int output; public MyQueue(int maxSize) { this.maxSize = maxSize; array = new int[maxSize]; this.input = -1; this.output = -1; } public void push(int x) { input++; array[input] = x; } public int pop() { output++; return array[output]; } public boolean isEmpty() { if (input == output) { return true; } return false; } public void reset() { input = output = -1; } } public class test { static int N, M; static int x, y; static int num_fire; static int num_lake; static int num_exit; static int[][] map = new int[100][100]; static int[][] exit = new int[100][100]; static int[][] vs = new int[100][100]; static int[][] Score = new int[100][100]; static int[][] time_fire = new int[100][100]; static MyQueue queueFireX = new MyQueue(100000); static MyQueue queueFireY = new MyQueue(100000); static MyQueue queueTime = new MyQueue(100000); static MyQueue queueHugoX = new MyQueue(100000); static MyQueue queueHugoY = new MyQueue(100000); static MyQueue queueHugoTime = new MyQueue(100000); static MyQueue queueHugoScore = new MyQueue(100000); static int[] Bx = { 0, -1, 0, 1 }; static int[] By = { -1, 0, 1, 0 }; static int ans = 0; static boolean check = false; static void reset() { for (int j = 0; j < N; j++) { for (int k = 0; k < M; k++) { map[j][k] = 0; vs[j][k] = 0; exit[j][k] = 0; Score[j][k] = 0; time_fire[j][k] = 0; } } ans = 0; check = false; queueFireX.reset(); queueFireY.reset(); queueHugoScore.reset(); queueHugoTime.reset(); queueHugoX.reset(); queueHugoY.reset(); queueTime.reset(); } static void BFS_fire() { while (!queueFireX.isEmpty()) { int a = queueFireX.pop(); int b = queueFireY.pop(); int t = queueTime.pop(); for (int i = 0; i < 4; i++) { int r = a + Bx[i]; int c = b + By[i]; if (r < 0 || r >= N || c < 0 || c >= M) continue; if (map[r][c] == 0 && time_fire[r][c] == 0) { time_fire[r][c] = t + 1; queueFireX.push(r); queueFireY.push(c); queueTime.push(t + 1); } } } } static void BFS_Hugo() { queueHugoX.push(x - 1); queueHugoY.push(y - 1); queueHugoTime.push(0); queueHugoScore.push(Score[x - 1][y - 1]); vs[x - 1][y - 1] = 1; while (!queueHugoX.isEmpty()) { int a = queueHugoX.pop(); int b = queueHugoY.pop(); int t = queueHugoTime.pop(); int score = queueHugoScore.pop(); if (map[a][b] == 0) { t = t + 1; } if (map[a][b] == 3) { t = t + 2; } for (int i = 0; i < 4; i++) { int r = a + Bx[i]; int c = b + By[i]; if (r < 0 || r >= N || c < 0 || c >= M || vs[r][c] == 1) continue; if ((exit[r][c] == 1 && t < time_fire[r][c] && score + Score[r][c] > vs[r][c]) || (exit[r][c] == 1 && time_fire[r][c] == 0 && score + Score[r][c] > vs[r][c])) { vs[r][c] = score + Score[r][c]; if (score + Score[r][c] > ans) { check = true; ans = score + Score[r][c]; } } if ((map[r][c] == 0 && vs[r][c] == 0 && t < time_fire[r][c]) || (map[r][c] == 0 && vs[r][c] == 0 && time_fire[r][c] == 0)) { vs[r][c] = 1; queueHugoX.push(r); queueHugoY.push(c); queueHugoTime.push(t); queueHugoScore.push(score + Score[r][c]); } else if (map[r][c] == 3 && vs[r][c] == 0) { vs[r][c] = 1; queueHugoX.push(r); queueHugoY.push(c); queueHugoTime.push(t); queueHugoScore.push(score + Score[r][c]); } } } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int tc = sc.nextInt(); for (int i = 0; i < tc; i++) { reset(); N = sc.nextInt(); M = sc.nextInt(); x = sc.nextInt(); y = sc.nextInt(); num_fire = sc.nextInt(); for (int j = 0; j < num_fire; j++) { int r = sc.nextInt(); int c = sc.nextInt(); map[r - 1][c - 1] = 2; queueFireX.push(r - 1); queueFireY.push(c - 1); queueTime.push(0); } num_lake = sc.nextInt(); for (int j = 0; j < num_lake; j++) { int r = sc.nextInt(); int c = sc.nextInt(); map[r - 1][c - 1] = 3; } num_exit = sc.nextInt(); for (int j = 0; j < num_exit; j++) { int r = sc.nextInt(); int c = sc.nextInt(); exit[r - 1][c - 1] = 1; } for (int j = 0; j < N; j++) { for (int k = 0; k < M; k++) { Score[j][k] = sc.nextInt(); } } // for(int j=0;j<N;j++) { // for(int k=0;k<M;k++) { // System.out.print(map[j][k]+" "); // } // System.out.println(); // } // System.out.println(); // for(int j=0;j<N;j++) { // for(int k=0;k<M;k++) { // System.out.print(exit[j][k]+" "); // } // System.out.println(); // } // System.out.println(); BFS_fire(); // for(int j=0;j<N;j++) { // for(int k=0;k<M;k++) { // System.out.print(time_fire[j][k]+" "); // } // System.out.println(); // } BFS_Hugo(); if (check == true) { System.out.println("Case #" + (i + 1)); System.out.println(ans); } else { System.out.println("Case #" + (i + 1)); System.out.println(-1); } } } }
Editor is loading...