(UVA) Maze Exploration - Solution
I used Depth-First Search to solve this problem.
import java.io.*;
import java.util.*;
import java.lang.*;
class Main {
public static char[][] matrix;
public static int[] vx = {-1,1,0,0};
public static int[] vy = {0,0,1,-1};
public static void dfs(int currX, int currY, int numRows, int numCols) {
if (currX < 0 || currY < 0 || currX >= numRows || currY >= numCols || matrix[currX][currY] == '#' || (matrix[currX][currY] != ' ' && matrix[currX][currY] != '*')) {
return;
}
matrix[currX][currY] = '#';
for (int i = 0; i < 4; i++) {
int nextX = currX+vx[i];
int nextY = currY+vy[i];
dfs(nextX, nextY, numRows, numCols);
}
}
public static void process() throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
int numCases = Integer.parseInt(line);
for (int i = 0; i < numCases; i++) {
ArrayList<String> strings = new ArrayList<String>();
line = br.readLine();
while (line.charAt(0) != '_') {
strings.add(line);
line = br.readLine();
}
String separate = line;
int maxLength = 0;
for (int j = 0; j < strings.size(); j++) {
if (strings.get(j).length() > maxLength) {
maxLength = strings.get(j).length();
}
}
int x = -1;
int y = -1;
matrix = new char[strings.size()][maxLength];
for (int j = 0; j < strings.size(); j++) {
for (int k = 0; k < strings.get(j).length(); k++) {
matrix[j][k] = strings.get(j).charAt(k);
if (strings.get(j).charAt(k) == '*') {
x = j;
y = k;
}
}
}
dfs(x, y, strings.size(), maxLength);
for (int j = 0; j < strings.size(); j++) {
for (int k = 0; k < strings.get(j).length(); k++) {
System.out.print(matrix[j][k]);
}
System.out.println();
}
System.out.println(separate);
}
return;
}
public static void main(String[] args) throws NumberFormatException, IOException {
Main m = new Main();
m.process();
System.exit(0);
}
}
import java.io.*;
import java.util.*;
import java.lang.*;
class Main {
public static char[][] matrix;
public static int[] vx = {-1,1,0,0};
public static int[] vy = {0,0,1,-1};
public static void dfs(int currX, int currY, int numRows, int numCols) {
if (currX < 0 || currY < 0 || currX >= numRows || currY >= numCols || matrix[currX][currY] == '#' || (matrix[currX][currY] != ' ' && matrix[currX][currY] != '*')) {
return;
}
matrix[currX][currY] = '#';
for (int i = 0; i < 4; i++) {
int nextX = currX+vx[i];
int nextY = currY+vy[i];
dfs(nextX, nextY, numRows, numCols);
}
}
public static void process() throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
int numCases = Integer.parseInt(line);
for (int i = 0; i < numCases; i++) {
ArrayList<String> strings = new ArrayList<String>();
line = br.readLine();
while (line.charAt(0) != '_') {
strings.add(line);
line = br.readLine();
}
String separate = line;
int maxLength = 0;
for (int j = 0; j < strings.size(); j++) {
if (strings.get(j).length() > maxLength) {
maxLength = strings.get(j).length();
}
}
int x = -1;
int y = -1;
matrix = new char[strings.size()][maxLength];
for (int j = 0; j < strings.size(); j++) {
for (int k = 0; k < strings.get(j).length(); k++) {
matrix[j][k] = strings.get(j).charAt(k);
if (strings.get(j).charAt(k) == '*') {
x = j;
y = k;
}
}
}
dfs(x, y, strings.size(), maxLength);
for (int j = 0; j < strings.size(); j++) {
for (int k = 0; k < strings.get(j).length(); k++) {
System.out.print(matrix[j][k]);
}
System.out.println();
}
System.out.println(separate);
}
return;
}
public static void main(String[] args) throws NumberFormatException, IOException {
Main m = new Main();
m.process();
System.exit(0);
}
}
Comments
Post a Comment