feat: Initial commit
This commit is contained in:
5
Laboratorio 4/Esercizio 1/grafo.txt
Normal file
5
Laboratorio 4/Esercizio 1/grafo.txt
Normal file
@@ -0,0 +1,5 @@
|
||||
4 4
|
||||
0 1
|
||||
0 2
|
||||
1 2
|
||||
1 3
|
||||
77
Laboratorio 4/Esercizio 1/main.c
Normal file
77
Laboratorio 4/Esercizio 1/main.c
Normal file
@@ -0,0 +1,77 @@
|
||||
// Laboratorio 4 - Esercizio 1
|
||||
// Matteo Schiff - s295565
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#define FILENAME "grafo.txt"
|
||||
|
||||
int checkVertexCover(int *soluzione, int N, int **archi, int E) {
|
||||
for (int i = 0; i < E; i++) {
|
||||
if (soluzione[archi[i][0]] == 0 && soluzione[archi[i][1]] == 0)
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
void vertexCoverR(int pos, int **archi, int E, int *soluzione, int N) {
|
||||
if (pos >= N) {
|
||||
if (checkVertexCover(soluzione, N, archi, E)) {
|
||||
printf("(");
|
||||
for (int i = 0; i < N; i++) {
|
||||
if (soluzione[i] == 1) {
|
||||
printf("%d,", i);
|
||||
}
|
||||
}
|
||||
printf("\b)\n");
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
soluzione[pos] = 0;
|
||||
vertexCoverR(pos+1, archi, E, soluzione, N);
|
||||
soluzione[pos] = 1;
|
||||
vertexCoverR(pos+1, archi, E, soluzione, N);
|
||||
}
|
||||
|
||||
void vertexCovers(int **archi, int N, int E) {
|
||||
int *soluzione = (int *) malloc(N * sizeof(int));
|
||||
vertexCoverR(0, archi, E, soluzione, N);
|
||||
free(soluzione);
|
||||
}
|
||||
|
||||
void leggiGrafo(int ***archi, int *N, int *E) {
|
||||
FILE *fp;
|
||||
|
||||
if ((fp = fopen(FILENAME, "r")) == NULL) {
|
||||
printf("Impossibile aprire il file di input %s", FILENAME);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
fscanf(fp, "%d %d", N, E);
|
||||
*archi = (int **) malloc(*E * sizeof(int *));
|
||||
if (*archi == NULL) {
|
||||
printf("Impossibile allocare memoria");
|
||||
exit(2);
|
||||
}
|
||||
|
||||
for (int i = 0; i < *E; i++) {
|
||||
(*archi)[i] = (int *) malloc(2 * sizeof(int));
|
||||
if ((*archi)[i] == NULL) {
|
||||
printf("Impossibile allocare memoria");
|
||||
exit(2);
|
||||
}
|
||||
fscanf(fp, "%d %d", &(*archi)[i][0], &(*archi)[i][1]);
|
||||
}
|
||||
fclose(fp);
|
||||
}
|
||||
|
||||
int main(int argc, char ** argv)
|
||||
{
|
||||
int **archi, N, E;
|
||||
|
||||
leggiGrafo(&archi, &N, &E);
|
||||
vertexCovers(archi, N, E);
|
||||
free(archi);
|
||||
|
||||
return 0;
|
||||
}
|
||||
3
Laboratorio 4/Esercizio 2/anag1.txt
Normal file
3
Laboratorio 4/Esercizio 2/anag1.txt
Normal file
@@ -0,0 +1,3 @@
|
||||
A0001 Mario Rossi 01/02/1990 ViaMarcoPolo Torino 10129
|
||||
A0002 Lucia Verdi 07/11/1989 ViaTorino Milano 20123
|
||||
A0003 Antonio Neri 19/04/1999 GalleriaTermini Roma 00185
|
||||
2
Laboratorio 4/Esercizio 2/anag2.txt
Normal file
2
Laboratorio 4/Esercizio 2/anag2.txt
Normal file
@@ -0,0 +1,2 @@
|
||||
A0004 Anna Gialli 04/09/1995 FratelliAlinari Firenze 50123
|
||||
A0005 Marco Bianchi 03/07/1993 PiazzaMazzini Napoli 80135
|
||||
192
Laboratorio 4/Esercizio 2/item.c
Normal file
192
Laboratorio 4/Esercizio 2/item.c
Normal file
@@ -0,0 +1,192 @@
|
||||
// Laboratorio 4 - Esercizio 2 - item.c
|
||||
// Matteo Schiff - s295565
|
||||
|
||||
#ifndef ITEM_C_DEFINED
|
||||
#define ITEM_C_DEFINED
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include "item.h"
|
||||
|
||||
int DateGreater(Date a, Date b) {
|
||||
if (a.year > b.year)
|
||||
return true;
|
||||
else if (a.year < b.year)
|
||||
return false;
|
||||
|
||||
if (a.month > b.month)
|
||||
return true;
|
||||
else if (a.month < b.month)
|
||||
return false;
|
||||
|
||||
if (a.day >= b.day)
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
Link newNode(Item val, Link next) {
|
||||
Link t = (Link) malloc(sizeof(Node));
|
||||
if (t == NULL) {
|
||||
printf("Impossibile allocare memoria");
|
||||
exit(2);
|
||||
}
|
||||
t->val = val;
|
||||
t->next = next;
|
||||
|
||||
return t;
|
||||
}
|
||||
|
||||
void freeNode(Link node) {
|
||||
free(node->val);
|
||||
free(node);
|
||||
}
|
||||
|
||||
Item ItemFromString(char *str) {
|
||||
Item item;
|
||||
|
||||
if ((item = (Item) malloc(sizeof(struct item_s))) == NULL) {
|
||||
printf("Impossibile allocare memoria");
|
||||
exit(2);
|
||||
}
|
||||
|
||||
if (sscanf(str, " %5s %50s %50s %d/%d/%d %50s %50s %d",
|
||||
item->code,
|
||||
item->name,
|
||||
item->surname,
|
||||
&item->date.day,
|
||||
&item->date.month,
|
||||
&item->date.year,
|
||||
item->address,
|
||||
item->city,
|
||||
&item->cap
|
||||
) != 9) {
|
||||
free(item);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return item;
|
||||
}
|
||||
|
||||
Item ItemRead(FILE * fp) {
|
||||
Item item;
|
||||
|
||||
if ((item = (Item) malloc(sizeof(struct item_s))) == NULL) {
|
||||
printf("Impossibile allocare memoria");
|
||||
exit(2);
|
||||
}
|
||||
|
||||
if (fscanf(fp, " %5s %50s %50s %d/%d/%d %50s %50s %d",
|
||||
item->code,
|
||||
item->name,
|
||||
item->surname,
|
||||
&item->date.day,
|
||||
&item->date.month,
|
||||
&item->date.year,
|
||||
item->address,
|
||||
item->city,
|
||||
&item->cap
|
||||
) != 9) {
|
||||
free(item);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return item;
|
||||
}
|
||||
|
||||
Key ItemKey(Item val) {
|
||||
return val->code;
|
||||
}
|
||||
|
||||
int ItemEq(Key x, Key y) {
|
||||
return strcmp(x, y) == 0;
|
||||
}
|
||||
|
||||
void ItemPrint(FILE *fp, Item x) {
|
||||
fprintf(fp, "%s %s %s %02d/%02d/%04d %s %s %05d\n",
|
||||
x->code,
|
||||
x->name,
|
||||
x->surname,
|
||||
x->date.day,
|
||||
x->date.month,
|
||||
x->date.year,
|
||||
x->address,
|
||||
x->city,
|
||||
x->cap
|
||||
);
|
||||
}
|
||||
|
||||
Link insertOrderedByDate(Item val, Link head) {
|
||||
Link x, p;
|
||||
|
||||
if (head == NULL || DateGreater(val->date, head->val->date))
|
||||
return newNode(val, head);
|
||||
|
||||
for (p = head, x = head->next; x != NULL && DateGreater(x->val->date, val->date); p = x, x = x->next);
|
||||
p->next = newNode(val, x);
|
||||
|
||||
return head;
|
||||
}
|
||||
|
||||
Item getItemByCode(Link head, Key key) {
|
||||
for (Link t = head; t != NULL; t = t->next) {
|
||||
if (ItemEq(ItemKey(t->val), key))
|
||||
return t->val;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void printList(FILE *fp, Link head) {
|
||||
for (Link t = head; t != NULL; t = t->next) {
|
||||
ItemPrint(fp, t->val);
|
||||
}
|
||||
}
|
||||
|
||||
Item extractCode(Link *headp, Key key) {
|
||||
Link *t, r;
|
||||
Item element = NULL;
|
||||
|
||||
for (t = headp; *t != NULL; t = &((*t)->next)) {
|
||||
if (ItemEq(ItemKey((*t)->val), key)) {
|
||||
element = (*t)->val;
|
||||
// Salvo `r` per poi chiamare free
|
||||
r = *t;
|
||||
// rimuovo il nodo dalla lista
|
||||
*t = (*t)->next;
|
||||
// Faccio free solo del nodo (non dell'item)
|
||||
free(r);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return element;
|
||||
}
|
||||
|
||||
Item extractDates(Link *headp, Date first, Date last) {
|
||||
Link *t, r;
|
||||
Item element = NULL;
|
||||
|
||||
for (t = headp; *t != NULL; t = &((*t)->next)) {
|
||||
if (DateGreater((*t)->val->date, first) && DateGreater(last, (*t)->val->date)) {
|
||||
element = (*t)->val;
|
||||
r = *t;
|
||||
*t = (*t)->next;
|
||||
free(r);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return element;
|
||||
}
|
||||
|
||||
void freeList(Link head) {
|
||||
Link p, x;
|
||||
for (p = head, x = head->next; x != NULL; p = x, x = x->next){
|
||||
freeNode(p);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
55
Laboratorio 4/Esercizio 2/item.h
Normal file
55
Laboratorio 4/Esercizio 2/item.h
Normal file
@@ -0,0 +1,55 @@
|
||||
// Laboratorio 4 - Esercizio 2 - item.h
|
||||
// Matteo Schiff - s295565
|
||||
|
||||
#ifndef ITEM_H_DEFINED
|
||||
#define ITEM_H_DEFINED
|
||||
|
||||
#define CODE_LEN 5
|
||||
#define STR_LEN 50
|
||||
|
||||
typedef struct date_s {
|
||||
int day;
|
||||
int month;
|
||||
int year;
|
||||
} Date;
|
||||
|
||||
typedef struct item_s {
|
||||
char code[CODE_LEN+1];
|
||||
char name[STR_LEN+1];
|
||||
char surname[STR_LEN+1];
|
||||
Date date;
|
||||
char address[STR_LEN+1];
|
||||
char city[STR_LEN+1];
|
||||
int cap;
|
||||
} *Item;
|
||||
|
||||
typedef struct node_s *Link;
|
||||
typedef struct node_s {
|
||||
Item val;
|
||||
Link next;
|
||||
} Node;
|
||||
|
||||
typedef char* Key;
|
||||
|
||||
// Date
|
||||
|
||||
int DateGreater(Date a, Date b);
|
||||
|
||||
// Item
|
||||
|
||||
Item ItemFromString(char *str);
|
||||
Item ItemRead(FILE * fp);
|
||||
Key ItemKey(Item item);
|
||||
int ItemEq(Key x, Key y);
|
||||
void ItemPrint(FILE *fp, Item x);
|
||||
|
||||
// List
|
||||
|
||||
Link insertOrderedByDate(Item val, Link head);
|
||||
Item getItemByCode(Link head, Key key);
|
||||
void printList(FILE *fp, Link head);
|
||||
Item extractCode(Link *headp, Key key);
|
||||
Item extractDates(Link *headp, Date first, Date last);
|
||||
void freeList(Link head);
|
||||
|
||||
#endif
|
||||
220
Laboratorio 4/Esercizio 2/main.c
Normal file
220
Laboratorio 4/Esercizio 2/main.c
Normal file
@@ -0,0 +1,220 @@
|
||||
// Laboratorio 4 - Esercizio 2 - main.c
|
||||
// Matteo Schiff - s295565
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <ctype.h>
|
||||
#include "item.h"
|
||||
#define MAX_LEN 30
|
||||
|
||||
const int MAXL = 100;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
k_codice_tratta,
|
||||
k_partenza,
|
||||
k_destinazione,
|
||||
k_data
|
||||
} corsa_keys;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
r_leggi_file,
|
||||
r_leggi_tastiera,
|
||||
r_ricerca_codice,
|
||||
r_cancella_codice,
|
||||
r_cancella_date,
|
||||
r_stampa,
|
||||
r_fine
|
||||
} t_comandi;
|
||||
|
||||
// Trasforma in lowercase tutti i caratteri di una stringa
|
||||
char *toLower(char *s)
|
||||
{
|
||||
for (char *p = s; *p; p++)
|
||||
*p = tolower(*p);
|
||||
return s;
|
||||
}
|
||||
|
||||
t_comandi leggiComando()
|
||||
{
|
||||
t_comandi c;
|
||||
char cmd[MAXL];
|
||||
char tabella[7][20] = {
|
||||
"leggi_file", "leggi_tastiera", "ricerca_codice", "cancella_codice", "cancella_date", "stampa", "fine"};
|
||||
printf("comando (leggi_file/leggi_tastiera/ricerca_codice");
|
||||
printf("/cancella_codice/cancella_date/stampa/fine): ");
|
||||
scanf("%s", cmd);
|
||||
toLower(cmd);
|
||||
c = r_leggi_file;
|
||||
while (c < 8 && strcmp(cmd, tabella[c]) != 0)
|
||||
c++;
|
||||
return (c);
|
||||
}
|
||||
|
||||
void carica_file(char *argomenti, Link * head)
|
||||
{
|
||||
char filename[MAX_LEN+1];
|
||||
FILE *fp_read;
|
||||
if (sscanf(argomenti, " %30s", filename) != 1)
|
||||
{
|
||||
puts("Sintassi non valida. La sintassi è 'carica <nome file>'");
|
||||
return;
|
||||
}
|
||||
|
||||
puts("Carico il nuovo file...");
|
||||
|
||||
if ((fp_read = fopen(filename, "r")) == NULL)
|
||||
{
|
||||
puts("Impossibile aprire il file");
|
||||
return;
|
||||
}
|
||||
|
||||
while(!feof(fp_read)) {
|
||||
Item nuovo = ItemRead(fp_read);
|
||||
// skip invalid entries
|
||||
if (nuovo != NULL)
|
||||
*head = insertOrderedByDate(nuovo, *head);
|
||||
}
|
||||
|
||||
fclose(fp_read);
|
||||
}
|
||||
|
||||
void carica_tastiera(char *argomenti, Link * head)
|
||||
{
|
||||
Item new = ItemFromString(argomenti);
|
||||
|
||||
if (new == NULL) {
|
||||
puts("Anagrafica non valida");
|
||||
return;
|
||||
}
|
||||
|
||||
*head = insertOrderedByDate(new, *head);
|
||||
}
|
||||
|
||||
void ricerca_codice(char *argomenti, Link * head)
|
||||
{
|
||||
char codice[CODE_LEN+1];
|
||||
if (sscanf(argomenti, " %5s", codice) != 1)
|
||||
{
|
||||
puts("Sintassi non valida. La sintassi è 'ricerca_codice <codice>'");
|
||||
return;
|
||||
}
|
||||
|
||||
Item found = getItemByCode(*head, codice);
|
||||
|
||||
if (found == NULL) {
|
||||
puts("Non è stato trovato nessun elemento");
|
||||
return;
|
||||
}
|
||||
|
||||
ItemPrint(stdout, found);
|
||||
}
|
||||
|
||||
void cancella_codice(char *argomenti, Link * head)
|
||||
{
|
||||
char codice[CODE_LEN+1];
|
||||
if (sscanf(argomenti, " %5s", codice) != 1)
|
||||
{
|
||||
puts("Sintassi non valida. La sintassi è 'cancella_codice <codice>'");
|
||||
return;
|
||||
}
|
||||
|
||||
Item found = extractCode(head, codice);
|
||||
|
||||
if (found == NULL) {
|
||||
puts("Non è stato trovato nessun elemento");
|
||||
return;
|
||||
}
|
||||
|
||||
ItemPrint(stdout, found);
|
||||
free(found);
|
||||
}
|
||||
|
||||
void cancella_date(char *argomenti, Link * head)
|
||||
{
|
||||
Date start, end;
|
||||
if (sscanf(argomenti, " %d/%d/%d %d/%d/%d", &(start.day), &(start.month), &(start.year), &(end.day), &(end.month), &(end.year)) != 6)
|
||||
{
|
||||
puts("Sintassi non valida. La sintassi è 'cancella_date <data inizio> <data fine>'");
|
||||
return;
|
||||
}
|
||||
|
||||
Item found;
|
||||
|
||||
while (found = extractDates(head, start, end), found != NULL) {
|
||||
ItemPrint(stdout, found);
|
||||
free(found);
|
||||
}
|
||||
}
|
||||
|
||||
void stampa_file(char *argomenti, Link * head)
|
||||
{
|
||||
FILE *fp_write;
|
||||
char filename[MAX_LEN+1];
|
||||
if (sscanf(argomenti, " %30s", filename) != 1)
|
||||
{
|
||||
puts("Sintassi non valida. La sintassi è 'stampa <nome file>'");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if ((fp_write = fopen(filename, "w")) == NULL)
|
||||
{
|
||||
puts("Impossibile aprire il file");
|
||||
return;
|
||||
}
|
||||
|
||||
printList(fp_write, *head);
|
||||
fclose(fp_write);
|
||||
}
|
||||
|
||||
void menuParola(Link * head)
|
||||
{
|
||||
t_comandi comando;
|
||||
char argomenti[MAXL];
|
||||
int i, continua = 1;
|
||||
while (continua)
|
||||
{
|
||||
comando = leggiComando();
|
||||
fgets(argomenti, MAXL, stdin); /* resto della riga */
|
||||
switch (comando)
|
||||
{
|
||||
case r_leggi_file:
|
||||
carica_file(argomenti, head);
|
||||
break;
|
||||
case r_leggi_tastiera:
|
||||
carica_tastiera(argomenti, head);
|
||||
break;
|
||||
case r_ricerca_codice:
|
||||
ricerca_codice(argomenti, head);
|
||||
break;
|
||||
case r_cancella_codice:
|
||||
cancella_codice(argomenti, head);
|
||||
break;
|
||||
case r_cancella_date:
|
||||
cancella_date(argomenti, head);
|
||||
break;
|
||||
case r_stampa:
|
||||
stampa_file(argomenti, head);
|
||||
break;
|
||||
case r_fine:
|
||||
continua = 0;
|
||||
break;
|
||||
default:
|
||||
puts("Comando non valido\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
Link head = NULL;
|
||||
|
||||
menuParola(&head);
|
||||
freeList(head);
|
||||
|
||||
return 0;
|
||||
}
|
||||
2
Laboratorio 4/Esercizio 2/out
Normal file
2
Laboratorio 4/Esercizio 2/out
Normal file
@@ -0,0 +1,2 @@
|
||||
A0004 Anna Gialli 04/09/1995 FratelliAlinari Firenze 50123
|
||||
A0002 Lucia Verdi 07/11/1989 ViaTorino Milano 20123
|
||||
17
Laboratorio 4/Esercizio 3/easy_test_set.txt
Normal file
17
Laboratorio 4/Esercizio 3/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 4/Esercizio 3/easy_test_set_result.txt
Normal file
45
Laboratorio 4/Esercizio 3/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 4/Esercizio 3/hard_test_set.txt
Normal file
16
Laboratorio 4/Esercizio 3/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 4/Esercizio 3/hard_test_set_result.txt
Normal file
45
Laboratorio 4/Esercizio 3/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
|
||||
107
Laboratorio 4/Esercizio 3/main.c
Normal file
107
Laboratorio 4/Esercizio 3/main.c
Normal file
@@ -0,0 +1,107 @@
|
||||
// Laboratorio 4 - Esercizio 3 - main.c
|
||||
// Matteo Schiff - s295565
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
enum Pietra{zaffiro, rubino, topazio, smeraldo};
|
||||
|
||||
// Esploro il set di collane valide in modo ordinato, quando arrivo alla fine della collana ne controllo
|
||||
// la lunghezza e aggiorno se necessario la soluzione
|
||||
void esploraSoluzioni(int z, int r, int t, int s, int len, int * tmp, int * max, int * sol) {
|
||||
int added = false;
|
||||
if (z > 0 && (len == 0 || (tmp[len - 1] == zaffiro || tmp[len - 1] == topazio))) {
|
||||
tmp[len] = zaffiro;
|
||||
esploraSoluzioni(z-1,r,t,s,len +1,tmp,max,sol);
|
||||
added = true;
|
||||
}
|
||||
|
||||
if (s > 0 && (len == 0 || (tmp[len - 1] == smeraldo || tmp[len - 1] == rubino))) {
|
||||
tmp[len] = smeraldo;
|
||||
esploraSoluzioni(z,r,t,s-1,len +1,tmp,max,sol);
|
||||
added = true;
|
||||
}
|
||||
|
||||
if (r > 0 && (len == 0 || (tmp[len - 1] == zaffiro || tmp[len - 1] == topazio))) {
|
||||
tmp[len] = rubino;
|
||||
esploraSoluzioni(z,r-1,t,s,len +1,tmp,max,sol);
|
||||
added = true;
|
||||
}
|
||||
|
||||
if (t > 0 && (len == 0 || (tmp[len - 1] == smeraldo || tmp[len - 1] == rubino))) {
|
||||
tmp[len] = topazio;
|
||||
esploraSoluzioni(z,r,t-1,s,len +1,tmp,max,sol);
|
||||
added = true;
|
||||
}
|
||||
|
||||
// se non è possibile più aggiungere pietre, ho raggiunto la lunghezza massima ottenibile dal ramo
|
||||
if (!added) {
|
||||
if (len > *max) {
|
||||
*max = len;
|
||||
for (int i = 0; i < len; i++) {
|
||||
sol[i] = tmp[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void solveCase(int z, int r, int t, int s) {
|
||||
int maxL = z+r+t+s;
|
||||
int max = 0;
|
||||
// vettore su cui la funzione costruisce le collane
|
||||
int * tmp = malloc(maxL * sizeof(int));
|
||||
// vettore utilizzato per memorizzare la soluzione
|
||||
int * sol = malloc(maxL * sizeof(int));
|
||||
|
||||
printf("zaffiri = %d, rubini = %d, topazi = %d, smeraldi = %d; numero pietre = %d\n", z,r,t,s,maxL);
|
||||
|
||||
esploraSoluzioni(z,r,t,s,0,tmp,&max,sol);
|
||||
|
||||
printf("Collana massima di lunghezza %d\n", max);
|
||||
for(int i = 0; i < max; i++) {
|
||||
switch (sol[i])
|
||||
{
|
||||
case zaffiro:
|
||||
printf("z ");
|
||||
break;
|
||||
case rubino:
|
||||
printf("r ");
|
||||
break;
|
||||
case smeraldo:
|
||||
printf("s ");
|
||||
break;
|
||||
case topazio:
|
||||
printf("t ");
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
putc('\n', stdout);
|
||||
fflush(stdout);
|
||||
free(tmp);
|
||||
free(sol);
|
||||
}
|
||||
|
||||
int main() {
|
||||
int z, r, t, s, N = 0;
|
||||
|
||||
FILE *fp;
|
||||
|
||||
if ((fp = fopen("easy_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);
|
||||
}
|
||||
7
Laboratorio 4/Esercizio 3/very_easy_test_set.txt
Normal file
7
Laboratorio 4/Esercizio 3/very_easy_test_set.txt
Normal file
@@ -0,0 +1,7 @@
|
||||
6
|
||||
2 1 2 2
|
||||
3 2 3 3
|
||||
6 2 2 3
|
||||
6 4 4 3
|
||||
4 3 7 4
|
||||
6 5 9 7
|
||||
3
Laboratorio 4/Esercizio 3/very_very_easy_test_set.txt
Normal file
3
Laboratorio 4/Esercizio 3/very_very_easy_test_set.txt
Normal file
@@ -0,0 +1,3 @@
|
||||
1
|
||||
2 1 2 2
|
||||
|
||||
122
Laboratorio 4/Esercizio 4/main.c
Normal file
122
Laboratorio 4/Esercizio 4/main.c
Normal file
@@ -0,0 +1,122 @@
|
||||
// Laboratorio 4 - Esercizio 4 - item.c
|
||||
// Matteo Schiff - s295565
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
enum Pietra{zaffiro, rubino, topazio, smeraldo};
|
||||
|
||||
// Ho messo queste variabili globali per diminuire il numero di parametri
|
||||
// di `esploraSoluzioni` per migliorarne la leggibilità
|
||||
// Questi numeri sono costanti per ogni set
|
||||
int vz, vr, vt, vs, max_rip;
|
||||
|
||||
void esploraSoluzioni(int z, int r, int t, int s, int nz, int ns, int cur_rip, int len, int val, int * tmp, int * max, int*maxlen, int * sol) {
|
||||
int added = false;
|
||||
if (z > 0 && (nz +1) <= ns) {
|
||||
tmp[len] = zaffiro;
|
||||
if (len == 0 || tmp[len - 1] == topazio) {
|
||||
esploraSoluzioni(z-1,r,t,s,nz+1,ns,1,len +1,val+vz,tmp,max,maxlen,sol);
|
||||
added = true;
|
||||
}
|
||||
if (len != 0 && tmp[len - 1] == zaffiro && cur_rip < max_rip) {
|
||||
esploraSoluzioni(z-1,r,t,s,nz+1,ns,cur_rip+1,len +1,val+vz,tmp,max,maxlen,sol);
|
||||
added = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (s > 0) {
|
||||
tmp[len] = smeraldo;
|
||||
if (len == 0 || tmp[len - 1] == rubino) {
|
||||
esploraSoluzioni(z,r,t,s-1,nz,ns+1,1,len +1,val+vs,tmp,max,maxlen,sol);
|
||||
added = true;
|
||||
}
|
||||
if (len != 0 && tmp[len - 1] == smeraldo && cur_rip < max_rip) {
|
||||
esploraSoluzioni(z,r,t,s-1,nz,ns+1,cur_rip+1,len +1,val+vs,tmp,max,maxlen,sol);
|
||||
added = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (r > 0 && (len == 0 || (tmp[len - 1] == zaffiro || tmp[len - 1] == topazio))) {
|
||||
tmp[len] = rubino;
|
||||
esploraSoluzioni(z,r-1,t,s,nz,ns,1,len +1,val+vr,tmp,max,maxlen,sol);
|
||||
added = true;
|
||||
}
|
||||
|
||||
if (t > 0 && (len == 0 || (tmp[len - 1] == smeraldo || tmp[len - 1] == rubino))) {
|
||||
tmp[len] = topazio;
|
||||
esploraSoluzioni(z,r,t-1,s,nz,ns,1,len +1,val+vt,tmp,max,maxlen,sol);
|
||||
added = true;
|
||||
}
|
||||
|
||||
// se non è possibile più aggiungere pietre, ho raggiunto la lunghezza massima ottenibile dal ramo
|
||||
if (!added) {
|
||||
if (val >= *max) {
|
||||
*max = val;
|
||||
*maxlen = len;
|
||||
for (int i = 0; i < len; i++) {
|
||||
sol[i] = tmp[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void solveCase(int z, int r, int t, int s) {
|
||||
int maxL = z+r+t+s;
|
||||
int max = 0;
|
||||
int maxlen = 0;
|
||||
int * tmp = malloc(maxL * sizeof(int));
|
||||
int * sol = malloc(maxL * sizeof(int));
|
||||
|
||||
printf("zaffiri = %d (valore: %d), rubini = %d (valore: %d), topazi = %d (valore: %d), smeraldi = %d (valore: %d); numero pietre = %d {max_rip = %d}\n", z, vz,r,vr,t,vt,s,vs,maxL, max_rip);
|
||||
|
||||
esploraSoluzioni(z,r,t,s,0,0,0,0,0,tmp,&max,&maxlen,sol);
|
||||
|
||||
printf("Collana con valore massimo %d, lunghezza %d\n", max, maxlen);
|
||||
for(int i = 0; i < maxlen; i++) {
|
||||
switch (sol[i])
|
||||
{
|
||||
case zaffiro:
|
||||
printf("z ");
|
||||
break;
|
||||
case rubino:
|
||||
printf("r ");
|
||||
break;
|
||||
case smeraldo:
|
||||
printf("s ");
|
||||
break;
|
||||
case topazio:
|
||||
printf("t ");
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
putc('\n', stdout);
|
||||
fflush(stdout);
|
||||
free(tmp);
|
||||
free(sol);
|
||||
}
|
||||
|
||||
int main() {
|
||||
int z, r, t, s, N = 0;
|
||||
|
||||
FILE *fp;
|
||||
|
||||
if ((fp = fopen("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 %d %d %d %d %d", &z, &r, &t, &s, &vz, &vr, &vt, &vs, &max_rip);
|
||||
printf("Test case %d\n", i+1);
|
||||
solveCase(z,r,t,s);
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
}
|
||||
21
Laboratorio 4/Esercizio 4/test_set.txt
Normal file
21
Laboratorio 4/Esercizio 4/test_set.txt
Normal file
@@ -0,0 +1,21 @@
|
||||
20
|
||||
8 1 10 5 6 6 20 8 3
|
||||
6 5 7 2 2 15 2 21 5
|
||||
10 7 8 10 17 16 18 17 3
|
||||
7 1 9 2 10 13 16 20 5
|
||||
6 6 8 10 8 25 20 10 7
|
||||
5 6 1 5 21 7 13 1 9
|
||||
9 5 3 8 7 3 10 8 6
|
||||
7 8 6 8 5 5 12 15 5
|
||||
4 8 8 6 1 19 13 19 1
|
||||
9 3 9 9 17 11 3 5 7
|
||||
1 1 5 10 15 12 14 15 6
|
||||
2 9 3 5 19 14 6 19 3
|
||||
4 5 2 7 21 10 16 5 10
|
||||
5 6 9 6 20 14 3 17 4
|
||||
9 8 9 2 17 5 21 11 9
|
||||
7 1 3 6 25 19 3 20 8
|
||||
6 2 5 4 19 22 3 11 8
|
||||
9 3 5 3 3 1 2 24 9
|
||||
5 10 7 9 19 1 6 5 1
|
||||
4 8 2 10 13 1 8 23 3
|
||||
80
Laboratorio 4/Esercizio 4/test_set_results.txt
Normal file
80
Laboratorio 4/Esercizio 4/test_set_results.txt
Normal file
@@ -0,0 +1,80 @@
|
||||
TEST #1
|
||||
zaffiro = 8 [6], rubino = 1 [6], topazio = 10 [20], smeraldo = 5 [8], TOT = 24 {max_rip = 3}
|
||||
Soluzione ottima di valore 116 usando 13 gemma/e
|
||||
Composizione collana: sstzzrssstzzz
|
||||
TEST #2
|
||||
zaffiro = 6 [2], rubino = 5 [15], topazio = 7 [2], smeraldo = 2 [21], TOT = 20 {max_rip = 5}
|
||||
Soluzione ottima di valore 133 usando 15 gemma/e
|
||||
Composizione collana: trtrtrtrtrsstzz
|
||||
TEST #3
|
||||
zaffiro = 10 [17], rubino = 7 [16], topazio = 8 [18], smeraldo = 10 [17], TOT = 35 {max_rip = 3}
|
||||
Soluzione ottima di valore 596 usando 35 gemma/e
|
||||
Composizione collana: trtrtrtrstzrssstzzzrssstzzzrssstzzz
|
||||
TEST #4
|
||||
zaffiro = 7 [10], rubino = 1 [13], topazio = 9 [16], smeraldo = 2 [20], TOT = 19 {max_rip = 5}
|
||||
Soluzione ottima di valore 105 usando 7 gemma/e
|
||||
Composizione collana: trsstzz
|
||||
TEST #5
|
||||
zaffiro = 6 [8], rubino = 6 [25], topazio = 8 [20], smeraldo = 10 [10], TOT = 30 {max_rip = 7}
|
||||
Soluzione ottima di valore 438 usando 29 gemma/e
|
||||
Composizione collana: trtrtrtrtrssstzzzrssssssstzzz
|
||||
TEST #6
|
||||
zaffiro = 5 [21], rubino = 6 [7], topazio = 1 [13], smeraldo = 5 [1], TOT = 17 {max_rip = 9}
|
||||
Soluzione ottima di valore 137 usando 13 gemma/e
|
||||
Composizione collana: rssssstzzzzzr
|
||||
TEST #7
|
||||
zaffiro = 9 [7], rubino = 5 [3], topazio = 3 [10], smeraldo = 8 [8], TOT = 25 {max_rip = 6}
|
||||
Soluzione ottima di valore 162 usando 23 gemma/e
|
||||
Composizione collana: rtrsstzzrsssssstzzzzzzr
|
||||
TEST #8
|
||||
zaffiro = 7 [5], rubino = 8 [5], topazio = 6 [12], smeraldo = 8 [15], TOT = 29 {max_rip = 5}
|
||||
Soluzione ottima di valore 262 usando 28 gemma/e
|
||||
Composizione collana: rtrtrtrtrsstzzrssssstzzzzzrs
|
||||
TEST #9
|
||||
zaffiro = 4 [1], rubino = 8 [19], topazio = 8 [13], smeraldo = 6 [19], TOT = 26 {max_rip = 1}
|
||||
Soluzione ottima di valore 374 usando 26 gemma/e
|
||||
Composizione collana: rtrtrstzrstzrstzrstzrstrst
|
||||
TEST #10
|
||||
zaffiro = 9 [17], rubino = 3 [11], topazio = 9 [3], smeraldo = 9 [5], TOT = 30 {max_rip = 7}
|
||||
Soluzione ottima di valore 243 usando 25 gemma/e
|
||||
Composizione collana: trtrsstzzrssssssstzzzzzzz
|
||||
TEST #11
|
||||
zaffiro = 1 [15], rubino = 1 [12], topazio = 5 [14], smeraldo = 10 [15], TOT = 17 {max_rip = 6}
|
||||
Soluzione ottima di valore 205 usando 14 gemma/e
|
||||
Composizione collana: sssstzrsssssst
|
||||
TEST #12
|
||||
zaffiro = 2 [19], rubino = 9 [14], topazio = 3 [6], smeraldo = 5 [19], TOT = 19 {max_rip = 3}
|
||||
Soluzione ottima di valore 207 usando 14 gemma/e
|
||||
Composizione collana: rtrtrsstzzrsss
|
||||
TEST #13
|
||||
zaffiro = 4 [21], rubino = 5 [10], topazio = 2 [16], smeraldo = 7 [5], TOT = 18 {max_rip = 10}
|
||||
Soluzione ottima di valore 181 usando 16 gemma/e
|
||||
Composizione collana: rtrsssstzzzzrsss
|
||||
TEST #14
|
||||
zaffiro = 5 [20], rubino = 6 [14], topazio = 9 [3], smeraldo = 6 [17], TOT = 26 {max_rip = 4}
|
||||
Soluzione ottima di valore 307 usando 24 gemma/e
|
||||
Composizione collana: trtrtrtrtrsstzzrsssstzzz
|
||||
TEST #15
|
||||
zaffiro = 9 [17], rubino = 8 [5], topazio = 9 [21], smeraldo = 2 [11], TOT = 28 {max_rip = 9}
|
||||
Soluzione ottima di valore 285 usando 21 gemma/e
|
||||
Composizione collana: trtrtrtrtrtrtrtrsstzz
|
||||
TEST #16
|
||||
zaffiro = 7 [25], rubino = 1 [19], topazio = 3 [3], smeraldo = 6 [20], TOT = 17 {max_rip = 8}
|
||||
Soluzione ottima di valore 295 usando 15 gemma/e
|
||||
Composizione collana: trsssssstzzzzzz
|
||||
TEST #17
|
||||
zaffiro = 6 [19], rubino = 2 [22], topazio = 5 [3], smeraldo = 4 [11], TOT = 17 {max_rip = 8}
|
||||
Soluzione ottima di valore 173 usando 13 gemma/e
|
||||
Composizione collana: trtrsssstzzzz
|
||||
TEST #18
|
||||
zaffiro = 9 [3], rubino = 3 [1], topazio = 5 [2], smeraldo = 3 [24], TOT = 20 {max_rip = 9}
|
||||
Soluzione ottima di valore 92 usando 13 gemma/e
|
||||
Composizione collana: trtrtrssstzzz
|
||||
TEST #19
|
||||
zaffiro = 5 [19], rubino = 10 [1], topazio = 7 [6], smeraldo = 9 [5], TOT = 31 {max_rip = 1}
|
||||
Soluzione ottima di valore 185 usando 28 gemma/e
|
||||
Composizione collana: rstzrstzrstzrstzrstzrstrstrs
|
||||
TEST #20
|
||||
zaffiro = 4 [13], rubino = 8 [1], topazio = 2 [8], smeraldo = 10 [23], TOT = 24 {max_rip = 3}
|
||||
Soluzione ottima di valore 278 usando 18 gemma/e
|
||||
Composizione collana: rssstzzzrssstzrsss
|
||||
Reference in New Issue
Block a user