例如如下的二维数组
var arr = [][]int{
{1, 2, 3, 4, 5, 6},
{7, 8, 9, 10, 11, 12},
{13, 14, 15, 16, 17, 18},
{19, 20, 21, 22, 23, 24},
}
依次打印1,2,3,4,5,6,11,16,21,20,19,13,7,8,9,10,15,14
func printArr(arr [][]int, brow int, bcol int, row int, col int) {
if len(arr) == 0 || row <= 0 || col <= 0 {
return
}
var rowIndex = brow
var colIndex = bcol
for colIndex < col {
fmt.Println(arr[rowIndex][colIndex])
path = append(path, fmt.Sprintf("%d%d", rowIndex, colIndex))
colIndex++
}
colIndex = colIndex - 1
for rowIndex < row-1 {
rowIndex++
colIndex--
fmt.Println(arr[rowIndex][colIndex])
path = append(path, fmt.Sprintf("%d%d", rowIndex, colIndex))
}
for colIndex > bcol {
colIndex--
fmt.Println(arr[rowIndex][colIndex])
path = append(path, fmt.Sprintf("%d%d", rowIndex, colIndex))
}
for rowIndex > brow+1 {
rowIndex--
fmt.Println(arr[rowIndex][colIndex])
path = append(path, fmt.Sprintf("%d%d", rowIndex, colIndex))
}
col = col - 2
colIndex = colIndex + 1
row = row - 1
printArr(arr, rowIndex, colIndex, row, col)
}