提高棋盘模式的时间复杂度
Improving time complexity of chessboard pattern
我已经编写了一个程序来打印棋盘图案。它是这样的:(注释解释逻辑和变量(
#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,可以通过按位运算计算rx
和ry
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
- 向量 <int> a {N, 0} 和 int arr a[N] = {0} 的时间复杂度有什么区别
- while循环中while循环的时间复杂度是多少
- 我可以创建一个包含两个变量的 for 循环,但时间复杂度仍然为 O(n) 吗?
- 函数的时间复杂度是多少?
- 如何检查两个 std::向量在小于 O(n) 的时间复杂度内是否相等
- 时间复杂度 当具有复合数据类型(如元组或对)时?
- 如何计算此排序函数的时间复杂度?
- 计算两个代码块的时间复杂度
- C++ 中具有 O(1) 搜索时间复杂度的数据结构
- 具有嵌套 if-else 的循环的时间复杂度
- 将树节点添加到向量向量中的 n 元树遍历的平均和最坏情况时间复杂度是多少?
- 如何计算函数的时间复杂度?
- 求解包含"variables"的 T(n) 时间复杂度
- 查找数字是否为 2 的幂的时间复杂度
- C++ - 最坏情况和平均情况插入时间复杂度在 std::unordered_map <int,int>?
- 为什么一种算法在相同的时间复杂度下比另一种算法更快?
- 关于记忆后这种递归关系的时间复杂度
- 2 个嵌套循环的时间复杂度
- 给定C++代码的时间复杂度是多少?
- 提高棋盘模式的时间复杂度