-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtraverse_array_like_clip.c
89 lines (80 loc) · 2.15 KB
/
traverse_array_like_clip.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <stdio.h>
#define M 3
#define N 3
int a[M][N] = {
0x1, 0x2, 0x3,
0x8, 0x9, 0x4,
0x7, 0x6, 0x5,
};
/*
#define M 4
#define N 3
int a[M][N] = {
0x1, 0x2, 0x3,
0xa, 0xb, 0x4,
0x9, 0xc, 0x5,
0x8, 0x7, 0x6,
};
*/
int nx = N;
int ny = M;
struct pos{
int x;
int y;
};
struct pos curr_pos = {0, 0};
int nDirection = 4;
enum Direction { Right=0, Down, Left, Up };
enum Direction curr_dir = Right;
void print()
{
while(1){
switch(curr_dir){
case Right:
for(int i=0; i<nx; i++){
printf("%x\n", a[curr_pos.y][curr_pos.x]);
if(i < nx-1) // 最后一次坐标不变
curr_pos.x++;
}
ny--; // 走完一行
curr_pos.y++; // 挪到下一个开始点
break;
case Down:
for(int i=0; i<ny; i++){
printf("%x\n", a[curr_pos.y][curr_pos.x]);
if(i < ny-1) // 最后一次坐标不变
curr_pos.y++;
}
nx--; // 走完一列
curr_pos.x--; // 挪到下一个开始点
break;
case Left:
for(int i=0; i<nx; i++){
printf("%x\n", a[curr_pos.y][curr_pos.x]);
if(i < nx-1) // 最后一次坐标不变
curr_pos.x--;
}
ny--; // 走完一行
curr_pos.y--; // 挪到下一个开始点
break;
case Up:
for(int i=0; i<ny; i++){
printf("%x\n", a[curr_pos.y][curr_pos.x]);
if(i < ny-1) // 最后一次坐标不变
curr_pos.y--;
}
nx--; // 走完一列
curr_pos.x++; // 挪到下一个开始点
break;
}
if(nx<=0 || ny<=0)
return;
// change direction
curr_dir = (curr_dir+1)%nDirection;
}
}
int main()
{
print();
return 0;
}