Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Street light
//'main' method must be in a class 'Rextester'. //Compiler version 1.8.0_111 import java.util.*; import java.lang.*; class Rextester { public static void main(String args[]) { Readfile(); } public static void Excute(int xStart, int yStart, int[][] matrix, int cost, int rows, int cols) { if (xStart == (rows - 1) && yStart == (cols - 1)) { if (min >= cost) { if(min!=cost) { count=0; } min = cost; count++; } } else { for (int i = 0; i < 4; i++) { int tempX = xStart + xPoint[i]; int tempY = yStart + yPoint[i]; if (0 <= tempX && tempX < rows && 0 <= tempY && tempY < cols) { if (matrix[tempX][tempY] == 3) { matrix[tempX][tempY] = 4; Excute(tempX, tempY, matrix, cost + 1, rows, cols); matrix[tempX][tempY] = 3; } } } } } public static void CreateNewMatrix(int[][] matrix, int xLight, int yLight, int rows, int cols) { for (int i = 0; i < 4; i++) { int tempX = xLight; int tempY = yLight; do { tempX = tempX + xPoint[i]; tempY = tempY + yPoint[i]; if (0 <= tempX && tempX < rows && 0 <= tempY && tempY < cols && (matrix[tempX][tempY] == 3 || matrix[tempX][tempY] == 0)) { matrix[tempX][tempY] = 3; } else break; } while (true); } // for (int i = 0; i < rows; i++) { // for (int j = 0; j < cols; j++) { // System.out.print(matrix[i][j] + " "); // } // System.out.println(); // } // System.out.println(); } public static void Readfile() { BufferedReader reader; try { reader = new BufferedReader(new FileReader("D:\\Long\\Homework\\Street light\\input.txt")); String line = reader.readLine(); int case_seat = Integer.parseInt(line); line = reader.readLine(); while (line != null) { String[] size_matrix = line.split(" "); if (size_matrix.length == 2) { int rows = Integer.parseInt(size_matrix[0]); int cols = Integer.parseInt(size_matrix[1]); int[][] matrix = new int[rows][cols]; line = reader.readLine(); for (int row = 0; row < rows; row++) { if (row != rows) { String[] row_matrix = line.split(" "); for (int col = 0; col < cols; col++) { try { matrix[row][col] = Integer.parseInt(row_matrix[col]); } catch (Exception e) { } } line = reader.readLine(); } } for (int row = 0; row < rows; row++) { for (int col = 0; col < cols; col++) { if (matrix[row][col] == 2) { matrix[row][col] = 3; CreateNewMatrix(matrix, row, col, rows, cols); } } } min=Integer.MAX_VALUE;; count=0; matrix[0][0]=4; Excute(0, 0, matrix, 0, rows, cols); System.out.println(count); } } } catch (IOException e) { e.printStackTrace(); } } }
run
|
edit
|
history
|
help
0
[a-zA-Z ]{0,12}
Java
// Java Coding Challenge - 08: Reversing a Number using String
First Interview question - Java
1a
reverse Linked List
pattern2
1.7
hello_world_test
Человечки