-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbingo.c
More file actions
339 lines (292 loc) · 8.13 KB
/
Copy pathbingo.c
File metadata and controls
339 lines (292 loc) · 8.13 KB
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
// Crearea matricelor din vector (atribuirea matricelor pe pozitiile unui vector, creem vector 3D)
int **creMat()
{
int i, **mat = (int **)malloc(5 * sizeof(int *));
for (i = 0; i < 5; i++)
mat[i] = (int *)malloc(5 * sizeof(int));
return mat;
}
// Citire matrice
void citMat(int **mat)
{
int i, j;
for (i = 0; i < 5; i++)
for (j = 0; j < 5; j++)
scanf("%d", &mat[i][j]);
mat[2][2] = -mat[2][2];
}
// Print matrice pastigatoare
void prtWin(int **mat)
{
int i, j;
for (i = 0; i < 5; i++)
{
for (j = 0; j < 5; j++)
if (mat[i][j] >= 0)
printf("%d ", mat[i][j]);
else
printf("# ");
printf("\n");
}
}
// Swap
void swap(int *a, int *b)
{
*a = *a + *b;
*b = *a - *b;
*a = *a - *b;
}
// cauta la ce pozitie se afla coloana
int search(int *v, int x)
{
int i;
for (i = 0; i < 5; i++)
if (v[i] == x)
return i;
return 0;
}
// Bubble sort (for collumn X asc/desc)
void ASC(int ***mat, int n, int x)
{
int i, j, k;
for (k = 0; k < n; k++)
for (i = 0; i < 5; i++)
for (j = 0; j < 4; j++)
if (abs(mat[k][j][x]) > abs(mat[k][j + 1][x]))
swap(&mat[k][j][x], &mat[k][j + 1][x]);
}
void DSC(int ***mat, int n, int x)
{
int i, j, k;
for (k = 0; k < n; k++)
for (i = 0; i < 5; i++)
for (j = 0; j < 4; j++)
if (abs(mat[k][j + 1][x]) > abs(mat[k][j][x]))
swap(&mat[k][j + 1][x], &mat[k][j][x]);
}
// Hasuram spatiile
void hash(int **mat, int pz, int y)
{
int i;
for (i = 0; i < 5; i++)
if (mat[i][pz] == y)
mat[i][pz] = -mat[i][pz];
}
// SHIFT-X-Y
void shiftXY(int ***mat, int n, int x, int y)
{
int i, j, v[5], poz;
for (i = 0; i < n; i++)
{
// atribuim vectorului elementele coloanei pentru a le permuta
for (j = 0; j < 5; j++)
v[j] = mat[i][j][x];
if (!(y % 5))
continue;
else
for (j = 0; j < 5; j++)
{
poz = (j + y) % 5;
mat[i][poz][x] = v[j];
}
}
}
// SWAP-X-Y
void swapXY(int ***mat, int n, int x, int y, int *v)
{
int i, j;
if (x != y)
{
for (i = 0; i < n; i++)
for (j = 0; j < 5; j++)
swap(&mat[i][j][x], &mat[i][j][y]);
swap(&v[x], &v[y]);
}
}
// Verifica daca matricea a facut bingo
int points(int ***mat, int *pt, int n)
{
int i, j, k;
int c, l, d, ds;
int poz = -1, max = -1;
for (i = 0; i < n; i++)
{
d = 0, ds = 0;
// Verificam BINGO pe diagonala secundara
for (j = 0; j < 5; j++)
{
if (mat[i][4 - j][j] < 0)
ds++;
if (ds == 5)
pt[i] += 10;
}
// Verificam BINGO pe linie
for (j = 0; j < 5; j++)
{
l = 0;
for (k = 0; k < 5; k++)
if (mat[i][j][k] < 0)
l++;
if (l == 5)
pt[i] += 10, l = 0;
}
// Verificam BINGO pe coloana
for (j = 0; j < 5; j++)
{
c = 0;
for (k = 0; k < 5; k++)
if (mat[i][k][j] < 0)
c++;
if (c == 5)
pt[i] += 10, c = 0;
}
// Verificam BINGO pe diagonala
for (j = 0; j < 5; j++)
{
if (mat[i][j][j] < 0)
d++;
if (d == 5)
pt[i] += 10;
}
}
for (i = 0; i < n; i++)
if (pt[i] > max && pt[i] > 0)
max = pt[i], poz = i;
if ((max >= 0))
{
printf("%d\n", poz);
prtWin(mat[poz]);
printf("%d\n", pt[poz]);
return 1;
}
return 0;
}
// Completam matricea cu datele de sus^
int completare(int ***cards, int m, int n, int *v)
{
int i, j, nr = 0, x = 0, y = 0, k = 0, h, d = 0;
int *pt;
char *p, c;
char date[11], aux[11];
pt = (int *)calloc(n, sizeof(int));
for (h = 0; h < m; h++)
{
scanf("%s", date);
strcpy(aux, date);
p = strtok(aux, "-");
k = x = y = 0;
// verificam daca fct este de colorare(fct hash) sau de lucru pe matrice
if (strlen(p) > 2)
{
// verificam nr de argumente al fct
if (p[0] == 'A' ||
p[0] == 'D' ||
strcmp(p, "SHOW") == 0)
k = 1; // ASC si DSC au un singur parametru x
p = strtok(NULL, "-");
for (i = 0; i < 2 - k; i++)
{ // pentru fct cu 2 parametri si posibili parametrii de 2 cifre
if (!i)
for (j = 0; j < strlen(p); j++)
x = x * 10 + p[j] - '0';
else
for (j = 0; j < strlen(p); j++)
y = y * 10 + p[j] - '0';
p = strtok(NULL, "-");
}
p = strtok(date, "-");
// printf("\n%d %d\n", x, y);
if (!strcmp(p, "SWAP"))
swapXY(cards, n, x, y, v);
else if (!strcmp(p, "SHIFT"))
shiftXY(cards, n, x, y);
else if (!strcmp(p, "ASC"))
ASC(cards, n, x);
else if (!strcmp(p, "DSC"))
DSC(cards, n, x);
else if (!strcmp(p, "SHOW"))
{
prtWin(cards[x]);
}
}
else
{
c = p[0];
p = strtok(NULL, "-");
// Pentru cazurile B/I/N/G/O
switch (c)
{
case 'B':
if (p[1] != '\0') // transformam nr din char in int
nr = (p[0] - '0') * 10 + (p[1] - '0');
else
nr = p[0] - '0';
for (j = 0; j < n; j++)
hash(cards[j], search(v, 0), nr);
break;
case 'I':
if (p[1] != '\0')
nr = (p[0] - '0') * 10 + (p[1] - '0');
else
nr = p[0] - '0';
for (j = 0; j < n; j++)
hash(cards[j], search(v, 1), nr);
break;
case 'N':
if (p[1] != '\0')
nr = (p[0] - '0') * 10 + (p[1] - '0');
else
nr = p[0] - '0';
for (j = 0; j < n; j++)
hash(cards[j], search(v, 2), nr);
break;
case 'G':
if (p[1] != '\0')
nr = (p[0] - '0') * 10 + (p[1] - '0');
else
nr = p[0] - '0';
for (j = 0; j < n; j++)
hash(cards[j], search(v, 3), nr);
break;
case 'O':
if (p[1] != '\0')
nr = (p[0] - '0') * 10 + (p[1] - '0');
else
nr = p[0] - '0';
for (j = 0; j < n; j++)
hash(cards[j], search(v, 4), nr);
break;
}
}
// Verificam daca s-a facut bingo
d = points(cards, pt, n);
if (d)
return 1;
}
return 0;
}
int main()
{
int n, m, ***cards, i, v[5] = {0, 1, 2, 3, 4};
int d = 0;
scanf("%d", &n); // nr de matrici date
cards = (int ***)calloc(n, sizeof(int **));
// creem matricile in vector
for (i = 0; i < n; i++)
{
cards[i] = creMat(n);
citMat(cards[i]);
}
scanf("%d", &m); // citim nr de comenzi date
d = completare(cards, m, n, v);
if (d)
return 0;
printf("NO WINNER\n");
// eliberam spatiul din memorie
free(cards);
return 0;
}