C. Hex
题意简要是:两人玩游戏,判断当前状态。有四种状态:不可能的出现的状态、红赢、蓝赢、还没人赢。棋盘的格子是六边形,看原题有图。红、蓝轮流下子,红下红子,蓝下篮子,随机先手。红色如果把从上到下连通了,则赢。蓝色如果把从左到右连通了,则赢。一旦有人赢棋局就结束了。
判断连通性,可以转化成图来思考。通过遍历就能知道。分别看一下四种状态如何判断。
1、不可能出现的状态:
1)红色、蓝色棋子数量差大于1
2)某一方赢了,但是棋子还少一个
3)双方都赢了.
4)某一方赢了,但是去掉最后下的棋子,还是赢的状态
2、红赢、或者蓝赢
在可能出现的状态中,如果上下通过红子连通,则红赢。如果左右通过篮子连通,则蓝赢
3、还没人赢
不是以上三种情况,则没人赢。
通过计数,和遍历比较好的判断了所有情况,只有一种例外:1种的4)去掉最后下的棋子,还是赢的状态。
接下来分析如何判断这种情况。
直观的,枚举每一个棋子,去掉一个棋子后,看是否还是赢的状态,如果不是了,就认为这是最后下的棋子。如果没有找到这个棋子,说明就是不可能出现的状态。 枚举的办法应该是可以通过small数据集,但是large就显得太慢了。
是否存在删除某个点后,这两点(这两点指的是上边和下边,把边看成一个节点,不是棋子)就不连通的,就是说这两点之间的简单路径是不是唯一。想到了最大流,如果路径唯一,最大流则是1。最大流是在边上面的容量,这里是点上的容量,所以需要对图做一点改动,把除原点和终点外,的中间节点都拆分成两个,左节点和右节点,然后通过大小为1的边连接,原图中的入边都进左节点,出边都从右节点。这样就好了。代码如下:
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.OutputStreamWriter;
- import java.math.BigInteger;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Collections;
- import java.util.LinkedList;
- import java.util.List;
- import java.util.Queue;
- public class HelloWorld {
- static int n;
- static int[] preOrder;
- static int[] backOrder;
- static int[][] graph;
- static int[] len;
- static int seq;
- static int root;
- static int[] dx = {0, 0, -1, -1, 1, 1};
- static int[] dy = {-1, 1, 0, 1, -1, 0};
- static class MaxFlow {
- class Edge {
- int to;
- int cap;
- int rev;
- Edge(int to, int cap, int rev) {
- this.to = to;
- this.cap = cap;
- this.rev = rev;
- }
- }
- int V;
- Edge[][] G;
- int[] len;
- int[] level;
- int[] iter;
- int INF = Integer.MAX_VALUE;
- MaxFlow(int n) {
- V = n;
- G = new Edge[n][16];
- len = new int[n];
- Arrays.fill(len, 0);
- level = new int[n];
- iter = new int[n];
- }
- void addEdge(int from, int to, int cap) {
- expand(from);
- expand(to);
- G[from][len[from]++] = new Edge(to, cap, len[to]);
- G[to][len[to]++] = new Edge(from, 0, len[from] - 1);
- }
- void expand(int index) {
- if (G[index].length == len[index]) {
- Edge[] tmp = new Edge[len[index] * 2];
- for (int i = 0; i < len[index]; i++)
- tmp[i] = G[index][i];
- G[index] = tmp;
- }
- }
- void bfs(int s) {
- Arrays.fill(level, -1);
- Queue<Integer> que = new LinkedList<Integer>();
- level[s] = 0;
- que.add(s);
- while (!que.isEmpty()) {
- int v = que.poll();
- for (int i = 0; i < len[v]; i++) {
- Edge e = G[v][i];
- if (e.cap > 0 && level[e.to] < 0) {
- level[e.to] = level[v] + 1;
- que.add(e.to);
- }
- }
- }
- }
- int dfs(int v, int t, int f) {
- if (v == t) return f;
- for (int i = iter[v]; i < len[v]; i++, iter[v]++) {
- Edge e = G[v][i];
- if (e.cap > 0 && level[v] < level[e.to]) {
- int d = dfs(e.to, t, Math.min(f, e.cap));
- if (d > 0) {
- e.cap -= d;
- G[e.to][e.rev].cap += d;
- return d;
- }
- }
- }
- return 0;
- }
- int maxFlow(int s, int t) {
- int flow = 0;
- for (;;) {
- bfs(s);
- if (level[t] < 0) return flow;
- Arrays.fill(iter, 0);
- int f;
- while ((f = dfs(s, t, INF)) > 0) {
- flow += f;
- }
- }
- }
- }
- public static void main(String[] args) {
- try {
- // BufferedReader br = new BufferedReader(new FileReader("D:/codejam/a.txt"));
- // BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
- // BufferedReader br = new BufferedReader(new FileReader("D:/codejam/C-small-practice.in"));
- // BufferedWriter bw = new BufferedWriter(new FileWriter("D:/codejam/C-small-result.txt"));
- // BufferedReader br = new BufferedReader(new FileReader("D:/codejam/A-small-attempt1.in"));
- BufferedReader br = new BufferedReader(new FileReader("D:/codejam/C-large-practice.in"));
- BufferedWriter bw = new BufferedWriter(new FileWriter("D:/codejam/C-large-result.txt"));
- String line;
- line = br.readLine();
- int T = Integer.parseInt(line);
- for (int cas = 1; cas <= T; cas++) {
- line = br.readLine();
- n = Integer.parseInt(line);
- String[] table = new String[n];
- int numR = 0;
- int numB = 0;
- for (int i = 0; i < n ;i++) {
- table[i] = br.readLine();
- for (int j =0 ;j < n;j++) {
- numR += (table[i].charAt(j) == 'R') ? 1 : 0;
- numB += (table[i].charAt(j) == 'B') ? 1 : 0;
- }
- }
- String ans = "";
- if (Math.abs(numR - numB) > 1) {
- ans = "Impossible";
- } else {
- boolean winR = false;
- boolean winB = false;
- int src = n * n * 2;
- int dest = n * n * 2 + 1;
- MaxFlow flow = new MaxFlow(n * n * 2 + 2);
- // dfs R
- for (int i = 0; i < n ;i++)
- for (int j = 0;j < n; j++) if (table[i].charAt(j) == 'R') {
- int node = i * n + j;
- flow.addEdge(node, node + n*n, 1);
- for (int k = 0; k< 6; k++) {
- int ni = i + dx[k];
- int nj = j + dy[k];
- if (inTable(ni, nj) && table[ni].charAt(nj) == 'R') {
- flow.addEdge(node + n * n, ni * n + nj, 1);
- }
- }
- }
- for (int i = 0; i < n; i++) {
- if (table[0].charAt(i) == 'R') {
- flow.addEdge(src, i, 1);
- }
- if (table[n-1].charAt(i) == 'R') {
- flow.addEdge((n-1) * n + i + n * n, dest, 1);
- }
- }
- int ret = flow.maxFlow(src, dest);
- if (ret > 1) ans = "Impossible";
- if (ret == 1) {
- winR = true;
- if (numR < numB) ans = "Impossible";
- }
- // dfs B;
- flow = new MaxFlow(n * n * 2 + 2);
- for (int i = 0; i < n ;i++)
- for (int j = 0;j < n; j++) if (table[i].charAt(j) == 'B') {
- int node = i * n + j;
- flow.addEdge(node, node + n*n, 1);
- for (int k = 0; k< 6; k++) {
- int ni = i + dx[k];
- int nj = j + dy[k];
- if (inTable(ni, nj) && table[ni].charAt(nj) == 'B') {
- flow.addEdge(node + n * n, ni * n + nj, 1);
- }
- }
- }
- for (int i = 0; i < n; i++) {
- if (table[i].charAt(0) == 'B') {
- flow.addEdge(src, i * n, 1);
- }
- if (table[i].charAt(n-1) == 'B') {
- flow.addEdge(i * n + n - 1 + n * n, dest, 1);
- }
- }
- ret = flow.maxFlow(src, dest);
- if (ret > 1) ans = "Impossible";
- if (ret == 1) {
- winB = true;
- if (numB < numR) ans = "Impossible";
- }
- if (ans.length() == 0) {
- if (!winR && !winB) {
- ans = "Nobody wins";
- } else if (winR && winB) {
- ans = "Impossible";
- } else if (winR) {
- ans = "Red wins";
- } else {
- ans = "Blue wins";
- }
- }
- }
- bw.write("Case #" + cas + ": " + ans);
- bw.newLine();
- bw.flush();
- }
- br.close();
- bw.close();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- private static boolean inTable(int x, int y) {
- return x >= 0 && x < n && y >= 0 && y < n;
- }
- }
- http://blog.csdn.net/albani/article/details/12685667