# 885. Spiral Matrix III

Source: Internet
Author: User

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 are at the first row and column, and the South-east corner of the grid are at the LA St row and column.

Now, we walk in a clockwise spiral shape to visit every position in the this grid.

Whenever we would move outside the boundary of the grid, we continue our walk outside the grid (and 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
``Class Solution:def spiralmatrixiii (self, R, C, R0, C0): "" ": Type R:int:type C:int:typ        e r0:int:type C0:int:rtype:list[list[int]] "" "count = 1 L = 1 res = []                 Direction = 1 Res.append ([r0,c0]) while count<r*c:if direction==1:c0 + = L                    # print (' Direction:1,pos: ', r0,c0) for I in Range (c0-l+1,c0+1): #不考虑起点, consider the end point                If 0<=r0<r and 0<=i<c:res.append ([r0,i]) Count + = 1            # print (count) # Print (res) Direction = 2 Continue If direction==2:r0 + = l # print (' Direction:2,pos: ', r0,c0) for I in range (r0-                        L+1,R0+1): If 0<=i<r and 0<=c0<c:res.append ([i,c0])      Count + = 1          # print (count) # Print (res) Direction = 3 L + = 1                Continue if direction==3:c0-= l # print (' Direction:3,pos: ', r0,c0) For I in Range (c0+l-1,c0-1,-1): If 0<=r0<r and 0<=i<c:res.ap                Pend ([r0,i]) Count + = 1 # print (count) # Print (res) Direction = 4 Continue if direction = = 4:r0 = L # print (' d Irection:4,pos: ', r0,c0) for I in Range (r0+l-1,r0-1,-1): If 0<=i<r and 0<=c0<                C:res.append ([i,c0]) Count + = 1 # print (count) # Print (res) Direction = 1 L + = 1 Continue return res``

Each conversion direction, each walk two times the length plus 1, the loop jumps out the condition for the quantity to reach all the lattice number. For each walk, the judgment is preserved within the range of the board.

885. Spiral Matrix III

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.