hgcn
dsadunknown
java
a year ago
4.1 kB
7
Indexable
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.util.Scanner; public class Solution { public static void main(String[] args) throws FileNotFoundException { System.setIn(new FileInputStream( "D:/LeCongMinh/B8/HugoChayNha/input.txt")); Scanner sc = new Scanner(System.in); int tc = sc.nextInt(); for (int t = 1; t <= tc; t++) { n = sc.nextInt(); m = sc.nextInt(); posX = sc.nextInt()-1; posY = sc.nextInt()-1; int n_lua = sc.nextInt(); matrix = new int[n][m]; matrixLua = new int[n][m]; visitLua = new int[n][m]; queue = new Queue(50000); for(int i = 0;i<n_lua;i++){ x = sc.nextInt()-1; y = sc.nextInt()-1; matrixLua[x][y] = 1; matrix[x][y]=1; queue.enQueue(x); queue.enQueue(y); visitLua[x][y] = 1; } int n_ho = sc.nextInt(); for(int i = 0;i<n_ho;i++){ x = sc.nextInt()-1; y = sc.nextInt()-1; matrix[x][y] = 2; } lanMatrixLua(); int n_loithoat = sc.nextInt(); loiThoat = new int[n][m]; for(int i = 0;i<n_loithoat;i++){ x = sc.nextInt()-1; y = sc.nextInt()-1; loiThoat[x][y] = 1; } kimCuong = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { kimCuong[i][j] = sc.nextInt(); } } visit = new int[n][m]; res = 0; visit[posX][posY] = 1; thoatDuoc = false; backtrack(posX,posY,0,0); System.out.println("Case #" + t); if(!thoatDuoc){ System.out.println(-1); }else{ System.out.println(res+kimCuong[posX][posY]); } } sc.close(); } public static int res; public static int n, m, posX, posY,x,y,countKC,q; public static boolean thoatDuoc; public static int[][] matrix; public static int[][] visit; public static int[][] visitLua; public static int[][] matrixKC; public static int[][] matrixLua; public static int[][] loiThoat; public static int[][] kimCuong; public static int[] lua; public static Queue queue; public static int[] moveX = { 0, -1, 0, 1 }; public static int[] moveY = { -1, 0, 1, 0 }; public static class Queue{ int front,rear; int[] data; Queue(int size){ front =rear = -1; data = new int[size]; } boolean isEmpty(){ return front == rear; } void enQueue(int x){ data[++rear] = x; } int deQueue(){ return data[++front]; } void reset(){ front = rear= -1; } } public static void print(){ for(int i = 0;i<matrixLua.length;i++){ for(int j = 0;j<matrixLua[i].length;j++){ System.out.print(matrixLua[i][j]+" "); } System.out.println(); } } public static boolean checkBien(int x, int y) { if (x < 0 || x >= n || y < 0 || y >= m) { return false; } return true; } public static void lanMatrixLua(){ while(!queue.isEmpty()){ int currX = queue.deQueue(); int currY = queue.deQueue(); for(int i = 0;i<4;i++){ int newX = currX + moveX[i]; int newY = currY + moveY[i]; if(checkBien(newX, newY) && matrix[newX][newY]!=2 && visitLua[newX][newY] == 0){ queue.enQueue(newX); queue.enQueue(newY); visitLua[newX][newY] = 1; matrixLua[newX][newY] = matrixLua[currX][currY]+1; } } } } public static boolean checkStep(int a,int b,int time){ if(matrixLua[a][b] == 0) return true; if(matrixLua[a][b]-1 <= time) return false; return true; } public static void backtrack(int currX,int currY,int total,int step){ if(loiThoat[currX][currY]==1){ if(res<total){ res = total; } thoatDuoc = true; } for(int i = 0;i<4;i++){ int newX = currX + moveX[i]; int newY = currY + moveY[i]; int tmp; if(matrix[currX][currY] == 2){ tmp = 2; }else{ tmp = 1; } if(checkBien(newX, newY) && checkStep(newX, newY, step+tmp) && visit[newX][newY]==0 && matrix[newX][newY]!=1){ visit[newX][newY] = 1; backtrack(newX,newY,total+kimCuong[newX][newY],step+tmp); visit[newX][newY] = 0; } } } }
Editor is loading...
Leave a Comment