-
Notifications
You must be signed in to change notification settings - Fork 0
/
sky_scrapers.c
199 lines (186 loc) · 3.8 KB
/
sky_scrapers.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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* rush01.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dazaffal <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/30 14:27:07 by dazaffal #+# #+# */
/* Updated: 2024/04/01 18:42:53 by dazaffal ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
#include <unistd.h>
#define N 4
//M[m][n]; M[i][j]= *(M+(i*n+j))
void fill_board(int board[][N], int c)
{
int i;
int j;
i = 0;
while (i < N)
{
j = 0;
while (j < N)
{
board[i][j] = c;
j++;
}
i++;
}
}
void print_board(int board[][N])
{
int i;
int j;
char v;
i = 0;
while (i < N)
{
j = 0;
while (j < N)
{
v = board[i][j] + 48;
write(1, &v, 1);
write(1, " ", 1);
j++;
}
write(1, "\n", 1);
i++;
}
}
int parsing(int views[][N], char *p)
{
int i;
int j;
int counter;
//write(1, "Views are: \nUp\nDown\nLeft\nRight\n", 32);
counter = 0;
i = 0;
while (*p != '\0' && i < N)
{
j = 0;
while (*p != '\0' && j < N)
{
views[i][j] = *p - 48;
j++;
p = p + 2;
counter++;
}
i++;
}
if (counter != (N * N))
return (0);
else
return (1);
}
int is_valid(int board[][N], int views[][N], int position)
{
int i;
int acc_view[4]; //accumulate up, down, left, right view
int last_max[4]; //last maximum value found for each view
int row;
int col;
int num;
row = position / N;
col = position % N;
num = board[row][col];
i = 0;
while (i < N)
{
if (board[row][i] == num && i != col)
return (0);
if (board[i][col] == num && i != row)
return (0);
i++;
}
i = 0;
while (i < 4)
{
acc_view[i] = 0;
last_max[i] = 0;
i++;
}
i = 0;
while (i < N)
{
if (board[i][col] == 0)
{
acc_view[0] = -1;
acc_view[1] = -1;
}
if (board[row][i] == 0)
{
acc_view[2] = -1;
acc_view[3] = -1;
}
if (acc_view[0] >= 0 && board[i][col] > last_max[0])
{
last_max[0] = board[i][col];
acc_view[0]++;
}
if (acc_view[1] >= 0 && board[(N - 1) - i][col] > last_max[1])
{
last_max[1] = board[(N - 1) -i][col];
acc_view[1]++;
}
if (acc_view[2] >= 0 && board[row][i] > last_max[2])
{
last_max[2] = board[row][i];
acc_view[2]++;
}
if (acc_view[3] >= 0 && board[row][(N - 1) - i] > last_max[3])
{
last_max[3] = board[row][(N - 1) -i];
acc_view[3]++;
}
i++;
}
if ((acc_view[0] == views[0][col] || acc_view[0] == -1)
&& (acc_view[1] == views[1][col] || acc_view[1] == -1)
&& (acc_view[2] == views[2][row] || acc_view[2] == -1)
&& (acc_view[3] == views[3][row] || acc_view[3] == -1))
return (1);
else
return (0);
}
int solve(int board[][N], int position, int views[][N])
{
int row;
int col;
int num;
num = 1;
if (position == N * N)
return 1;
row = position / N;
col = position % N;
while (num <= N)
{
board[row][col] = num;
if (is_valid(board, views, position))
{
if (solve(board,(position + 1),views))
return (1);
}
num++;
}
board[row][col] = 0;
return (0);
}
int main(int ac,char **av)
{
char *input;
int board[N][N];
int views[4][N];
input = av[1];
if (!parsing(views, input) && ac != 2)
{
write(1, "Error: unexpected number of arguments\n", 39);
return (1);
}
fill_board(board, 0);
if (solve(board, 0, views))
print_board(board);
else
write(1, "It can not be solved\n", 21);
}