递归分治:L 型覆盖问题

递归分治:L 型覆盖问题

L 型覆盖问题

要在一个 2^n × 2^n​ 的棋盘中,放置一块 1×1​ 的指挥营(置为 0),其余区域用 L 形的 3 格军营(如下四种形状)填满:
11 11 1 1
1 1 11 11

且要保证军营编号按照行先列后的顺序进行排序

L 形覆盖法的核心思想是:

  1. 将当前区域分为四个象限;
  2. 找出包含指挥营的象限;
  3. 在其他三个象限的中心交点放置一个 L 形军营,这样这三个象限就都拥有了一个“假指挥营”;
  4. 对四个子区域递归处理。

解法:

  • 终止条件:当前区域是 2×2​,那么直接用一个 L 形覆盖 3 格,跳过指挥营的位置。
  • 每次将区域分成四个 2^(n-1) × 2^(n-1)​ 的子区块,判断指挥营在哪一个区块中,然后在中心三格放一个 L 形军营,使其它三块看起来也“有一个缺口”。
  • 使用递增的军营编号来标识不同 L 形军营。
  • 最后再使用 Map 来进行映射,实现排序
public class Main {
    static int id = 1;
    static int[][] board;

    public static void main(String[] args) throws Exception {
        int n = Reader.nextInt();
        int x = Reader.nextInt() - 1;
        int y = Reader.nextInt() - 1;
        int size = 1 << n;

        board = new int[size][size];
        tromino(0, 0, x, y, size);

		// 转换编号
        HashMap<Integer, String> map = new HashMap<>();
        int idx = 1;
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size; j++) {
                if (board[i][j] == 0) {
                    map.put(0, "0");
                } else if (!map.containsKey(board[i][j])) {
                    map.put(board[i][j], String.valueOf(idx++));
                }
                Writer.write(String.valueOf(map.get(board[i][j])));
                Writer.write(" ");
            }
            Writer.write("\n");
        }
        Writer.flush();
        Writer.close();
    }

    private static void tromino(int sx, int sy, int px, int py, int size) {
        if (size == 2) {
            for (int dx = 0; dx < 2; dx++) {
                for (int dy = 0; dy < 2; dy++) {
                    int nx = sx + dx, ny = sy + dy;
                    if (nx != px || ny != py) {
                        board[nx][ny] = id;
                    }
                }
            }
            id++;
            return;
        }

        int mid = size / 2;
        int cx = sx + mid;
        int cy = sy + mid;

        // 找缺口
        int[][] centers = {
                {cx - 1, cy - 1}, // top-left
                {cx - 1, cy},     // top-right
                {cx, cy - 1},     // bottom-left
                {cx, cy}          // bottom-right
        };

        // 确定指挥营
        int quad = (px < cx ? 0 : 2) + (py < cy ? 0 : 1);

        // 在中心放置 L 形军营,三个假缺口
        for (int i = 0; i < 4; i++) {
            if (i == quad) continue;
            int tx = centers[i][0];
            int ty = centers[i][1];
            board[tx][ty] = id;
        }
        id++;

        // 递归处理四个象限
        tromino(sx, sy, quad == 0 ? px : cx - 1, quad == 0 ? py : cy - 1, mid);         // top-left
        tromino(sx, cy, quad == 1 ? px : cx - 1, quad == 1 ? py : cy, mid);             // top-right
        tromino(cx, sy, quad == 2 ? px : cx, quad == 2 ? py : cy - 1, mid);             // bottom-left
        tromino(cx, cy, quad == 3 ? px : cx, quad == 3 ? py : cy, mid);                 // bottom-right
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值