feat: Initial commit
This commit is contained in:
17
Laboratorio 6/Esercizio 2/easy_test_set.txt
Normal file
17
Laboratorio 6/Esercizio 2/easy_test_set.txt
Normal file
@@ -0,0 +1,17 @@
|
||||
15
|
||||
5 7 4 10
|
||||
6 8 10 1
|
||||
3 1 6 10
|
||||
4 4 2 1
|
||||
7 2 4 10
|
||||
8 8 4 6
|
||||
6 1 4 4
|
||||
8 3 6 4
|
||||
7 7 4 2
|
||||
1 3 7 1
|
||||
10 10 9 10
|
||||
8 8 8 6
|
||||
9 6 10 5
|
||||
6 10 9 5
|
||||
9 6 9 9
|
||||
|
||||
45
Laboratorio 6/Esercizio 2/easy_test_set_result.txt
Normal file
45
Laboratorio 6/Esercizio 2/easy_test_set_result.txt
Normal file
@@ -0,0 +1,45 @@
|
||||
TEST #1
|
||||
zaffiro = 5, rubino = 7, topazio = 4, smeraldo = 10, TOT = 26
|
||||
Collana massima di lunghezza 24
|
||||
TEST #2
|
||||
zaffiro = 6, rubino = 8, topazio = 10, smeraldo = 1, TOT = 25
|
||||
Collana massima di lunghezza 24
|
||||
TEST #3
|
||||
zaffiro = 3, rubino = 1, topazio = 6, smeraldo = 10, TOT = 20
|
||||
Collana massima di lunghezza 16
|
||||
TEST #4
|
||||
zaffiro = 4, rubino = 4, topazio = 2, smeraldo = 1, TOT = 11
|
||||
Collana massima di lunghezza 10
|
||||
TEST #5
|
||||
zaffiro = 7, rubino = 2, topazio = 4, smeraldo = 10, TOT = 23
|
||||
Collana massima di lunghezza 22
|
||||
TEST #6
|
||||
zaffiro = 8, rubino = 8, topazio = 4, smeraldo = 6, TOT = 26
|
||||
Collana massima di lunghezza 23
|
||||
TEST #7
|
||||
zaffiro = 6, rubino = 1, topazio = 4, smeraldo = 4, TOT = 15
|
||||
Collana massima di lunghezza 13
|
||||
TEST #8
|
||||
zaffiro = 8, rubino = 3, topazio = 6, smeraldo = 4, TOT = 21
|
||||
Collana massima di lunghezza 19
|
||||
TEST #9
|
||||
zaffiro = 7, rubino = 7, topazio = 4, smeraldo = 2, TOT = 20
|
||||
Collana massima di lunghezza 18
|
||||
TEST #10
|
||||
zaffiro = 1, rubino = 3, topazio = 7, smeraldo = 1, TOT = 12
|
||||
Collana massima di lunghezza 9
|
||||
TEST #11
|
||||
zaffiro = 10, rubino = 10, topazio = 9, smeraldo = 10, TOT = 39
|
||||
Collana massima di lunghezza 39
|
||||
TEST #12
|
||||
zaffiro = 8, rubino = 8, topazio = 8, smeraldo = 6, TOT = 30
|
||||
Collana massima di lunghezza 30
|
||||
TEST #13
|
||||
zaffiro = 9, rubino = 6, topazio = 10, smeraldo = 5, TOT = 30
|
||||
Collana massima di lunghezza 27
|
||||
TEST #14
|
||||
zaffiro = 6, rubino = 10, topazio = 9, smeraldo = 5, TOT = 30
|
||||
Collana massima di lunghezza 30
|
||||
TEST #15
|
||||
zaffiro = 9, rubino = 6, topazio = 9, smeraldo = 9, TOT = 33
|
||||
Collana massima di lunghezza 31
|
||||
16
Laboratorio 6/Esercizio 2/hard_test_set.txt
Normal file
16
Laboratorio 6/Esercizio 2/hard_test_set.txt
Normal file
@@ -0,0 +1,16 @@
|
||||
15
|
||||
19 6 8 5
|
||||
19 14 5 11
|
||||
14 14 5 8
|
||||
12 16 5 14
|
||||
12 17 20 20
|
||||
13 11 14 18
|
||||
7 14 12 10
|
||||
13 20 12 17
|
||||
17 18 6 9
|
||||
5 5 15 19
|
||||
18 12 20 17
|
||||
13 6 19 6
|
||||
17 20 15 19
|
||||
12 18 17 8
|
||||
7 20 12 7
|
||||
45
Laboratorio 6/Esercizio 2/hard_test_set_result.txt
Normal file
45
Laboratorio 6/Esercizio 2/hard_test_set_result.txt
Normal file
@@ -0,0 +1,45 @@
|
||||
TEST #1
|
||||
zaffiro = 19, rubino = 6, topazio = 8, smeraldo = 5, TOT = 38
|
||||
Collana massima di lunghezza 37
|
||||
TEST #2
|
||||
zaffiro = 19, rubino = 14, topazio = 5, smeraldo = 11, TOT = 49
|
||||
Collana massima di lunghezza 41
|
||||
TEST #3
|
||||
zaffiro = 14, rubino = 14, topazio = 5, smeraldo = 8, TOT = 41
|
||||
Collana massima di lunghezza 33
|
||||
TEST #4
|
||||
zaffiro = 12, rubino = 16, topazio = 5, smeraldo = 14, TOT = 47
|
||||
Collana massima di lunghezza 37
|
||||
TEST #5
|
||||
zaffiro = 12, rubino = 17, topazio = 20, smeraldo = 20, TOT = 69
|
||||
Collana massima di lunghezza 67
|
||||
TEST #6
|
||||
zaffiro = 13, rubino = 11, topazio = 14, smeraldo = 18, TOT = 56
|
||||
Collana massima di lunghezza 54
|
||||
TEST #7
|
||||
zaffiro = 7, rubino = 14, topazio = 12, smeraldo = 10, TOT = 43
|
||||
Collana massima di lunghezza 42
|
||||
TEST #8
|
||||
zaffiro = 13, rubino = 20, topazio = 12, smeraldo = 17, TOT = 62
|
||||
Collana massima di lunghezza 55
|
||||
TEST #9
|
||||
zaffiro = 17, rubino = 18, topazio = 6, smeraldo = 9, TOT = 50
|
||||
Collana massima di lunghezza 39
|
||||
TEST #10
|
||||
zaffiro = 5, rubino = 5, topazio = 15, smeraldo = 19, TOT = 44
|
||||
Collana massima di lunghezza 35
|
||||
TEST #11
|
||||
zaffiro = 18, rubino = 12, topazio = 20, smeraldo = 17, TOT = 67
|
||||
Collana massima di lunghezza 60
|
||||
TEST #12
|
||||
zaffiro = 13, rubino = 6, topazio = 19, smeraldo = 6, TOT = 44
|
||||
Collana massima di lunghezza 32
|
||||
TEST #13
|
||||
zaffiro = 17, rubino = 20, topazio = 15, smeraldo = 19, TOT = 71
|
||||
Collana massima di lunghezza 67
|
||||
TEST #14
|
||||
zaffiro = 12, rubino = 18, topazio = 17, smeraldo = 8, TOT = 55
|
||||
Collana massima di lunghezza 55
|
||||
TEST #15
|
||||
zaffiro = 7, rubino = 20, topazio = 12, smeraldo = 7, TOT = 46
|
||||
Collana massima di lunghezza 39
|
||||
201
Laboratorio 6/Esercizio 2/main.c
Normal file
201
Laboratorio 6/Esercizio 2/main.c
Normal file
@@ -0,0 +1,201 @@
|
||||
// Laboratorio 6 - Esercizio 2
|
||||
// Matteo Schiff - s295565
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int fZ(int z, int r, int t, int s, int len);
|
||||
int fR(int z, int r, int t, int s, int len);
|
||||
int fS(int z, int r, int t, int s, int len);
|
||||
int fT(int z, int r, int t, int s, int len);
|
||||
|
||||
enum Pietra{zaffiro, rubino, topazio, smeraldo};
|
||||
|
||||
int *cache;
|
||||
int mz, mr, mt, ms;
|
||||
|
||||
// Funzioni helper per semplificare la memorizzazione
|
||||
// NOTA se `getCache` restituisce 0 vuol dire che la collana non è ancora stata analizzata
|
||||
int getCache(int p, int z, int r, int t, int s) {
|
||||
return *(cache + p*mz*mr*mt*ms + z*mr*mt*ms + r*mt*ms + t*ms + s);
|
||||
}
|
||||
|
||||
void saveCache(int p, int z, int r, int t, int s, int val) {
|
||||
*(cache + p*mz*mr*mt*ms + z*mr*mt*ms + r*mt*ms + t*ms + s) = val;
|
||||
}
|
||||
|
||||
int fZ(int z, int r, int t, int s, int len) {
|
||||
if (len != 0 && getCache(zaffiro, z,r,t,s) != 0) {
|
||||
return getCache(zaffiro,z,r,t,s);
|
||||
}
|
||||
|
||||
int x = -1;
|
||||
int m;
|
||||
if (z > 0) {
|
||||
m = fZ(z-1,r,t,s,len +1);
|
||||
if (m > x)
|
||||
x = m;
|
||||
}
|
||||
|
||||
if (r > 0) {
|
||||
m = fR(z,r-1,t,s,len +1);
|
||||
if (m > x)
|
||||
x = m;
|
||||
}
|
||||
|
||||
// se non è possibile più aggiungere pietre, ho raggiunto la lunghezza massima ottenibile dal ramo
|
||||
if (x == -1) {
|
||||
saveCache(zaffiro,z,r,t,s,len);
|
||||
return len;
|
||||
}
|
||||
|
||||
// se no restituisco il massimo fin'ora
|
||||
saveCache(zaffiro,z,r,t,s,x);
|
||||
return x;
|
||||
}
|
||||
|
||||
int fR(int z, int r, int t, int s, int len) {
|
||||
if (len != 0 && getCache(rubino, z,r,t,s) != 0) {
|
||||
return getCache(rubino,z,r,t,s);
|
||||
}
|
||||
|
||||
int x = -1;
|
||||
int m;
|
||||
if (s > 0) {
|
||||
m = fS(z,r,t,s-1,len +1);
|
||||
if (m > x)
|
||||
x = m;
|
||||
}
|
||||
|
||||
if (t > 0) {
|
||||
m = fT(z,r,t-1,s,len +1);
|
||||
if (m > x)
|
||||
x = m;
|
||||
}
|
||||
|
||||
// se non è possibile più aggiungere pietre, ho raggiunto la lunghezza massima ottenibile dal ramo
|
||||
if (x == -1) {
|
||||
saveCache(rubino,z,r,t,s,len);
|
||||
return len;
|
||||
}
|
||||
|
||||
// se no restituisco il massimo fin'ora
|
||||
saveCache(rubino,z,r,t,s,x);
|
||||
return x;
|
||||
}
|
||||
|
||||
int fT(int z, int r, int t, int s, int len) {
|
||||
if (len != 0 && getCache(topazio, z,r,t,s) != 0) {
|
||||
return getCache(topazio,z,r,t,s);
|
||||
}
|
||||
|
||||
int x = -1;
|
||||
int m;
|
||||
if (z > 0) {
|
||||
m = fZ(z-1,r,t,s,len +1);
|
||||
if (m > x)
|
||||
x = m;
|
||||
}
|
||||
|
||||
if (r > 0) {
|
||||
m = fR(z,r-1,t,s,len +1);
|
||||
if (m > x)
|
||||
x = m;
|
||||
}
|
||||
|
||||
// se non è possibile più aggiungere pietre, ho raggiunto la lunghezza massima ottenibile dal ramo
|
||||
if (x == -1) {
|
||||
saveCache(topazio,z,r,t,s,len);
|
||||
return len;
|
||||
}
|
||||
|
||||
// se no restituisco il massimo fin'ora
|
||||
saveCache(topazio,z,r,t,s,x);
|
||||
return x;
|
||||
}
|
||||
|
||||
int fS(int z, int r, int t, int s, int len) {
|
||||
if (len != 0 && getCache(smeraldo, z,r,t,s) != 0) {
|
||||
return getCache(smeraldo,z,r,t,s);
|
||||
}
|
||||
|
||||
int x = -1;
|
||||
int m;
|
||||
if (s > 0) {
|
||||
m = fS(z,r,t,s-1,len +1);
|
||||
if (m > x)
|
||||
x = m;
|
||||
}
|
||||
|
||||
if (t > 0) {
|
||||
m = fT(z,r,t-1,s,len +1);
|
||||
if (m > x)
|
||||
x = m;
|
||||
}
|
||||
|
||||
// se non è possibile più aggiungere pietre, ho raggiunto la lunghezza massima ottenibile dal ramo
|
||||
if (x == -1) {
|
||||
saveCache(smeraldo,z,r,t,s,len);
|
||||
return len;
|
||||
}
|
||||
|
||||
// se no restituisco il massimo fin'ora
|
||||
saveCache(smeraldo,z,r,t,s,x);
|
||||
return x;
|
||||
}
|
||||
|
||||
void solveCase(int z, int r, int t, int s) {
|
||||
mz = z+1;
|
||||
mr = r+1;
|
||||
mt = t+1;
|
||||
ms = s+1;
|
||||
cache = calloc(4*mz*mr*mt*ms, sizeof(int));
|
||||
|
||||
int max = z+r+t+s;
|
||||
|
||||
int res = -1, x;
|
||||
|
||||
printf("zaffiri = %d, rubini = %d, topazi = %d, smeraldi = %d; numero pietre = %d\n", z,r,t,s,max);
|
||||
|
||||
x = fZ(z-1,r,t,s,1);
|
||||
if (x > res)
|
||||
res = x;
|
||||
|
||||
x = fR(z,r-1,t,s,1);
|
||||
if (x > res)
|
||||
res = x;
|
||||
|
||||
x = fS(z,r,t,s-1,1);
|
||||
if (x > res)
|
||||
res = x;
|
||||
|
||||
x = fT(z,r,t-1,s,1);
|
||||
if (x > res)
|
||||
res = x;
|
||||
free(cache);
|
||||
|
||||
printf("Collana massima di lunghezza %d\n", res);
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
int main() {
|
||||
int z, r, t, s, N = 0;
|
||||
|
||||
FILE *fp;
|
||||
|
||||
if ((fp = fopen("hard_test_set.txt", "r")) == NULL)
|
||||
{
|
||||
puts("Impossibile aprire il file");
|
||||
return 1;
|
||||
}
|
||||
|
||||
fscanf(fp, "%d", &N);
|
||||
|
||||
for (int i = 0; i < N; i++) {
|
||||
fscanf(fp, " %d %d %d %d", &z, &r, &t, &s);
|
||||
printf("Test case %d\n", i+1);
|
||||
solveCase(z,r,t,s);
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
}
|
||||
Reference in New Issue
Block a user