您的位置:首页 > 编程语言 > Go语言

UVa852 - Deciding victory in Go(DFS,种子填充法)

2014-09-15 15:58 190 查看
The history of Go stretches back some 3000 years and the rules have remained essentially unchanged throughout this very long period. The game probably originated in China or the Himalayas. In the Far East, where it originated, Go enjoys great popularity today
and interest in the game is growing steadily in Europe and America. 

A game of Go starts with an empty square board and each player has an unlimited supply of stones, one taking the black stones, the other taking white. The basic object of the game is to use one’s stones to form territories by surrounding vacant areas of the
board. It is also possible to capture the opponent’s stones by completely surrounding them. The players take turns, placing one of their stones on a vacant point at each turn, Black playing first. Note that the stones are placed on the intersections of the
lines rather than in the squares (there are no diagonal connections). Once played, stones are not moved although they may be captured, in which case they are removed from the board. At the end of the game (when both players pass) the players count one point
for each vacant point inside their own territory and one point for every stone they have on the board. The player with the larger total of points wins.


Problem

Given a certain Go board position; decide the number of points of each player.

 


Example

Black has surrounded 15 points of territory: 3 at the top of the board, 2 points just below, 9 in the lower right corner plus one point for the territory at intersection a. Adding the actual stones on board (24 stones), Black has a total of 39 points.White’s
territory is 17 points: 11 on the left sector plus 6 on the right sector. With 24 stones on board, he has a total of 41 points. So, White wins the game by two points.Notice that intersection b does not belong to either player.

 


Input

The input file will contain one line with one integer defining the number of board positions. On the following lines the positions are presented. Each position consists of nine lines of nine characters: X for black stones, O for white stones and a ‘.’ (a dot)
for empty intersections. There is no empty line separating each problem set. 


Output

Correct output consists of a set of lines (one for each problem solution) where each line consists of: Black <Black points> White <White points> newline.


Sample Input

1

OX..XXO..

OXX.XOO..

OOXX.XO.O

.OOX.XOO.

..OOXXXOO

..OO.X.XO

..OOXX.XX

..OX.X...

..OXX.... 



Sample Output

Black 39 White 41

 

import java.io.FileInputStream;
import java.io.BufferedInputStream;
import java.io.PrintWriter;
import java.io.OutputStreamWriter;
import java.util.Scanner;

public class Main implements Runnable{
private static final boolean DEBUG = false;
private static final int N = 9;
private Scanner cin;
private PrintWriter cout;
private char[][] grid = new char

;
private int[][] vis;
private boolean found;
private int cnt;

private void init()
{
try {
if (DEBUG) {
cin = new Scanner(new BufferedInputStream(
new FileInputStream("e:\\uva_in.txt")));
} else {
cin = new Scanner(new BufferedInputStream(System.in));
}

cout = new PrintWriter(new OutputStreamWriter(System.out));
} catch (Exception e) {
e.printStackTrace();
}
}

private void input()
{
for (int i = 0; i < N; i++) {
String s = cin.next();
grid[i] = s.toCharArray();
}
}

private int dfs(int x, int y, char ch, int v)
{
if (x < 0 || x >= N || y < 0 || y >= N || vis[x][y] != 0) return 0;

if (grid[x][y] != ch) return 0;
else {
vis[x][y] = v;

}

int ans = 1;

ans += dfs(x - 1, y, ch, v);
ans += dfs(x + 1, y, ch, v);
ans += dfs(x, y - 1, ch, v);
ans += dfs(x, y + 1, ch, v);

return ans;
}

private void dfs2(int x, int y, char ch, int v)
{
if (x < 0 || x >= N || y < 0 || y >= N) return ;

if (grid[x][y] == ch && vis[x][y] == 0) {
vis[x][y] = v;
cnt++;
dfs2(x - 1, y, ch, v);
dfs2(x + 1, y, ch, v);
dfs2(x, y - 1, ch, v);
dfs2(x, y + 1, ch, v);
} else {
if (vis[x][y] != v) {
found = false;
}

return;
}
}

private void solve()
{
vis = new int

;

int black = 0, white = 0;

for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (grid[i][j] != '.') {
if (grid[i][j] == 'X' && vis[i][j] == 0) {

black += dfs(i, j, 'X', 1);
} else if (grid[i][j] == 'O' && vis[i][j] == 0) {

white += dfs(i, j, 'O', 2);

}
}
}
}

for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (grid[i][j] == 'X') {
if (i > 0 && grid[i - 1][j] == '.' && vis[i - 1][j] == 0) {
found = true;
cnt = 0;
dfs2(i - 1, j, '.', 1);
if (found) black += cnt;
}

if (i < N - 1 && grid[i + 1][j] == '.' && vis[i + 1][j] == 0) {
found = true;
cnt = 0;
dfs2(i + 1, j, ',', 1);
if (found) black += cnt;
}

if (j > 0 && grid[i][j - 1] == '.' && vis[i][j - 1] == 0) {
found = true;
cnt = 0;
dfs2(i, j - 1, '.', 1);
if (found) black += cnt;
}

if (j < N - 1 && grid[i][j + 1] == '.' && vis[i][j + 1] == 0) {
found = true;
cnt = 0;
dfs2(i, j + 1, '.', 1);
if (found) black += cnt;
}
}else if (grid[i][j] == 'O') {
if (i > 0 && grid[i - 1][j] == '.' && vis[i - 1][j] == 0) {
found = true;
cnt = 0;
dfs2(i - 1, j, '.', 2);
if (found) white += cnt;
}

if (i < N - 1 && grid[i + 1][j] == '.' && vis[i + 1][j] == 0) {
found = true;
cnt = 0;
dfs2(i + 1, j, '.', 2);
if (found) white += cnt;
}

if (j > 0 && grid[i][j - 1] == '.' && vis[i][j - 1] == 0) {
found = true;
cnt = 0;
dfs2(i, j - 1, '.', 2);
if (found) white += cnt;
}

if (j < N - 1 && grid[i][j + 1] == '.' && vis[i][j + 1] == 0) {
found = true;
cnt = 0;
dfs2(i, j + 1, '.', 2);
if (found) white += cnt;
}
}
}
}

cout.println("Black " + black + " White " + white);
cout.flush();
}

public void run()
{
init();

int t = cin.nextInt();
while (t-- > 0) {
input();
solve();
}
}

public static void main(String[] args)
{
new Thread(new Main()).start();
}
}


 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: