某比赛小记5- 螺旋遍历矩阵

题目:给定一个二维矩阵,从[0][0]开始向右,按顺时针遍历全部数据,比如[[a,b][c,d]],遍历顺序就是a,b,d,c。最后将遍历的元素用逗号连接,打印整个遍历结果。给定二维矩阵见文件。

题解:本题思路很容易设计,就是每次访问越界或者是访问到已经访问的元素就向右转向,当右边没有路了则结束。难点主要是边界条件的处理。

python版本:

m = [给定矩阵]
#利用numpy构造二维全0数组
# tagnp = np.zeros(32*32)
# tagnp = tagnp.reshape(32,32)
# tagnp = tagnp.astype(int)
# tag = tagnp.tolist()
#直接构造二维全零数组
tag = [[0 for i in range(32)] for j in range(32)]  #用来存储元素是否访问过
i = 0  #当前访问元素下标记录
j = 0  #当前访问元素下标记录
ret = [] #保存访问的结果
ret.append(m[i][j])
tag[i][j] = 1
while (j+1<32 and tag[i][j+1]==0) or (i+1<32 and tag[i+1][j]==0) or (j-1>=0 and tag[i][j-1]==0) or (i-1 >=0 and tag[i-1][j]==0):
    #右
    while j+1 < 32 and tag[i][j+1] == 0:
        ret.append(m[i][j+1])
        tag[i][j+1] = 1
        j += 1
    while i+1 < 32 and tag[i+1][j] == 0:
        ret.append(m[i+1][j])
        tag[i+1][j] = 1
        i += 1
    while j-1>=0 and tag[i][j-1] == 0:
        ret.append(m[i][j-1])
        tag[i][j-1] = 1
        j -= 1
    while i-1>=0 and tag[i-1][j] == 0:
        ret.append(m[i-1][j])
        tag[i-1][j] = 1
        i -= 1
retstrlist = [] #把结果转成str,比赛时过于紧张忘记使用map()
for mi in ret:
    retstrlist.append(str(mi))
print(retstrlist)
fret = ‘,‘.join(retstrlist)
fret.replace(‘ ‘,‘‘)
print(fret) #最终结果

  

原文地址:https://www.cnblogs.com/chmod777/p/9975740.html

时间: 2024-07-30 12:14:56

某比赛小记5- 螺旋遍历矩阵的相关文章

LeetCode:Spiral Matrix - 螺旋输出矩阵中的元素

1.题目名称 Spiral Matrix(螺旋输出矩阵中的元素) 2.题目地址 https://leetcode.com/problems/spiral-matrix/ 3.题目内容 英文:Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. 中文:给出一个m行n列的矩阵,以螺旋顺序返回矩阵中的所有元素. 例如:现有矩阵如下: [  [ 1,

[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的数字

螺旋的矩阵

螺旋的矩阵 Time Limit: 1000 MS Memory Limit: 32768 K Total Submit: 23(16 users) Total Accepted: 14(14 users) Rating: Special Judge: No Description 给出一个奇数 n,我们可以把数字 1 到 n*n 放到一个螺旋的矩阵中. 下图给出了从1到25的放法: 1    2   3   4   5 --------------------- 1| 21  22  23  

OpenCV快速遍历矩阵元素方法

OpenCV中Mat矩阵data数据的存储方式和二维数组不一致,二维数组按照行优先的顺序依次存储,而Mat中还有一个标示行步进的变量Step.使用Mat.ptr<DataTyte>(row) 行指针的方式定位到每一行,可快速遍历矩阵.例程如下: 1 std::cout << "The inverse matrix of K is:" << std::endl; 2 for(int i=0;i<3;i++) 3 { 4 float* data =

矩阵螺旋遍历Spiral Matrix,Spiral Matrix2

SpiralMatrix: 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]. 算法分析:其实就是两

[C++]LeetCode: 110 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]. 思路:我们来自己画一个螺旋线的行走轨迹

螺旋打印矩阵

题目: 对于一个N*M矩阵,请顺时针螺旋打印输出: 如 3 * 4 矩阵, 1   2   3  4 10 11 12 5 9    8   7  6 输出: 1 2 3 4 5 6 7 8 9 10 11 12 1 #include <stdio.h> 2 // nomally print matric 3 void print_matric(const int *m, int r, int c) { 4 int i, j; 5 printf("%d * %d matric:\n&

简单螺旋打印矩阵 - 直接, 好理解

打印螺旋矩阵的一种简单办法 , C 语言实现 #include <stdio.h> #define N 30 static int val = 10; int arr[N][N]; void print_row(int row_cur, int col_start, int col_end, int left_to_right){ int col_cur = col_start; if( col_start < 0 || col_end < 0) return; if( left_

SpiralOrderTraverse,螺旋遍历二叉树,利用两个栈

问题描述:s型遍历二叉树,或者反s型遍历二叉树 算法分析:层序遍历二叉树只需要一个队列,因为每一层都是从左往右遍历,而s型遍历二叉树就要用两个栈了,因为每次方向相反. public static void SpiralOrderTraverse(TreeNode t) { Stack<TreeNode> stack1 = new Stack<>(); Stack<TreeNode> stack2 = new Stack<>(); stack1.push(t)