(UVA) The Seasonal War - Solution 2
This solution is different from the previous one (here) because now I am using a matrix to keep the visited positions. Consequently, it is possible to discard the unidimensional array to where the initial matrix m was mapped in the previous solution.
import java.io.*;
import java.util.*;
class Main {
public static int[][] visited;
public static int[][] m;
public static void bfs(int startPosI, int startPosJ, int squareDimension) {
Queue<Coord> queue = new ArrayDeque<Coord>();
Coord c = new Coord(startPosI, startPosJ);
queue.add(c);
int[] vi = {-1,-1,-1,0,0,1,1,1};
int[] vj = {-1,0,1,-1,1,-1,0,1};
while (queue.size() > 0) {
Coord tmp = queue.poll();
int currPosI = tmp.i;
int currPosJ = tmp.j;
visited[currPosI][currPosJ] = 1;
for (int i = 0; i < 8; i++) {
int nextPosI = vi[i]+currPosI;
int nextPosJ = vj[i]+currPosJ;
if (nextPosI >= 0 && nextPosJ >= 0 && nextPosI < squareDimension && nextPosJ < squareDimension && m[nextPosI][nextPosJ] == 1 && visited[nextPosI][nextPosJ] == 0) {
c = new Coord(nextPosI, nextPosJ);
queue.add(c);
}
}
}
}
public static int checkComponents(int squareDimension) {
int components = 0;
for (int i = 0; i < squareDimension; i++) {
for (int j = 0; j < squareDimension; j++) {
if (m[i][j] == 1 && visited[i][j] == 0) {
bfs(i, j, squareDimension);
components++;
}
}
}
return components;
}
public static void readMatrix(BufferedReader br, int squareDimension) throws NumberFormatException, IOException {
for (int i = 0; i < squareDimension; i++) {
String matrixLine = br.readLine();
for (int j = 0; j < matrixLine.length(); j++) {
m[i][j] = matrixLine.charAt(j)-'0';
}
}
}
public static void process() throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int testCase = 0;
String line = br.readLine();
while (line != null) {
int squareDimension = Integer.parseInt(line);
visited = new int[squareDimension][squareDimension];
m = new int[squareDimension][squareDimension];
readMatrix(br, squareDimension);
int components = checkComponents(squareDimension);
System.out.println("Image number " + (++testCase) + " contains " + components + " war eagles.");
line = br.readLine();
}
return;
}
public static void main(String[] args) throws NumberFormatException, IOException {
Main m = new Main();
m.process();
System.exit(0);
}
}
class Coord {
int i;
int j;
public Coord(int i, int j) {
this.i = i;
this.j = j;
}
}
import java.io.*;
import java.util.*;
class Main {
public static int[][] visited;
public static int[][] m;
public static void bfs(int startPosI, int startPosJ, int squareDimension) {
Queue<Coord> queue = new ArrayDeque<Coord>();
Coord c = new Coord(startPosI, startPosJ);
queue.add(c);
int[] vi = {-1,-1,-1,0,0,1,1,1};
int[] vj = {-1,0,1,-1,1,-1,0,1};
while (queue.size() > 0) {
Coord tmp = queue.poll();
int currPosI = tmp.i;
int currPosJ = tmp.j;
visited[currPosI][currPosJ] = 1;
for (int i = 0; i < 8; i++) {
int nextPosI = vi[i]+currPosI;
int nextPosJ = vj[i]+currPosJ;
if (nextPosI >= 0 && nextPosJ >= 0 && nextPosI < squareDimension && nextPosJ < squareDimension && m[nextPosI][nextPosJ] == 1 && visited[nextPosI][nextPosJ] == 0) {
c = new Coord(nextPosI, nextPosJ);
queue.add(c);
}
}
}
}
public static int checkComponents(int squareDimension) {
int components = 0;
for (int i = 0; i < squareDimension; i++) {
for (int j = 0; j < squareDimension; j++) {
if (m[i][j] == 1 && visited[i][j] == 0) {
bfs(i, j, squareDimension);
components++;
}
}
}
return components;
}
public static void readMatrix(BufferedReader br, int squareDimension) throws NumberFormatException, IOException {
for (int i = 0; i < squareDimension; i++) {
String matrixLine = br.readLine();
for (int j = 0; j < matrixLine.length(); j++) {
m[i][j] = matrixLine.charAt(j)-'0';
}
}
}
public static void process() throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int testCase = 0;
String line = br.readLine();
while (line != null) {
int squareDimension = Integer.parseInt(line);
visited = new int[squareDimension][squareDimension];
m = new int[squareDimension][squareDimension];
readMatrix(br, squareDimension);
int components = checkComponents(squareDimension);
System.out.println("Image number " + (++testCase) + " contains " + components + " war eagles.");
line = br.readLine();
}
return;
}
public static void main(String[] args) throws NumberFormatException, IOException {
Main m = new Main();
m.process();
System.exit(0);
}
}
class Coord {
int i;
int j;
public Coord(int i, int j) {
this.i = i;
this.j = j;
}
}
Comments
Post a Comment