c++ matrix逆时针螺旋

题目

输入:3
输入:  

5 4 3
6 1 2
7 8 9

AC代码

#include <iostream>
using namespace std;
int a[101][101];
int main()
{
    memset(a,0,sizeof(a));
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        a[0][i]=a[i][0]=a[n+1][i]=a[i][n+1]=1;
    }
    int x=n,y=n,p=n*n;
    while(p>=1)
    {
        while (a[x][y]==0) a[x][y--]=p--;
        x--;y++;
        while (a[x][y]==0) a[x--][y]=p--;
        x++;y++;
        while (a[x][y]==0) a[x][y++]=p--;
        x++;y--;
        while (a[x][y]==0) a[x++][y]=p--;
        x--;y--;
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            printf("%d ",a[i][j]);
        }
        printf("\n");
    }
    return 0;
}

原文地址:https://www.cnblogs.com/LJA001162/p/11217369.html

时间: 2024-11-05 18:50:15

c++ matrix逆时针螺旋的相关文章

顺时针和逆时针螺旋打印二维数组(行列式)

一.要求: 行列式,行和宽不一定相等,要求顺时针螺旋打印每一个元素,所谓顺时针螺旋即: 第一圈:从第一行第一列元素开始,先从左到右打印第一行所有元素,接着打印最后一剩余列元素,再从右到左打印最后一行剩余元素,接着从下到上打印第一列剩余元素 第二圈:从第二行第二列开始,按上面的顺时针顺序打印一圈元素 ...,一圈圈反复,直到把所有元素无重复的打印完. 逆时针螺旋与上面的顺序刚好相反. 二.分析: 顺时针螺旋打印可以将问题分割为:先按顺时针打印第一圈,再按顺时针打印第二圈,其中每圈又分四个步骤:从左

leetcode-Spiral Matrix II 螺旋矩阵2之python大法好,四行就搞定,你敢信?

Spiral Matrix II 螺旋矩阵 Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example,Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] 真的不容易..看博客园有人面试碰到过这个问题,长篇

poj1696Space Ant(逆时针螺旋形)

链接 贪心做法,没次找最外面的点,也就是相对前面那条线偏转角度最小的点,除第一个点需要找到最下面的点即Y坐标最小,其余的每次进行极角排序. 1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<stdlib.h> 6 #include<vector> 7 #include<cmath> 8

【LeetCode-面试算法经典-Java实现】【054-Spiral Matrix(螺旋矩阵)】

[054-Spiral Matrix(螺旋矩阵)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. For example, Given the following matrix: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ]

[C++]LeetCode: 110 Spiral Matrix II (螺旋写入矩阵)

题目: Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example, Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] 思路:这道题和Spiral Matrix的模型和解法一样.要求我们将1~n^2的数字

leetCode 59.Spiral Matrix II (螺旋矩阵II) 解题思路和方法

Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example, Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] 思路:此题和螺旋矩阵题差不多,没什么难的地方,主要就是四个方向的转换. 具体代码如下: pu

LeetCode 59 Spiral Matrix II 螺旋矩阵之二

Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example,Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] 此题跟之前那道本质上没什么区别,就相当于个类似逆运算的过程,这道题是要按螺旋的顺序来填数,由于

LeetCode OJ:Spiral Matrix(螺旋矩阵)

Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. For example,Given the following matrix: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] You should return [1,2,3,6,9,8,7,4,5]. 典型的dfs问题,与前面一个问题比较像,代码如下

[LeetCode] 885. Spiral Matrix III 螺旋矩阵之三

On a 2 dimensional grid with?R?rows and?C?columns, we start at?(r0, c0)?facing east. Here, the north-west corner of the grid is at the?first row and column, and the south-east corner of the grid is at the last row and column. Now, we walk in a clockw