西南交通大学算法分析与设计实验4.3

 


#include "iostream"
#include "algorithm"

using namespace std;

#define MAX 100

int map[MAX][MAX];
int n, position_x, position_y, ncount;

void Search(int up, int down, int left, int right, int special_x, int special_y) {
    if (down - up >= 3 || right - left >= 3) {
        int mid_height = (up + down) / 2;
        int mid_width = (left + right) / 2;
        if (mid_height >= special_x && mid_width >= special_y) {
            ncount++;
            map[mid_height + 1][mid_width] = ncount;
            map[mid_height + 1][mid_width + 1] = ncount;
            map[mid_height][mid_width + 1] = ncount;
            Search(up, mid_height, left, mid_width, special_x, special_y);
            Search(mid_height + 1, down, left, mid_width, mid_height + 1, mid_width);
            Search(up, mid_height, mid_width + 1, right, mid_height, mid_width + 1);
            Search(mid_height + 1, down, mid_width + 1, right, mid_height + 1, mid_width + 1);
        } else if (mid_height >= special_x && mid_width < special_y) {
            ncount++;
            map[mid_height][mid_width] = ncount;
            map[mid_height + 1][mid_width + 1] = ncount;
            map[mid_height + 1][mid_width] = ncount;
            Search(up, mid_height, mid_width + 1, right, special_x, special_y);
            Search(up, mid_height, left, mid_width, mid_height, mid_width);
            Search(mid_height + 1, down, left, mid_width, mid_height + 1, mid_width);
            Search(mid_height + 1, down, mid_width + 1, right, mid_height + 1, mid_width + 1);
        } else if (mid_height < special_x && mid_width >= special_y) {
            ncount++;
            map[mid_height][mid_width] = ncount;
            map[mid_height + 1][mid_width + 1] = ncount;
            map[mid_height][mid_width + 1] = ncount;
            Search(mid_height + 1, down, left, mid_width, special_x, special_y);
            Search(up, mid_height, mid_width + 1, right, mid_height, mid_width + 1);
            Search(up, mid_height, left, mid_width, mid_height, mid_width);
            Search(mid_height + 1, down, mid_width + 1, right, mid_height + 1, mid_width + 1);
        } else if (mid_height < special_x && mid_width < special_y) {
            ncount++;
            map[mid_height + 1][mid_width] = ncount;
            map[mid_height][mid_width] = ncount;
            map[mid_height][mid_width + 1] = ncount;
            Search(mid_height + 1, down, mid_width + 1, right, special_x, special_y);
            Search(up, mid_height, mid_width + 1, right, mid_height, mid_width + 1);
            Search(up, mid_height, left, mid_width, mid_width, mid_height);
            Search(mid_height + 1, down, left, mid_width, mid_height + 1, mid_width);
        }
    } else {
        ncount++;

        for (int i = up; i <= down; i++) {
            for (int j = left; j <= right; j++) {
                if (i == special_x && j == special_y) {
                    continue;
                } else {
                    map[i][j] = ncount;
                }
            }
        }
        return ;
    }
}

int main() {
    cin >> n >> position_x >> position_y;
    if (position_x < 0 || position_y < 0 || position_x >= n || position_y >= n || n % 2 != 0) {
        cout << "Informal Input" << endl;
        return 1;
    }
    Search(0, n - 1, 0, n - 1, position_x, position_y);
    if (!ncount) {
        cout << "Informal input or can't be solved!" << endl;
    } else {
        cout << ncount << " is needed!" << endl;
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            printf("%4d", map[i][j]);
        }
        cout << endl;
    }
    return 0;
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jellyfish Knight

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值