经过扫描后得到的数据结果为:
57 | 45 | 0 | 23 | 0 | 0 | 0 | 0 |
0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
0 | -30 | 1 | 0 | 0 | 0 | 0 | 0 |
-16 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
“57,45,0,0,0,0,23,0,-30,-16,0,0,1,0,0,0,0,…,0,0”,数据元素个数为64个。进一步经过压缩后,最终得到行程编码:“57, 1, 45, 1, 0, 4, 23, 1, 0, 1,-30, 1,-16, 1, 0, 2, 1, 1, 0, 51”,数据元素个数为20个。
Dim a(0 To 1000) As Integer '存储原矩阵数据,按行优先存储
Dim b(0 To 1000) As Integer '存储Z形扫描后数据
Dim c(0 To 1000) As Integer '存储行程编码压缩后数据
Dim n As Integer
‘矩阵导入代码略,以行优先存储在a数组中,如例子中数据存储顺序为“57,45,0,23,0,0…”
Private Sub Command2_Click()
Dim choice As Integer ' 1:向右移动 ;2:向右上移动;3向下移动 4向左下移动
Dim row As Integer, col As Integer, i As Integer, j As Integer
Dim pre As Integer, count As Integer
choice = 1: row = 0: col = 0: i = 0
Do While (row <> n - 1 Or col <> n - 1)
b(i) = a(row * n + col):i = i + 1
If choice = 1 Then
If row = 0 Then choice = 4 Else choice = 2
ElseIf choice = 2 Then
row = row - 1: col = col + 1
If Then
choice = 1
ElseIf col = n - 1 Then
choice = 3
End If
ElseIf choice = 3 Then
row = row + 1
If col = 0 Then choice = 2 Else choice = 4
ElseIf choice = 4 Then
row = row + 1: col = col - 1
If row = n - 1 Then
choice = 1
ElseIf col = 0 Then
choice = 3
End If
End If
Loop
b(i) = a(n * n - 1):j = 0: pre = b(0): count = 0
For i = 0 To n * n - 1 '输出Z形序列,并进行行程压缩
If pre = b(i) Then
count = count + 1
Else
c(j) = pre: c(j + 1) = count
pre = b(i):j = j + 2
End If
Next i
c(j) = pre: c(j + 1) = count
Text1.Text = ""
For i = 0 To j + 1
Text1.Text = Text1.Text + Str(c(i)) + ","
Next i
End Sub