【BXOI R1.5】T3

· · 个人记录

在讲题之前,先带大家看一下坐标图:(来源于scratch)

此题看上去是一道稍稍难一些的 分治、递归、模拟 之类的题目。但是如果真的这样想就只能得到 10 pts。

当然在 狐狸的极力努力 我的思想斗争之下,我决定主要给你们讲如何分割和如何分封。

Part.1 暴力 10pts

Part.1.1:模拟

首先我们可以观察样例解释:

4 3 2 1
5 4 3 2
6 5 4 3
7 6 5 4

可以看到:其实分封是这样操作的:

那么我们就可以按照以下步骤完成一次分封:

(以左上点与右下点连城的对角线为界限,此对角线与其右上方称为子正方形的右上部分,此对角线的左下方称为子正方形的左下部分)。

1.分封

将坐标与边长作为实参传入到 分封(过程:work) 里。

Part.1.1.1 进行右上部分的分封:

可以看到,每一次的一斜列的赋值的每个点的坐标,都是由上一个 x 、y 的增加得来的。

核心代码如下:

void work_right_up (int x, int y, int s){
    //x、y 表示 斜边最上方的点 (后称顶点) 的坐标  s表示子正方形的边长
        //如下 : k 表示在以 (y, x + tem) 为顶点 的斜边所需赋的值
    for(int k = s, tem = 0; k >= 1 && tem <= s; k--, tem++)
        for(int i = x + tem, j = y; i < x + s && j < y + s; i++, j++)
            a[j][i] = k;
}

Part.1.1.2 进行左下部分的分封:

可以看到,其实上面的样例中,右上部分和左下部分是几乎对称的,以对角线为对称轴,每条斜边都是对称的,所以与上方的代码没有太大区别,但是要注意赋值时坐标的顺序。

void work_left_down (int x, int y, int s){
        //如下 : k 表示在以 (x + tem, y) 为顶点 的斜边所需赋的值
    for(int k = s + 1, tem = 0; k <= 2 * s - 1 && tem < s; k++, tem++)
        for(int i = x + tem + 1, j = y; i < x + s && j < y + s; i++, j++)
            a[i][j] = k;
}

对于分封的伪代码如下:

#include<bits/stdc++.h>
using namespace std;
int n, a[10][10];
void work_right_up (int x, int y, int s){
    for(int k = s, tem = 0; k >= 1 && tem <= s; k--, tem++)
        for(int i = x + tem, j = y; i < x + s && j < y + s; i++, j++)
            a[j][i] = k;
}
void work_left_down (int x, int y, int s){
    for(int k = s + 1, tem = 0; k <= 2 * s - 1 && tem < s; k++, tem++)
        for(int i = x + tem + 1, j = y; i < x + s && j < y + s; i++, j++)
            a[i][j] = k;

}
int main(){
    cin >> n;
    memset(a, 0, sizeof(a));
    work_right_up(1, 1, n);
    work_left_down(1, 1, n);
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= n; j++)
            cout << a[i][j] << ' ';
        cout << endl;
    }
    return 0;
}

运行结果:(所以我们分封(模拟)的步骤就完成了)

Part.1.2 DP(或许不是严谨的DP)

观察样例,我们可以看到,其实第一行都是从 s 到 1。将每列看为一个数列,可以发现这个数列是以 1 为差单调递增。所以我们就有了以下代码:

void DP(int x, int y, int s){
    for(int i = y, k = s; i <= y + s - 1; i++, k--)
        a[x][i] = k;
    for(int i = x + 1; i <= x + s - 1; i++)
        for(int j = y; j <= y + s - 1; j++)
            a[i][j] = a[i - 1][j] + 1;
}

对于分封的伪代码如下:

#include<bits/stdc++.h>
using namespace std;
int n, s, x, y, to, a[260][260];
void DP(int x, int y, int s){
    for(int i = y, k = s; i <= y + s - 1; i++, k--)
        a[x][i] = k;
    for(int i = x + 1; i <= x + s - 1; i++)
        for(int j = y; j <= y + s - 1; j++)
            a[i][j] = a[i - 1][j] + 1;
}
int main(){
    cin >> n >> x >> y >> to;
    s = pow(2, n);
    DP(x, y, to);
    for(int i = 1; i <= s; i++){
        for(int j = 1; j <= s; j++)
            cout << a[i][j] << ' ';
        cout << endl;
    }
    return 0;
}

运行结果:(所以我们分封(DP)的步骤就完成了)

Part.1.3 分割

这个步骤不管你选择上方的 模拟 还是 DP, 你最后都要经历这一步。

如何分割:我们可以运用递归或递推的思想,从边长为 2 开始分封,当结束这一步骤后,将边长 \times 2 即可。

分割代码如下:

递推:

#include<bits/stdc++.h>
using namespace std;
int n, s, a[110][110];
void deco(int x, int y, int ts){
    cout << y << ' ' << x << endl;
    if(ts == s)
        exit(0);
    if(x == s - ts + 1 && y == s - ts + 1)
        deco(1, 1, ts * 2);
    else if(x == s - ts + 1)
        deco(1, y + ts, ts);
    deco(x + ts, y, ts);
}
int main(){
    cin >> n;
    s = pow(2, n);
    deco(1, 1, 2);
    return 0;
}

运行结果:

递归:

#include<bits/stdc++.h>
using namespace std;
int n, s, a[110][110];
void deco(int x, int y, int ts){
    cout << y << ' ' << x << endl;
    if(ts == 2 && x == s - ts + 1 && y == s - ts + 1)
        exit(0);
    if(x == s - ts + 1 && y == s - ts + 1 || ts == s)
        deco(1, 1, ts / 2);
    else if(x == s - ts + 1)
        deco(1, y + ts, ts);
    deco(x + ts, y, ts);
}
int main(){
    cin >> n;
    s = pow(2, n);
    deco(1, 1, s);
    return 0;
}

运行结果:

那么将上面的代码结合起来,暴力的代码就ok啦!

Part.2 数学/找规律 100pts

其实在上面的解析中,我们已经发现了这种操作的性质。(依然以边长为 4 的正方形为例)

| -----------: | -----------: | -----------: | -----------: | | $5$ | $4$ | $3$ | $2$ | | $6$ | $5$ | $4$ |$3$ | | $7$ | $6$ | $5$ | $4$| 可以看到,如果我们将第一行的 $x$ 坐标与第一行的数值对应起来,会得到以下表格: | $x$ 坐标 | 1 | 2 | 3 | 4 | | -----------: | -----------: | -----------: | -----------: | -----------: | | 数值 | 4 | 3 | 2 | 1 | 那么我们就会发现,其实当 $x$ 坐标为 $j$ 时,对应的数值为 $4-j+1$。 ------------ 而纵着的是比自己上面的一项 $+1$。如果我们将任意一列 $y$ 坐标的数值与其列第一个数的差进行对应,可以得到一下表格: | $y$ 坐标 | 1 | 2 | 3 | 4 | | -----------: | -----------: | -----------: | -----------: | -----------: | | 两者的差 | 0 | 1 | 2 | 3 | 那么我们就会发现,其实当 $y$ 坐标为 $i$ 时,设顶点数为 $d$,对应的数值为 $d+i - 1$。 ------------ 于是我们就可以快快乐乐的列公式啦!(设 $s$ 为子正方形的边长) $$a_{i,j} = s - j + 1 + i - 1$$ $$=s-j+i$$ 那么如果不是 1、2、3、4 这几个特定的数值呢? 那么我们可以运用 模运算 ! $$a_{i,j} = s - (j \mod s) + (i \mod s)$$ 当然我们还要特判 $s$ 可以被除尽的情况: 当可以除尽时,结果为边长,否则是膜后的结果。 公式如下: ```a_i,j = s - (j % s ? j % s : s) + (i % s ? i % s : s)``` 核心代码如下: ```cpp int work(int i, int j){ return s - (j % s ? j % s : s) + (i % s ? i % s : s); } ``` 完整代码如下: ```cpp #include<bits/stdc++.h> using namespace std; int n, s; int work(int i, int j){ return s - (j % s ? j % s : s) + (i % s ? i % s : s); } int main(){ cin >> n; s = pow(2, n); for(int i = 1; i <= s; i++){ for(int j = 1; j <= s; j++) cout << work(i, j) << ' '; cout << endl; } return 0; } ``` 运行结果: ![](https://cdn.luogu.com.cn/upload/image_hosting/oyi1q7ks.png) ------------ ## 收尾 又因为没个点都会分封多次,而没次分封点所在的子正方形的边长都不同。所以我们需要这样: ```cpp int work(int i, int j){ int ans = 0; for(int S = s; S >= 1; S /= 2) ans += S - (j % S ? j % S : S) + (i % S ? i % S : S); return ans; } ``` 完整代码如下: ```cpp #include<bits/stdc++.h> using namespace std; int n, s; int work(int i, int j){ int ans = 0; for(int S = s; S >= 1; S /= 2) ans += S - (j % S ? j % S : S) + (i % S ? i % S : S); return ans; } int main(){ cin >> n; s = pow(2, n); for(int i = 1; i <= s; i++){ for(int j = 1; j <= s; j++) cout << work(i, j) << ' '; cout << endl; } return 0; } ``` 运行结果: ![](https://cdn.luogu.com.cn/upload/image_hosting/7rfpb2ne.png) ------------ 大功告成!! ~~Birdly写这个题解写了一整天~~