-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAY1920 task2 Sky Fly Direction Arrow Array.cpp
153 lines (136 loc) · 4.33 KB
/
AY1920 task2 Sky Fly Direction Arrow Array.cpp
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <cstdio>
#include <iostream>
using namespace std;
#define MAXCOL 15
#define MAXROW 20
#define LEFT '<'
#define RIGHT '>'
#define UP '^'
#define DOWN 'v'
#define CROSS 'X'
#define EMPTY '-'
struct Plane{
int row;
int col;
char direction;
};
void print_trail(char region[MAXROW][MAXCOL], int nRow, int nCol)
{
int i, j;
for (i = 0; i < nRow; i++){
for (j = 0; j < nCol; j++){
printf("%c", region[i][j]);
}
printf("\n");
}
printf("\n");
}
void findEdgePlanes(char region[MAXROW][MAXCOL], Plane edgePlanes[], int &count){
int index = 0;
for(int i = 0; i < MAXROW; i++){
if(region[i][0] == RIGHT){ //left edge
edgePlanes[index] = {i, 0, RIGHT};
index++;
}
if(region[i][MAXCOL-1] == LEFT){ // right edge
edgePlanes[index] = {i, MAXCOL-1, LEFT};
index++;
}
}
for(int i = 0; i < MAXCOL; i++){
if(region[0][i] == DOWN){ //top edge
edgePlanes[index] = {0, i, DOWN};
index++;
}
if(region[MAXROW-1][i] == UP){ //bottom edge
edgePlanes[index] = {MAXROW-1, i, UP};
index++;
}
}
count = index;
}
bool hasObstacle(int row, int col, char region[MAXROW][MAXCOL]){
if(region[row][col] == EMPTY){
return false;
}
return true;
}
void updateMap(Plane plane, char region[MAXROW][MAXCOL]){
if(plane.direction == UP){
for(int i = MAXROW-2; i > -1; i--){
if(hasObstacle(i, plane.col, region)){
region[i][plane.col] = CROSS;
continue;
}
region[i][plane.col] = UP;
}
}
if(plane.direction == DOWN){
for(int i = 1; i < MAXROW; i++){
if(hasObstacle(i, plane.col, region)){
region[i][plane.col] = CROSS;
continue;
}
region[i][plane.col] = DOWN;
}
}
if(plane.direction == LEFT){
for(int i = MAXCOL-2; i > -1; i--){
if(hasObstacle(plane.row, i, region)){
region[plane.row][i] = CROSS;
continue;
}
region[plane.row][i] = LEFT;
}
}
if(plane.direction == RIGHT){
for(int i = 1; i < MAXCOL; i++){
if(hasObstacle(plane.row, i, region)){
region[plane.row][i] = CROSS;
continue;
}
region[plane.row][i] = RIGHT;
}
}
}
void plot_trail(char region[MAXROW][MAXCOL])
{
int count = 0;
int max = MAXROW * 2 + MAXCOL * 2;
Plane edgePlanes[max] = { };
findEdgePlanes(region, edgePlanes, count);
for(int i = 0; i < count; i++){
updateMap(edgePlanes[i], region);
}
}
int main()
{
char region[MAXROW][MAXCOL] = {
'-','-','-','-','v','-','-','^','-','-','-','-','-','-','-',
'-','-','-','-','-','-','-','-','-','-','-','-','-','-','-',
'-','-','-','-','-','-','-','-','-','-','-','-','-','-','-',
'-','-','-','-','P','P','E','1','-','-','-','-','-','-','-',
'-','-','-','-','-','-','-','-','-','-','-','-','-','-','-',
'>','-','-','-','-','-','1','-','N','O','T','-','-','-','-',
'-','-','-','-','-','-','-','-','-','-','-','h','-','-','-',
'-','-','-','-','-','-','-','-','-','-','-','a','-','-','-',
'<','-','-','-','-','-','-','-','-','-','-','r','-','-','-',
'-','-','-','-','-','-','-','-','-','-','-','d','-','-','-',
'-','-','-','-','-','-','-','@','-','-','-','!','-','-','-',
'-','-','-','-','-','-','$','-','-','-','-','-','-','-','-',
'-','-','-','-','-','*','-','-','-','-','-','-','-','-','-',
'-','-','-','-','-','-','-','-','-','-','-','-','-','-','-',
'-','-','-','-','-','-','-','-','^','-','-','-','-','-','>',
'-','-','-','-','-','-','-','<','-','-','-','-','-','-','-',
'-','-','-','-','-','-','-','>','-','-','-','-','-','-','-',
'-','-','-','-','-','-','v','-','-','-','-','-','-','-','<',
'-','-','-','-','-','-','-','-','-','-','-','-','-','-','-',
'-','-','v','-','-','-','-','-','-','-','^','-','-','-','-'
};
//Before trail is plotted
//print_trail(region, MAXROW, MAXCOL);
plot_trail(region);
//After trail is plotted
print_trail(region, MAXROW, MAXCOL);
return 0;
}