(UVA) Wetlands of Florida - Solution
I used Depth-First Search to solve this problem.
import java.io.*;
import java.util.*;
class Main {
public static char[][] matrix;
public static boolean[][] visited;
public static int[] vx = {-1,-1,-1,0,0,1,1,1};
public static int[] vy = {-1,0,1,-1,1,-1,0,1};
public static int area;
public static int numRows;
public static int numCols;
public static void dfs(int currRow, int currCol) {
if (currRow < 0 || currCol < 0 || currRow >= numRows || currCol >= numCols || visited[currRow][currCol] || matrix[currRow][currCol] == 'L') {
return;
}
area += 1;
visited[currRow][currCol] = true;
for (int i = 0; i < 8; i++) {
int nextRow = currRow+vx[i];
int nextCol = currCol+vy[i];
dfs(nextRow, nextCol);
}
}
public static void process() throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int numCases = Integer.parseInt(br.readLine());
br.readLine(); // blank line
for (int i = 0; i < numCases; i++) {
if (i > 0) {
System.out.println();
}
matrix = new char[100][100];
int indexLine = 0;
String line = br.readLine();
while (line.charAt(0) == 'L' || line.charAt(0) == 'W') {
for (int j = 0; j < line.length(); j++) {
matrix[indexLine][j] = line.charAt(j);
}
numCols = line.length();
indexLine++;
line = br.readLine();
}
numRows = indexLine;
while (!line.equals("")) {
String[] s = line.split("\\s");
int row = Integer.parseInt(s[0]);
int col = Integer.parseInt(s[1]);
area = 0;
visited = new boolean[numRows][numCols];
dfs(row-1, col-1);
System.out.println(area);
line = br.readLine();
if (line == null) {
break;
}
}
}
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.*;
class Main {
public static char[][] matrix;
public static boolean[][] visited;
public static int[] vx = {-1,-1,-1,0,0,1,1,1};
public static int[] vy = {-1,0,1,-1,1,-1,0,1};
public static int area;
public static int numRows;
public static int numCols;
public static void dfs(int currRow, int currCol) {
if (currRow < 0 || currCol < 0 || currRow >= numRows || currCol >= numCols || visited[currRow][currCol] || matrix[currRow][currCol] == 'L') {
return;
}
area += 1;
visited[currRow][currCol] = true;
for (int i = 0; i < 8; i++) {
int nextRow = currRow+vx[i];
int nextCol = currCol+vy[i];
dfs(nextRow, nextCol);
}
}
public static void process() throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int numCases = Integer.parseInt(br.readLine());
br.readLine(); // blank line
for (int i = 0; i < numCases; i++) {
if (i > 0) {
System.out.println();
}
matrix = new char[100][100];
int indexLine = 0;
String line = br.readLine();
while (line.charAt(0) == 'L' || line.charAt(0) == 'W') {
for (int j = 0; j < line.length(); j++) {
matrix[indexLine][j] = line.charAt(j);
}
numCols = line.length();
indexLine++;
line = br.readLine();
}
numRows = indexLine;
while (!line.equals("")) {
String[] s = line.split("\\s");
int row = Integer.parseInt(s[0]);
int col = Integer.parseInt(s[1]);
area = 0;
visited = new boolean[numRows][numCols];
dfs(row-1, col-1);
System.out.println(area);
line = br.readLine();
if (line == null) {
break;
}
}
}
return;
}
public static void main(String[] args) throws NumberFormatException, IOException {
Main m = new Main();
m.process();
System.exit(0);
}
}
Comments
Post a Comment