(UVA) Counting Stars - 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 count;
public static int row;
public static int col;
public static void dfs(int currX, int currY) {
if (currX < 0 || currY < 0 || currX >= row || currY >= col || matrix[currX][currY] != '*' || visited[currX][currY]) {
return;
}
count++;
visited[currX][currY] = true;
for (int i = 0; i < 8; i++) {
int nextX = currX+vx[i];
int nextY = currY+vy[i];
dfs(nextX, nextY);
}
}
public static void process() throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
String[] s = line.split("\\s");
row = Integer.parseInt(s[0]);
col = Integer.parseInt(s[1]);
while (row != 0 || col != 0) {
matrix = new char[row][col];
for (int i = 0; i < row; i++) {
line = br.readLine();
for (int j = 0; j < col; j++) {
matrix[i][j] = line.charAt(j);
}
}
int countStars = 0;
visited = new boolean[row][col];
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
count = 0;
dfs(i, j);
if (count == 1) {
countStars++;
}
}
}
System.out.println(countStars);
line = br.readLine();
s = line.split("\\s");
row = Integer.parseInt(s[0]);
col = Integer.parseInt(s[1]);
}
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 count;
public static int row;
public static int col;
public static void dfs(int currX, int currY) {
if (currX < 0 || currY < 0 || currX >= row || currY >= col || matrix[currX][currY] != '*' || visited[currX][currY]) {
return;
}
count++;
visited[currX][currY] = true;
for (int i = 0; i < 8; i++) {
int nextX = currX+vx[i];
int nextY = currY+vy[i];
dfs(nextX, nextY);
}
}
public static void process() throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
String[] s = line.split("\\s");
row = Integer.parseInt(s[0]);
col = Integer.parseInt(s[1]);
while (row != 0 || col != 0) {
matrix = new char[row][col];
for (int i = 0; i < row; i++) {
line = br.readLine();
for (int j = 0; j < col; j++) {
matrix[i][j] = line.charAt(j);
}
}
int countStars = 0;
visited = new boolean[row][col];
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
count = 0;
dfs(i, j);
if (count == 1) {
countStars++;
}
}
}
System.out.println(countStars);
line = br.readLine();
s = line.split("\\s");
row = Integer.parseInt(s[0]);
col = Integer.parseInt(s[1]);
}
return;
}
public static void main(String[] args) throws NumberFormatException, IOException {
Main m = new Main();
m.process();
System.exit(0);
}
}
Comments
Post a Comment