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.
Given a certain Go board position; decide the number of points of each player.
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.
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.
1
OX..XXO..
OXX.XOO..
OOXX.XO.O
.OOX.XOO.
..OOXXXOO
..OO.X.XO
..OOXX.XX
..OX.X...
..OXX....
Black 39 White 41
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
1OX..XXO..
OXX.XOO..
OOXX.XO.O
.OOX.XOO.
..OOXXXOO
..OO.X.XO
..OOXX.XX
..OX.X...
..OXX....
Sample Output
Black 39 White 41import 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(); } }
相关文章推荐
- UVa776 - Monkeys in a Regular Forest(种子填充法)
- UVA 784 Maze Exploration (DFS || 种子填充)
- UVa 572 Oil Desposits (dfs 图 染色 八连通种子填充)
- Uva572种子填充<dfs>
- uva 10308 - Roads in the North(dfs)
- Flood fill种子填充(DFS求连通快)
- UVA 216 Getting in Line dfs
- 种子填充算法(Uva 572)
- uva 10422 Knights in FEN(迭代dfs)
- uva 10422 Knights in FEN(DFS + 回溯)
- uva-572 种子填充floodfill
- UVA 784(dfs填充房间)
- [POJ 2631/UVA 10308 Roads in the North] DFS求树上的最长路
- uva 657 The die in cast (掷骰子) —— DFS
- uva 10422 - Knights in FEN ID+dfs
- UVa 11090 Going in Cycle!!(环上平均值最大+SPFA DFS判负环)
- UVA572 Oil Deposits(DFS求连通块(floodfill 种子填充))
- UVa 10308 Roads in the North(DFS)
- UVA 12657 Boxes in a Line
- uva 11054 Wine trading in Gergovia