Java 迷宫破关
Java maze breaking closure
我有下一个迷宫 class。我只需要添加一项优化。如果for循环中没有maze = readFromFile();
,方法检查只运行一次,因为maze
数组在check
方法中被修改。我想避免每次迭代都从文件中读取。我正在尝试复制我的 maze
数组以避免它发生变异,但没有成功。
public class Maze {
private static char[][] maze;
private static int size;
private static Cell startCell = null;
private static ArrayList<String> resultPaths = new ArrayList<>();
private static final String INPUT_FILE_NAME = "Problem.in";
private static final String OUTPUT_FILE_NAME = "Problem.out";
private static char[][] readFromFile() throws FileNotFoundException {
Scanner scanner = new Scanner(new File(Maze.INPUT_FILE_NAME));
try {
// Read maze size
size = scanner.nextInt();
scanner.nextLine();
// Create the maze
maze = new char[size][size];
// Read the maze cells from the file
for (int row = 0; row < size; row++) {
String line = scanner.nextLine();
for (int col = 0; col < line.length(); col++) {
char ch = line.charAt(col);
maze[row][col] = ch;
if (ch == '*') {
startCell = new Cell(row, col);
}
}
}
return maze;
} finally {
scanner.close();
}
}
public static void saveResult(String fileName, ArrayList<String> resultPaths) throws IOException {
FileWriter writer = new FileWriter(fileName);
try {
for (String resultPath : resultPaths) {
writer.write("" + resultPath + "\n");
}
} finally {
writer.close();
}
}
private static void check(int x, int y, int dest_x, int dest_y, char[][] maze, String path) {
if (x < 0 || y < 0 || x >= size || y >= size || maze[y][x] == '#') {
return;
}
if (maze[y][x] != '*') {
path += String.valueOf(maze[y][x]);
}
if (x == dest_x && y == dest_y) {
System.out.println(path);
resultPaths.add(path);
return;
} else {
maze[y][x] = '#';
check (x + 0, y - 1, dest_x, dest_y, maze, path);
check (x + 0, y + 1, dest_x, dest_y, maze, path);
check (x - 1, y + 0, dest_x, dest_y, maze, path);
check (x + 1, y + 0, dest_x, dest_y, maze, path);
maze[y][x] = '#';
}
}
public static ArrayList<Cell> getExits(char[][] maze) {
ArrayList<Cell> result = new ArrayList<>();
for (int row = 0; row < size; row++) {
for (int col = 0; col < size; col++) {
char ch = maze[row][col];
maze[row][col] = ch;
if (ch == '*') {
startCell = new Cell(row, col);
}
if ((ch != '#') && (row == 0 || col == 0 || row == (size - 1) || col == (size - 1))) {
result.add(new Cell(row, col));
}
}
}
return result;
}
public static void main(String[] args) throws IOException {
maze = readFromFile();
ArrayList<Cell> possibleExits = getExits(maze);
String path = "";
for (Cell currentCell : possibleExits) {
maze = readFromFile(); // HERE I NEED TO REPLACE THIS WITH COPY OF CURRENT MAZE CHAR ARRAY
check(startCell.getCol(), startCell.getRow(), currentCell.getCol(), currentCell.getRow(), maze, path);
}
saveResult(OUTPUT_FILE_NAME, resultPaths);
}
}
输入文件:
6
a##km#
z#ada#
a*m###
#d####
rifid#
#d#d#t
输出:
aza
madk
madamk
madkm
madam
az
a
dir
did
difid
你可以在copyMaze
上操作,cleanMaze
不会改变,你每次需要新迷宫的时候都可以做那个副本。从文件中读取后永远不要修改 cleanMaze
。
final char[][] cleanMaze = readFromFile(); // never modify this again
char[][] copyMaze = new char[size][size];
for (int i = 0; i < size; i++) {
System.arraycopy(cleanMaze[i], 0, copyMaze[i], 0, size);
}
System.arracopy javadoc
不要试图从循环中删除副本,这是行不通的。它只会复制第一个维度,修改第二个维度会在两个数组中都修改它,这就是为什么我们需要复制所有内容。
我有下一个迷宫 class。我只需要添加一项优化。如果for循环中没有maze = readFromFile();
,方法检查只运行一次,因为maze
数组在check
方法中被修改。我想避免每次迭代都从文件中读取。我正在尝试复制我的 maze
数组以避免它发生变异,但没有成功。
public class Maze {
private static char[][] maze;
private static int size;
private static Cell startCell = null;
private static ArrayList<String> resultPaths = new ArrayList<>();
private static final String INPUT_FILE_NAME = "Problem.in";
private static final String OUTPUT_FILE_NAME = "Problem.out";
private static char[][] readFromFile() throws FileNotFoundException {
Scanner scanner = new Scanner(new File(Maze.INPUT_FILE_NAME));
try {
// Read maze size
size = scanner.nextInt();
scanner.nextLine();
// Create the maze
maze = new char[size][size];
// Read the maze cells from the file
for (int row = 0; row < size; row++) {
String line = scanner.nextLine();
for (int col = 0; col < line.length(); col++) {
char ch = line.charAt(col);
maze[row][col] = ch;
if (ch == '*') {
startCell = new Cell(row, col);
}
}
}
return maze;
} finally {
scanner.close();
}
}
public static void saveResult(String fileName, ArrayList<String> resultPaths) throws IOException {
FileWriter writer = new FileWriter(fileName);
try {
for (String resultPath : resultPaths) {
writer.write("" + resultPath + "\n");
}
} finally {
writer.close();
}
}
private static void check(int x, int y, int dest_x, int dest_y, char[][] maze, String path) {
if (x < 0 || y < 0 || x >= size || y >= size || maze[y][x] == '#') {
return;
}
if (maze[y][x] != '*') {
path += String.valueOf(maze[y][x]);
}
if (x == dest_x && y == dest_y) {
System.out.println(path);
resultPaths.add(path);
return;
} else {
maze[y][x] = '#';
check (x + 0, y - 1, dest_x, dest_y, maze, path);
check (x + 0, y + 1, dest_x, dest_y, maze, path);
check (x - 1, y + 0, dest_x, dest_y, maze, path);
check (x + 1, y + 0, dest_x, dest_y, maze, path);
maze[y][x] = '#';
}
}
public static ArrayList<Cell> getExits(char[][] maze) {
ArrayList<Cell> result = new ArrayList<>();
for (int row = 0; row < size; row++) {
for (int col = 0; col < size; col++) {
char ch = maze[row][col];
maze[row][col] = ch;
if (ch == '*') {
startCell = new Cell(row, col);
}
if ((ch != '#') && (row == 0 || col == 0 || row == (size - 1) || col == (size - 1))) {
result.add(new Cell(row, col));
}
}
}
return result;
}
public static void main(String[] args) throws IOException {
maze = readFromFile();
ArrayList<Cell> possibleExits = getExits(maze);
String path = "";
for (Cell currentCell : possibleExits) {
maze = readFromFile(); // HERE I NEED TO REPLACE THIS WITH COPY OF CURRENT MAZE CHAR ARRAY
check(startCell.getCol(), startCell.getRow(), currentCell.getCol(), currentCell.getRow(), maze, path);
}
saveResult(OUTPUT_FILE_NAME, resultPaths);
}
}
输入文件:
6
a##km#
z#ada#
a*m###
#d####
rifid#
#d#d#t
输出:
aza
madk
madamk
madkm
madam
az
a
dir
did
difid
你可以在copyMaze
上操作,cleanMaze
不会改变,你每次需要新迷宫的时候都可以做那个副本。从文件中读取后永远不要修改 cleanMaze
。
final char[][] cleanMaze = readFromFile(); // never modify this again
char[][] copyMaze = new char[size][size];
for (int i = 0; i < size; i++) {
System.arraycopy(cleanMaze[i], 0, copyMaze[i], 0, size);
}
System.arracopy javadoc
不要试图从循环中删除副本,这是行不通的。它只会复制第一个维度,修改第二个维度会在两个数组中都修改它,这就是为什么我们需要复制所有内容。