火柴游戏

这是一个纵横火柴棒游戏。
如图1,在3x4的格子中,游戏的双方轮流放置火柴棒。
其规则是:
1. 不能放置在已经放置了火柴棒的地方(即只能在空格中放置)。
2. 火柴棒的方向只能是竖直或水平放置。
3. 火柴棒不能与其它格子中的火柴“连通”。
所谓连通是指两根火柴棒可以连成一条直线,且中间没有其它不同方向的火柴“阻拦”。
例如:
图1所示的局面下,可以在C2位置竖直放置(为了方便描述格子位置,图中左、下都添加了标记),但不能水平放置,因为会与A2连通。
同样道理,B2,B3,D2此时两种方向都不可以放置。
但如果C2竖直放置后,D2就可以水平放置了,因为不再会与A2连通(受到了C2的阻挡)。
4. 游戏双方轮流放置火柴,不可以弃权,也不可以放多根。

如某一方无法继续放置,则该方为负(输的一方)。

游戏开始时可能已经放置了多根火柴。
你的任务是:编写程序,读入初始状态,计算出对自己最有利的放置方法并输出放置后的局面。
图1的局面表示为:
00-1
-000
0100
即用“0”表示空闲位置,用“1”表示竖直放置,用“-”表示水平放置。

解法不唯一,找到任意解法即可。

例如,局面:
0111
-000
-000
的解:
-111
-000
-000

再例如,局面:
1111
----
0010
的解:
1111
----
0110

import java.util.*;

public class Main {
    static void show(char[][] data) {
        System.out.println();
        for (int i = 0; i < data.length; i++) {
            System.out.println(new String(data[i]));
        }
    }

    static boolean f(char[][] data) {
        for (int i = 0; i < data.length; i++) {
            String s = new String(data[i]).replaceAll("0", "");
            if (s.contains("--"))
                return true;
        }

        for (int i = 0; i < data[0].length; i++) {
            String s = ("" + data[0][i] + data[1][i] + data[2][i]).replaceAll("0", "");
            if (s.contains("11"))
                return true;
        }

        for (int i = 0; i < data.length; i++) {
            for (int j = 0; j < data[i].length; j++) {
                if (data[i][j] == '0') {
                    try {
                        data[i][j] = '1';
                        if (f(data) == false)
                            return true;
                        data[i][j] = '-';
                        if (f(data) == false)
                            return true;
                    } finally {
                        data[i][j] = '0';
                    }
                }
            }
        }

        return false;
    }

    static void solve(char[][] data) {
        for (int i = 0; i < data.length; i++) {
            for (int j = 0; j < data[i].length; j++) {
                if (data[i][j] == '0') {
                    try {
                        data[i][j] = '1';
                        if (f(data) == false) {
                            show(data);
                            // return
                        }
                        data[i][j] = '-';
                        if (f(data) == false) {
                            show(data);
                            // return;
                        }
                    } finally {
                        data[i][j] = '0';
                    }
                }
            }
        }
    }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        char[][] data = new char[3][];
        data[0] = scan.nextLine().trim().toCharArray();
        data[1] = scan.nextLine().trim().toCharArray();
        data[2] = scan.nextLine().trim().toCharArray();

        solve(data);
        // System.out.println(f(data));
    }
}
原文地址:https://www.cnblogs.com/jizhidexiaobai/p/8653079.html