-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathview_count.c
124 lines (113 loc) · 2.76 KB
/
view_count.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
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
int view_count_top(int **board, int N, int x);
int view_count_bottom(int **board, int N, int x);
int view_count_right(int **board, int N, int y);
int view_count_left(int **board, int N, int y);
void view_count(int **board, int **current_view, N)
{
int i; //the column of the current_view matrix, meaning which column OR row you're checking
int j; //the row of the current_view matrix, so 0 is colup, 1 is coldown, 2 is rowleft, and 3 is rowright
j = 0;
while (j < 4)
{
i = 0;
while (i < N) //
{
if (j == 0)
current_view[j][i] = view_count_top(board, N, i);
if (j == 1)
current_view[j][i] = view_count_bottom(board, N, i);
if (j == 2)
current_view[j][i] = view_count_left(board, N, i);
if (j == 3)
current_view[j][i] = view_count_right(board, N, i);
i++;
}
j++;
}
return
}
//calculates the view count from the top downwards for a given value of x, so for a given column
int view_count_top(int **board, int N, int x)
{
int count;
int highest;
int y;
y = 0;
count = 0;
highest = 0;
while (y < N)
{
if (highest == N) //to make sure that we stop as soon as we've reached the highest height
return (count);
if (board[y][x] > highest) //if we've encountered a higher block
{
count++; //we now view one more
highest = board[y][x]; //set the current position as the new highest block
}
y++;
}
return (count);
}
int view_count_bottom(int **board, int N, int x)
{
int count;
int highest;
int y;
y = N - 1; //to start at the end of the column
count = 0;
highest = 0;
while (y >= 0)
{
if (highest == N)
return (count);
if (board[y][x] > highest)
{
count++;
highest = board[y][x];
}
y--; //because we're going bottom to top
}
return (count);
}
int view_count_left(int **board, int N, int y)
{
int count;
int highest;
int x;
x = 0;
count = 0;
highest = 0;
while (x < N)
{
if (highest == N)
return (count);
if (board[y][x] > highest)
{
count++;
highest = board[y][x];
}
x++;
}
return (count);
}
int view_count_right(int **board, int N, int y)
{
int count;
int highest;
int x;
x = N - 1;
count = 0;
highest = 0;
while (x >= 0)
{
if (highest == N)
return (count);
if (board[y][x] > highest)
{
count++;
highest = board[y][x];
}
x--;
}
return (count);
}