题目链接:https://leetcode-cn.com/problems/spiral-matrix-iii/
算法思路:https://www.cnblogs.com/grandyang/p/10887598.html
https://leetcode-cn.com/problems/spiral-matrix-iii/solution/luo-xuan-ju-zhen-iii-by-leetcode/
class Solution(object):
def spiralMatrixIII(self, R, C, r0, c0):
"""
:type R: int
:type C: int
:type r0: int
:type c0: int
:rtype: List[List[int]]
"""
res = [[r0, c0]]
X = [0, 1, 0, -1]#(r,c)
Y = [1, 0, -1, 0]
cur, step = 0, 0
while len(res) < R * C:
if cur == 0 or cur == 2:
step += 1
for _ in range(0, step, 1):
r0 += X[cur]
c0 += Y[cur]
if r0 < R and r0 >= 0 and c0 < C and c0 >= 0:
res.append([r0, c0])
#print(cur)
cur = (cur + 1) % 4
print(res)
return res
x = Solution()
R = 1
C = 4
r0 = 0
c0 = 0
x.spiralMatrixIII(R, C, r0, c0)
package main
import "fmt"
func spiralMatrixIII(R int, C int, r0 int, c0 int) [][]int {
var res [][]int
//东南西北,
X := []int{0, 1, 0, -1} //r0相当于南北
Y := []int{1, 0, -1, 0} //c0东西
cur, step := 0, 0
res = append(res, []int{r0, c0})
for len(res) < R * C {
if cur == 0 || cur == 2 {
step ++
}
for i := 0; i < step; i ++ {
r0 += X[cur]
c0 += Y[cur]
if r0 < R && r0 >= 0 && c0 < C && c0 >= 0 {
res = append(res, []int{r0, c0})
fmt.Println(res, len(res))
}
}
cur = (cur + 1) % 4
}
fmt.Println(res)
return res
}
func main() {
R, C, r0, c0 := 1, 4, 0, 0
spiralMatrixIII(R, C, r0, c0)
}