[Solution] 885. Spiral Matrix Ⅲ

  • Difficulty: Medium

Problem

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 talk in a clockwise spiral shape to visit every position in this grid.

Whenever we could move outside the boundary of the grid, we continue our walk outside the grid (but may return to the grid boundary later.)

Eventually, we reach all R * C spaces of the grid.

Return a list of coordinates representing the positions of the grid in the order they were visited.

Example 1:

Input: R = 1, C = 4, r0 = 0, c0 = 0
Output: [[0, 0], [0, 1], [0, 2], [0, 3]]

Example 2:

Input: R = 5, C = 6, r0 = 1, c0 = 4
Output:
[[1, 4], [1, 5],
 [2, 5], [2, 4], [2, 3],
 [1, 3], [0, 3],
 [0, 4], [0, 5],
 [3, 5], [3, 4], [3, 3], [3, 2],
 [2, 2], [1, 2], [0, 2],
 [4, 5], [4, 4], [4, 3], [4, 2], [4, 1],
 [3, 1], [2, 1], [1, 1], [0, 1],
 [4, 0], [3, 0], [2, 0], [1, 0], [0, 0]]

Note:

  1. 1 <= R <= 100
  2. 1 <= C <= 100
  3. 0 <= r0 < R
  4. 0 <= c0 < C

Related Topics

Math

Solution

题意就是给定一个矩阵大小和起始坐标,返回据此条件生成的螺旋方阵的路径,可以走到矩阵之外,但如果坐标落在矩阵之内,则要记录下来。比较容易想到的一个方法是模拟这样的一个走步过程。观察走螺旋的过程,可以发现其在每个方向上走过的距离依次是 [1, 1, 2, 2, 3, 3, ...],借助这个规律,就比较好模拟出来了。

public class Solution
{
    public int[][] SpiralMatrixIII(int R, int C, int r0, int c0)
    {
        int[][] ret = new int[R * C][];
        int i = 0;
        foreach (var coord in NextCoord(r0, c0))
        {
            if (0 <= coord.Item1 && coord.Item1 < R &&
                0 <= coord.Item2 && coord.Item2 < C)
            {
                ret[i++] = new int[] { coord.Item1, coord.Item2 };
            }
            if (i == R * C)
                break;
        }
        return ret;
    }

    private IEnumerable<Tuple<int, int>> NextCoord(int r, int c)
    {
        int step = 1;
        yield return new Tuple<int, int>(r, c);
        for( ; ; )
        {
            for (int i = 0; i < step; i++)
                yield return new Tuple<int, int>(r, ++c);
            for (int i = 0; i < step; i++)
                yield return new Tuple<int, int>(++r, c);
            step++;
            for (int i = 0; i < step; i++)
                yield return new Tuple<int, int>(r, --c);
            for (int i = 0; i < step; i++)
                yield return new Tuple<int, int>(--r, c);
            step++;
        }
    }
}
  • Note:这里使用到了 C? 中的 yield 关键字,有关这个的介绍,参见 MSDN 文档

另外还有一个可行的方法,就是对该矩阵内所有坐标进行排序(首先以其到 (r0, c0) 的距离排序,距离相同的,按照向量 \((r - r_0, c - c_0)^\mathrm{T}\) 与 x 轴正向的夹角大小排序),不过自己似乎写不出这样的代码,留待日后思量。

原文地址:https://www.cnblogs.com/Downstream-1998/p/10352613.html

时间: 2024-10-13 09:13:21

[Solution] 885. Spiral Matrix Ⅲ的相关文章

LeetCode 885. Spiral Matrix III

原题链接在这里:https://leetcode.com/problems/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

[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

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

[数学] leetcode 885 Spiral Matrix III

problem:https://leetcode.com/problems/spiral-matrix-iii/ 这道题挺简单的,只需要模拟一下题意就可以了.不断地增加步数和改变方向,直到已经读取到矩阵的所有数据. class Solution { public: vector<int> dx{ 0,1,0,-1 }; vector<int> dy{ 1,0,-1,0 }; vector<vector<int>> spiralMatrixIII(int R,

Spiral Matrix(LintCode)

Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. 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]. 难得的一次AC! 虽然感觉题

LeetCode:Spiral Matrix II - 将元素1-n^2以螺旋序填充到矩阵

1.题目名称 Spiral Matrix(螺旋输出矩阵中的元素) 2.题目地址 https://leetcode.com/problems/spiral-matrix-ii/ 3.题目内容 英文:Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. 中文:给出一个整数n,生成一个矩阵,使用数字1到n^2以螺旋顺序填充这个矩阵 例如:给出n=3,则生成如下矩阵:

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,

[LeetCode][Java] 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 ] ] 题意: 给定一个整数 n,生成一个正方形矩阵.矩阵中包含着从1到n2 这些元素,并且

[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 ] ] [分析] 模拟 [代码] /**-------------------------