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

标签 c++ algorithm optimization time-complexity

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

#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个for循环i,j + d,其中d的范围从0到block_size,不包括block_size
现在看来,复杂度是 O(n^4)。关于我可以采取哪些步骤来降低复杂性有什么想法吗?

最佳答案

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

话虽如此,有很多重复的代码,你可以完全消除 vector ,这使得你的空间复杂度保持不变。

这是重写,仅保留要点:

#include <cstdlib>
#include <fstream>

int main() {
    int block_size = 8;
    int dim = 8;
    int res = dim * block_size;
    std::ofstream pgm("chessboard.pgm");
    pgm << "P2\n" << res << " " << res << "\n255\n";

    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;
}

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

关于c++ - 提高棋盘模式的时间复杂度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56624250/

相关文章:

algorithm - 查找给定一组 N 个间隔是否覆盖了整个范围

java - 二值图像中的轮廓和周长识别

java - 最小化 Spring Boot 启动时间

c++ - 清除 boost::interprocess::map 的更快方法?

performance - 如何在 Haskell 中优化数值库的速度

c++ - 为什么 View 和表的 sqlite 列有不同的名称?

C++ 二进制文件 io

c++ - 非虚拟成员函数可以使用模板参数吗?

c++ - 使用删除的复制构造函数初始化 const 引用成员

c# - 是否有任何算法可以根据某些模式对数组进行分类?