首页 > 其他分享 >二维矩阵螺旋遍历

二维矩阵螺旋遍历

时间:2024-03-23 12:30:39浏览次数:21  
标签:遍历 false int 矩阵 else ++ 二维 && true

//3.螺旋矩阵
//例如 n=4
//要求:打印出螺旋矩阵,求 i 行 j 列的数字,0<n<10000
//算法思想:只要找出每一层的第一个数即可,第一个数值为上一层的第一个数+4*n+4,循
//环时 n 每次减 2
//+--------------------------> X 轴
//| 1  2   3  4
// 12  13  14 5
//| 11 16 15  6
//| 10 9   8  7
#include <iostream>
using namespace std;
void input(int A[100][100],int n,int x,int y)
{
    int a = 0, b = 0, c = 0, d = 0;
    bool right = true, left = false, up = false, donw = false;
    for (int i = 0; i < n; i++) {
        scanf("%d", &A[a][b]);
        if (b == (y - 1) && right == true) {
            right = false;
            donw = true;
        } else if (a == (x - 1) && b == (y - 1) && donw == true) {
            left = true;
            donw = false;
        } else if (b == d && a == (x - 1) && left == true) {
            left = false;
            up = true;
            c++;
        } else if (c == a && b == d && up == true) {
            up = false;
            right = true;
            d++;
            x--;
            y--;
        }
        if (right == true)
            b++;
        else if (left == true)
            b--;
        else if (up == true)
            a--;
        else if (donw == true)
            a++;
    }
}//非递归调用螺旋输入
void print(int A[100][100],int n,int x,int y)
{
    int a = 0, b = 0, c = 0, d = 0;
    bool right = true, left = false, up = false, donw = false;
for (int i = 0; i < n; i++) {
    printf("%3d",A[a][b]);
if (b == (y - 1) && right == true) {
right = false;
donw = true;
} else if (a == (x - 1) && b == (y - 1) && donw == true) {
left = true;
donw = false;
} else if (b == d && a == (x - 1) && left == true) {
left = false;
up = true;
c++;
} else if (c == a && b == d && up == true) {
up = false;
right = true;
d++;
x--;
y--;
}
if (right == true)
b++;
else if (left == true)
b--;
else if (up == true)
a--;
else if (donw == true)
a++;
}
}//非递归调用螺旋打印
void Found(int A[100][100],int a,int b,int start,int x,int y) {
    if (x <= 0||y<=0)
        return;
    if (x*y == 1) {
        A[a][b] = start;
        return;
    }
    for (int i = b; i < b + y - 1; i++)//上部
        A[a][i] = start++;
    for (int j = a; j < a + x - 1; j++)//右边
        A[j][b + y - 1] = start++;
    for (int i = b + y - 1; i > b; i--)//底部
        A[a + x - 1][i] = start++;
    for (int j = a + x - 1; j > a; j--)//左边
        A[j][b] = start++;
    Found(A, a + 1, b + 1, start, x - 2,y-2);
}//递归调用螺旋输入

void Print(int A[100][100],int a,int b,int start,int x,int y) {
    if (x*y <= 0)
        return;
    if (x*y == 1) {
        printf("%3d",A[a][b]);
        return;
    }
    for (int i = b; i < b + y - 1; i++)//上部
        printf("%3d",A[a][i]);
    for (int j = a; j < x + a - 1; j++)//右边
         printf("%3d",A[j][y + b - 1]);
    for (int i = y + b - 1; i > b; i--)//底部
         printf("%3d",A[x + a - 1][i]);
    for (int j = x + a - 1; j > a; j--)//左边
         printf("%3d",A[j][b] );
    Print(A, a + 1, b + 1, start, x - 2,y-2);
}//递归调用螺旋打印
int main() {
    int A[100][100] = {0};
    int x, y, n;
    scanf("%d%d", &x,&y);
    n = x * y;
    input(A,n,x,y);//非递归螺旋矩阵输入
    print(A,n,x,y);//非递归螺旋矩阵螺旋打印

//    Found(A,0,0,1,x,y);//递归螺旋矩阵输入
//    Print(A,0,0,1,x,y);//递归螺旋矩阵打印
    printf("\n\n");
    for (int i = 0; i < x; i++) {
        for (int j = 0; j < y; j++)
            printf("%5d",A[i][j]);//按行和列正常打印
        cout << endl;
    }
}

标签:遍历,false,int,矩阵,else,++,二维,&&,true
From: https://blog.csdn.net/2401_83795651/article/details/136938676

相关文章

  • (14)求两个矩阵之和、之积
    #define_CRT_SECURE_NO_WARNINGS#include<stdio.h>#defineM2#defineN2intmain(){ inta[M][N]={0},b[M][N]={0};//定义两个矩阵a和b intsum[M][N]={0};//矩阵sum保存矩阵a和b的和 intproduct[M][N]={0};//矩阵p......
  • 二叉树的创建,遍历与销毁
    二叉树的创建,遍历与销毁#include<iostream>#include<bits/stdc++.h>usingnamespacestd;structTreeNode{ charval;//数据域 TreeNode*lchild;//左子树 TreeNode*rchild;//右子树};classBiTree{ private: TreeNode*root;//根节点 public: BiTree()......
  • uniapp根据链接生成二维码
    1.我们在根目录common中新建一个js文件2.然后再这个js文件当中添加以下这些代码//uqrcode.js//---------------------------------------------------------------------//githubhttps://github.com/Sansnn/uQRCode//----------------------------------------------......
  • LeetCode.59. 螺旋矩阵 II
    题目描述: 给你一个正整数 n ,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的 nxn 正方形矩阵 matrix 。示例1:输入:n=3输出:[[1,2,3],[8,9,4],[7,6,5]]示例2:输入:n=1输出:[[1]]提示:1<=n<=20代码(Java): classSolution{publ......
  • 微信公众号开发 - 扫描带参数二维码事件支持EventKey字符串传参
    $access_token=$this->access_token();//获取access_token$json_url='https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token='.$access_token;$scene_id="A123B";$curl_data='{"action_name&......
  • LeetCodeHot100 二分查找 35. 搜索插入位置 74. 搜索二维矩阵 34. 在排序数组中查
    35.搜索插入位置https://leetcode.cn/problems/search-insert-position/description/?envType=study-plan-v2&envId=top-100-likedpublicintsearchInsert(int[]nums,inttarget){intleft=0;intright=nums.length-1;while(left<......
  • 邻接矩阵详解
    邻接矩阵是图论中用于表示图(Graph)结构的一种重要数据结构,特别适用于表示顶点之间连接关系的图形。在计算机科学和数学领域,它被广泛应用来编码无向图和有向图的信息。对于一个具有n个顶点的图G=(V,E),邻接矩阵是一个n×n的矩阵A,其中的行和列分别对应着图中的每个顶点。矩......
  • (41/60)0-1背包(二维数组、一维数组)、分割等和子集
    有点抽象0-1背包卡码网:携带研究材料(第六期模拟笔试)动态规划思路二维:意义:0~i物品内,放进容量为j的背包,最大价值为dp[i][j]递推:dp[i][j]=max(dp[i-1][j-weight[i],dp[i-1][j])初始化:第一列为0,第一行j>=weight[0]时赋值为value[0]遍历:先背包再物品/先物品再背包均可(......
  • 矩阵快速幂
    debug:重载乘号的时候要把两个传进来的矩阵用起来//Problem:P3390【模板】矩阵快速幂//Contest:Luogu//URL:https://www.luogu.com.cn/problem/P3390//MemoryLimit:256MB//TimeLimit:1000ms////PoweredbyCPEditor(https://cpeditor.org)#include<b......
  • 矩阵乘法
    intn,m;intk;structmatrix{ intc[101][101]; matrix(){memset(c,0,sizeofc);} };matrixoperator*(matrix&a,matrix&b){ matrixt; for(inti=1;i<=n;i++){ for(intj=1;j<=k;j++){ for(intg=1;g<=m;g++){ t.c[i][j]+=a.c[i][g]*......