提高棋盘模式的时间复杂度

Improving time complexity of chessboard pattern

本文关键字:时间复杂度 模式 高棋盘      更新时间:2023-10-16

我已经编写了一个程序来打印棋盘图案。它是这样的:(注释解释逻辑和变量(

#include<iostream>
#include<vector>
#include<string>
#include<fstream>
#include<cstdlib>
using namespace std;
int block_size = 8; //block_size * block_size is the size of each block on the board
int dim = 8; //8 blocks on every row or column, each block containing block_size * block_size pixels
int res = dim * block_size; //total number of pixels is res * res (resolution)
int main(){
    int size = 8;
    vector<vector<int> > vec(res);  
    for (int i = 0; i < res; i++) { 
        vec[i] = vector<int>(res); 
        for (int j = 0; j < res; j++) {
            vec[i][j] = 0; 
        }
    }//initialize all pixels to 0
    //int count=0;
    /*
    allocate black or white pixels based on odd/even status of array indices which are picked
    based on multiples of block_size
    ex. i,j = 0,4,8,16...
    pixels are allocated from the starting point of a particular coordinate like so: two for loops for i,j + d 
    where 0<=d<block_size
    */
    for (int i = 0; i < res; i=i+block_size) { 
        for (int j = 0; j < res; j=j+block_size) {
            //cout<<count<<" ";
            //count++;
            //cout<<i/block_size;
            if (int ((i/block_size)%2 == 0)){
                if(int ((j/block_size)%2 == 0)){
                    for(int k=i;k<i+block_size;k++){
                        for (int l=j;l<j+block_size;l++){
                            vec[k][l]=0;
                        }
                    }
                }
                else{
                    for(int k=i;k<i+block_size;k++){
                        for (int l=j;l<j+block_size;l++){
                            vec[k][l]=255;
                        }
                    }
                }
                }
                else{
                    if(int ((j/block_size)%2 == 0)){
                    for(int k=i;k<i+block_size;k++){
                        for (int l=j;l<j+block_size;l++){
                            vec[k][l]=255;
                        }
                    }
                }
                else{
                    for(int k=i;k<i+block_size;k++){
                        for (int l=j;l<j+block_size;l++){
                            vec[k][l]=0;
                        }
                    }
                }
                }
            }
        }
    cout<<endl;
    /*
    for (int i = 0; i < size; i++) { 
        for (int j = 0; j < vec[i].size(); j++) 
            cout << vec[i][j] << " "; 
        cout << endl; 
    }
    */
    string filename = "chessboard.pgm";
    ofstream pgmFile(filename);

    pgmFile << "P2" << endl;
    pgmFile << res << " " << res << endl;
    pgmFile << 255 << endl;

    for(int i=0;i<res;i++){
        for(int j=0;j<res;j++){
            pgmFile << vec[i][j] << " ";
        }
        pgmFile << endl;
    }
    pgmFile.close();
    return 0;
}

程序的输出被输入到pgm图像中,然后将其写入文件进行查看(Irfanview可用于查看pgm图像(。
算法如下:
--根据选取
的数组索引的奇数/偶数状态分配黑色或白色像素 基于block_size
的倍数 例如 i,j = 0,4,8,16...
--像素从特定坐标的起点分配:2 用于 i,j + d 的循环,其中 d 的范围从 0 到 block_size,不包括block_size
现在,看起来复杂度是O(n^4(。关于我可以采取哪些步骤来降低复杂性的任何想法?

您的时间复杂度已经是最佳的。当然,您正在对输入进行几次传递,但是该常量被忽略,复杂性归结为图像中的像素数(或O(side_length2 *block_size 2(或O(res2(或仅O(n((如果n是图像大小(。

话虽如此,有很多重复的代码,你可以完全消除向量,这使得你的空间复杂性恒定。

这是重写,只保留要点:

#include <cstdlib>
#include <fstream>
int main() {
    int block_size = 8;
    int dim = 8;
    int res = dim * block_size;
    std::ofstream pgm("chessboard.pgm");
    pgm << "P2n" << res << " " << res << "n255n";
    for (int i = 0; i < res; i++) {
        for (int j = 0; j < res; j++) {
            pgm << ((j / block_size + i / block_size) % 2 ? 255 : 0) << " ";
        }
        pgm << "n";
    }
    pgm.close();
    return 0;
}

最后:国际象棋的左下角通常有一个黑色方块,因此您可以考虑反转颜色。

棋盘有一个很好的图案。它交替偶数行(从黑色正方形开始(和奇数行(从白色开始(。这表明了一个自然的程序结构:

    for (row_pair = 0; row_pair < dim / 2; row_pair++) {
        emit_row(something_even);
        emit_row(something_odd);
    }

反过来,每行由block相同的细线(一个像素高(组成。为偶数行和奇数行做好准备;就两个。

    line_t even_line = prepare_even_line(block_size);
    line_t odd_line = prepare_odd_line(block_size);

并按

    void emit_row(line_t& line) {
        for (int i = 0; i < block_size; i++) {
            emit_line(line);
        }
    }

现在你可以

    for (row_pair = 0; row_pair < dim / 2; row_pair++) {
        emit_row(even_line);
        emit_row(odd_line);
    }

剩下的唯一事情就是弄清楚line_t应该是什么,以及如何准备一条细线。 emit是不言而喻的。

您可以使用这样的东西(这是控制台中董事会归档的完整代码(。

每个像素都会像在实现中一样访问一次,复杂性也O(res^2),但看起来更简单。

对于等于 2 的幂的 block_size -s,可以通过按位运算计算rxry

int main()
{
    int block_size = 4; //block_size * block_size is the size of each block on the board
    int dim = 4; //8 blocks on every row or column, each block containing block_size * block_size pixels
    int res = dim * block_size; //total number of pixels is res * res (resolution)
    vector<vector<int> > vec(res);
    for (int i = 0; i < res; i++) {
        vec[i] = vector<int>(res);
        }
    for (int y = 0; y < res; y++) {
        int ry = ((y % (block_size * 2)) < block_size) ? 0 : 1;
        for (int x = 0; x < res; x++) {
            int rx = ((x % (block_size * 2)) < block_size) ? 0 : 1;
            vec[y][x] = 255 * (ry ^ rx);
            cout << vec[y][x] << "t";
        }
        cout << "n";
    }
}

Jarod42提出的更简单的方法:

for (int y = 0; y < res; y++) {
    for (int x = 0; x < res; x++) {
        vec[y][x] = ((y / block_size) + (x / block_size)) % 2 == 0 ? 0 : 255;

4x4 的结果:

0       0       0       0       255     255     255     255     0       0       0       0       255     255     255     255
0       0       0       0       255     255     255     255     0       0       0       0       255     255     255     255
0       0       0       0       255     255     255     255     0       0       0       0       255     255     255     255
0       0       0       0       255     255     255     255     0       0       0       0       255     255     255     255
255     255     255     255     0       0       0       0       255     255     255     255     0       0       0       0
255     255     255     255     0       0       0       0       255     255     255     255     0       0       0       0
255     255     255     255     0       0       0       0       255     255     255     255     0       0       0       0
255     255     255     255     0       0       0       0       255     255     255     255     0       0       0       0
0       0       0       0       255     255     255     255     0       0       0       0       255     255     255     255
0       0       0       0       255     255     255     255     0       0       0       0       255     255     255     255
0       0       0       0       255     255     255     255     0       0       0       0       255     255     255     255
0       0       0       0       255     255     255     255     0       0       0       0       255     255     255     255
255     255     255     255     0       0       0       0       255     255     255     255     0       0       0       0
255     255     255     255     0       0       0       0       255     255     255     255     0       0       0       0
255     255     255     255     0       0       0       0       255     255     255     255     0       0       0       0
255     255     255     255     0       0       0       0       255     255     255     255     0       0       0       0