Luogu C++ Language | P5461 Pardon Prisoners of War

Learn C++ from a baby! Record the questions in the process of Luogu C++ learning and test preparation, and record every moment.

Attached is a summary post: Luogu Brush Questions C++ Language | Summary


[Title description]

There are currently 2^ n ×2^ n ( n ≤10) cheaters standing in a square array waiting for kkksc03 to be punished. kkksc03 decided to pardon some cheaters. He divided the square matrix into four smaller square matrices, each of which had half the side length of the original matrix. All cheaters in the matrix in the upper left corner will be pardoned, and each of the remaining 3 small matrices will be divided into 4 smaller matrices, and then the cheaters will be pardoned in the same way... until the matrix can no longer Until divided. All cheaters who are not pardoned will be punished with a brown name.

Given  n , please output the fate of each cheater, where 0 represents pardoned and 1 represents not pardoned.

【enter】

an integer  n .

【Output】

 01 matrix of 2^ n × 2^ n , representing whether each person is pardoned. There is a space between the numbers.

【Input sample】

3

【Output sample】

0 0 0 0 0 0 0 1
0 0 0 0 0 0 1 1
0 0 0 0 0 1 0 1
0 0 0 0 1 1 1 1
0 0 0 1 0 0 0 1
0 0 1 1 0 0 1 1
0 1 0 1 0 1 0 1
1 1 1 1 1 1 1 1

[Detailed code explanation]

#include <bits/stdc++.h>
using namespace std;
int a[1100][1100];
int fzdg(int x, int y, int z)
{
    if (z==1) return 0;
    for (int i=x; i<x+z/2; i++) {
        for (int j=y; j<y+z/2; j++) {
            a[i][j] = 1;
        }
    }
    fzdg(x+z/2, y, z/2);
    fzdg(x, y+z/2, z/2);
    fzdg(x+z/2, y+z/2, z/2);
}
int main()
{
    int n, fz;
    cin >> n;
    fz = pow(2, n);
    fzdg(1, 1, fz);
    for (int i=1; i<=fz; i++) {
        for (int j=1; j<=fz; j++) {
            if (a[i][j]==1) cout << "0 ";
            else cout << "1 ";
        }
        cout << endl;
    }
    return 0;
}

【operation result】

3
0 0 0 0 0 0 0 1
0 0 0 0 0 0 1 1
0 0 0 0 0 1 0 1
0 0 0 0 1 1 1 1
0 0 0 1 0 0 0 1
0 0 1 1 0 0 1 1
0 1 0 1 0 1 0 1
1 1 1 1 1 1 1 1

Guess you like

Origin blog.csdn.net/guolianggsta/article/details/132801643