螺旋矩阵、旋转矩阵、矩阵Z字打印

螺旋矩阵

cpp 复制代码
#include <iostream>
#include <vector>
void display(std::vector<std::vector<int>>&nums){
       for(int i = 0; i < nums.size(); ++i){
             for(int j = 0; j < nums[0].size(); ++j){
                   std::cout<<nums[i][j]<<' ';
             }
             std::cout<<std::endl;
       }
}
void Process(int x1, int y1, int x2, int y2, std::vector<std::vector<int>>&nums){
      if(x1 == x2){
            while (y1 <= y2)
            {
                  std::cout<<nums[x1][y1++]<<' ';
            }    
      } else if(y1 == y2){
            while (x1 <= x2)
            {
                  std::cout<<nums[x1++][y1]<<' ';
            }    
      }else{
            for(int i = y1; i <= y2; ++i){
                  std::cout<<nums[x1][i]<<' ';
            }
            for(int i = x1 + 1; i <= x2; ++i){
                  std::cout<<nums[i][y2]<<' ';
            }
            for(int i = y2 - 1; i >= y1; --i){
                  std::cout<<nums[x2][i]<<' ';
            }
            for(int i = x2 - 1; i > x1; --i){
                   std::cout<<nums[i][y1]<<' ';
            }
      }
}
int main(){
    std::vector<std::vector<int>>nums;
    int n = 6;
    int m = 6;
    nums.resize(n);
    int num = 1;
    for(int i = 0; i < n;  ++i){
        std::vector<int>tmp(m, 0);
        for(int j = 0; j < m; ++j){
             tmp[j] = num++;
        }
        nums[i] = tmp;
    }
    display(nums);
    // 矩阵类型的题目可以枚举一些特殊的点来做左上角点和右下角点, 然后在往内部收缩
    int x1 = 0, y1 = 0, x2 = n-1, y2 = m-1;
    while (x1 <= x2)
    {
        Process(x1, y1, x2, y2, nums);
        ++x1;
        ++y1;
        --x2;
        --y2;
    }
    
    std::getchar();
 

}

旋转矩阵

类似于这个螺旋矩阵我们也是在每次处理最外层的矩形,然后往内收缩。

对于一个矩形我们选取四个点依次进行交换即可

cpp 复制代码
#include <iostream>
#include <vector>
void display(std::vector<std::vector<int>>&nums){
       for(int i = 0; i < nums.size(); ++i){
             for(int j = 0; j < nums[0].size(); ++j){
                   std::cout<<nums[i][j]<<' ';
             }
             std::cout<<std::endl;
       }
}
void Process(int x1, int y1, int x2, int y2, std::vector<std::vector<int>>&nums){
      if(x1 == x2){
            while (y1 <= y2)
            {
                  std::cout<<nums[x1][y1++]<<' ';
            }    
      } else if(y1 == y2){
            while (x1 <= x2)
            {
                  std::cout<<nums[x1++][y1]<<' ';
            }    
      }else{
            for(int i = y1; i <= y2; ++i){
                  std::cout<<nums[x1][i]<<' ';
            }
            for(int i = x1 + 1; i <= x2; ++i){
                  std::cout<<nums[i][y2]<<' ';
            }
            for(int i = y2 - 1; i >= y1; --i){
                  std::cout<<nums[x2][i]<<' ';
            }
            for(int i = x2 - 1; i > x1; --i){
                   std::cout<<nums[i][y1]<<' ';
            }
      }
}
int main(){
    std::vector<std::vector<int>>nums;
    int n = 4;
    int m = 4;
    nums.resize(n);
    int num = 1;
    for(int i = 0; i < n;  ++i){
        std::vector<int>tmp(m, 0);
        for(int j = 0; j < m; ++j){
             tmp[j] = num++;
        }
        nums[i] = tmp;
    }
    display(nums);
    // 枚举矩阵的四个点把最外圈的给调整好再往内圈收缩
    int x1, y1, x2, y2, x3, y3, x4, y4;
    int count = 0;

    while(count < n -2){
          x1 = count, y1 = count;
          x2 = count, y2 = n - 1 - count;
          x3 = n - 1 - count, y3 = count;
          x4 = n - 1 - count, y4 = n - 1 - count;
          while(y1 < y2 && x2 < x4 && y4 > y3 && x3 > x1){
                std::swap(nums[x1][y1], nums[x2][y2]);
                std::swap(nums[x1][y1], nums[x4][y4]);
                std::swap(nums[x1][y1], nums[x3][y3]);
                ++y1;
                ++x2;
                --y4;
                --x3;
                
          }
          ++count;
    }
    display(nums);
   
    
    std::getchar();
 

}

矩阵Z字打印

也是和螺旋矩阵类似选取两个点进行循环

cpp 复制代码
#include <iostream>
#include <vector>
void display(int x1, int y1, int x2, int y2, std::vector<std::string>&strs){
      if(x1 < x2){
           while ( x1 <= x2 )
           {
                std::cout<<strs[x1++][y1--]<<' ';
           }
           std::cout<<std::endl;      
      } else{
           while ( x1 >= x2  )
           {
                std::cout<<strs[x1--][y1++]<<' ';
           }
           std::cout<<std::endl;          
      }
}
int main(){
     std::vector<std::string>strs = {"abcd", "efgh", "jklh"};
     int n = strs.size();
     int m = strs[0].size();
     int x1 = 0, y1 = 0;
     int x2 = 0, y2 = 0;
     int num = 0;
     while(x1 < n){
          if(num % 2 == 0){
               display(x1, y1, x2, y2, strs);
          } else{
               display(x2, y2, x1, y1, strs);
          }
          if(y1 < m - 1){
              y1++;
          }else{
              x1++;
          }
          if(x2 < n - 1){
              x2++;
          }else{
              y2++;
          }
          num++;
     } 
     std::getchar();


}

矩阵打印的题目可以从选取若干个点然后处理一层之后再往内进行收缩的做法先进行考虑

相关推荐
naruto_lnq15 小时前
分布式系统安全通信
开发语言·c++·算法
Jasmine_llq15 小时前
《P3157 [CQOI2011] 动态逆序对》
算法·cdq 分治·动态问题静态化+双向偏序统计·树状数组(高效统计元素大小关系·排序算法(预处理偏序和时间戳)·前缀和(合并单个贡献为总逆序对·动态问题静态化
爱吃rabbit的mq16 小时前
第09章:随机森林:集成学习的威力
算法·随机森林·集成学习
(❁´◡`❁)Jimmy(❁´◡`❁)17 小时前
Exgcd 学习笔记
笔记·学习·算法
YYuCChi17 小时前
代码随想录算法训练营第三十七天 | 52.携带研究材料(卡码网)、518.零钱兑换||、377.组合总和IV、57.爬楼梯(卡码网)
算法·动态规划
CSDN_RTKLIB17 小时前
【四个场景测试】源文件编码UTF-8 BOM
c++
不能隔夜的咖喱17 小时前
牛客网刷题(2)
java·开发语言·算法
VT.馒头17 小时前
【力扣】2721. 并行执行异步函数
前端·javascript·算法·leetcode·typescript
进击的小头18 小时前
实战案例:51单片机低功耗场景下的简易滤波实现
c语言·单片机·算法·51单片机
肉包_51118 小时前
两个数据库互锁,用全局变量互锁会偶发软件卡死
开发语言·数据库·c++