(UVA) Deciding Victory in Go - 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[] vi = {-1,1,0,0};
public static int[] vj = {0,0,-1,1};
public static final int LENGTH = 9;
public static int numX;
public static int numO;
public static int countPoint;
public static void dfs(int currI, int currJ) {
if (currI < 0 || currJ < 0 || currI >= LENGTH || currJ >= LENGTH || visited[currI][currJ] == true || matrix[currI][currJ] != '.') {
if (currI >= 0 && currJ >= 0 && currI < LENGTH && currJ < LENGTH) {
if (matrix[currI][currJ] == 'X') {
numX++;
}
else if (matrix[currI][currJ] == 'O') {
numO++;
}
}
return;
}
visited[currI][currJ] = true;
countPoint++;
for (int i = 0; i < 4; i++) {
int nextI = currI+vi[i];
int nextJ = currJ+vj[i];
dfs(nextI, nextJ);
}
}
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++) {
matrix = new char[LENGTH][LENGTH];
visited = new boolean[LENGTH][LENGTH];
int pointsX = 0;
int pointsO = 0;
for (int j = 0; j < LENGTH; j++) {
line = br.readLine();
for (int k = 0; k < LENGTH; k++) {
matrix[j][k] = line.charAt(k);
if (matrix[j][k] == 'X') {
pointsX++;
}
else if (matrix[j][k] == 'O') {
pointsO++;
}
}
}
for (int j = 0; j < LENGTH; j++) {
for (int k = 0; k < LENGTH; k++) {
numX = 0;
numO = 0;
countPoint = 0;
dfs(j, k);
if (numX > 0 && numO == 0) { // if '.'s are surrounded only by 'X'
pointsX += countPoint;
}
else if (numO > 0 && numX == 0) { // if '.'s are surrounded only by 'O'
pointsO += countPoint;
}
}
}
System.out.println("Black " + pointsX + " White " + pointsO);
}
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[] vi = {-1,1,0,0};
public static int[] vj = {0,0,-1,1};
public static final int LENGTH = 9;
public static int numX;
public static int numO;
public static int countPoint;
public static void dfs(int currI, int currJ) {
if (currI < 0 || currJ < 0 || currI >= LENGTH || currJ >= LENGTH || visited[currI][currJ] == true || matrix[currI][currJ] != '.') {
if (currI >= 0 && currJ >= 0 && currI < LENGTH && currJ < LENGTH) {
if (matrix[currI][currJ] == 'X') {
numX++;
}
else if (matrix[currI][currJ] == 'O') {
numO++;
}
}
return;
}
visited[currI][currJ] = true;
countPoint++;
for (int i = 0; i < 4; i++) {
int nextI = currI+vi[i];
int nextJ = currJ+vj[i];
dfs(nextI, nextJ);
}
}
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++) {
matrix = new char[LENGTH][LENGTH];
visited = new boolean[LENGTH][LENGTH];
int pointsX = 0;
int pointsO = 0;
for (int j = 0; j < LENGTH; j++) {
line = br.readLine();
for (int k = 0; k < LENGTH; k++) {
matrix[j][k] = line.charAt(k);
if (matrix[j][k] == 'X') {
pointsX++;
}
else if (matrix[j][k] == 'O') {
pointsO++;
}
}
}
for (int j = 0; j < LENGTH; j++) {
for (int k = 0; k < LENGTH; k++) {
numX = 0;
numO = 0;
countPoint = 0;
dfs(j, k);
if (numX > 0 && numO == 0) { // if '.'s are surrounded only by 'X'
pointsX += countPoint;
}
else if (numO > 0 && numX == 0) { // if '.'s are surrounded only by 'O'
pointsO += countPoint;
}
}
}
System.out.println("Black " + pointsX + " White " + pointsO);
}
return;
}
public static void main(String[] args) throws NumberFormatException, IOException {
Main m = new Main();
m.process();
System.exit(0);
}
}
Comments
Post a Comment