feat: Initial commit
This commit is contained in:
73
Laboratorio 7/Esercizio 2/Diagonali.c
Normal file
73
Laboratorio 7/Esercizio 2/Diagonali.c
Normal file
@@ -0,0 +1,73 @@
|
||||
// Laboratorio 7 - Esercizio 2 - Diagonali.c
|
||||
// Matteo Schiff - s295565
|
||||
|
||||
#include <stdlib.h>
|
||||
#include "Elementi.h"
|
||||
#include "Diagonali.h"
|
||||
|
||||
typedef struct node_s *link;
|
||||
|
||||
typedef struct node_s {
|
||||
Diagonale diag;
|
||||
link next;
|
||||
} node;
|
||||
|
||||
typedef struct diagonali
|
||||
{
|
||||
link head;
|
||||
int N;
|
||||
} *Diagonali;
|
||||
|
||||
link new_node(Diagonale diag, link next) {
|
||||
link t = malloc(sizeof(node));
|
||||
if (t == NULL) {
|
||||
printf("Errore: impossibile allocare memoria");
|
||||
exit(2);
|
||||
}
|
||||
t->diag = diag;
|
||||
t->next = next;
|
||||
|
||||
return t;
|
||||
}
|
||||
|
||||
void free_node(link p) {
|
||||
free(p);
|
||||
}
|
||||
|
||||
/* creatore e distruttore */
|
||||
Diagonali DiagionaliInit() {
|
||||
Diagonali new = malloc(sizeof(struct diagonali));
|
||||
new->head = NULL;
|
||||
new->N = 0;
|
||||
return new;
|
||||
}
|
||||
|
||||
void DiagonaliFree(Diagonali diags) {
|
||||
link p, x;
|
||||
for (p = diags->head, x = diags->head->next; x != NULL; p = x, x = x->next){
|
||||
free_node(p);
|
||||
}
|
||||
free(diags);
|
||||
}
|
||||
|
||||
void DiagonaliInsert(Diagonali diags, Diagonale newDiag) {
|
||||
link nd = new_node(newDiag, diags->head);
|
||||
diags->head = nd;
|
||||
diags->N++;
|
||||
}
|
||||
|
||||
link DiagonaliTraverse(Diagonali diags, link prev, Diagonale ** elem) {
|
||||
link next;
|
||||
|
||||
if (prev == NULL) {
|
||||
next = diags->head;
|
||||
} else {
|
||||
next = prev->next;
|
||||
}
|
||||
|
||||
if (next == NULL)
|
||||
return NULL;
|
||||
|
||||
*elem = &next->diag;
|
||||
return next;
|
||||
}
|
||||
27
Laboratorio 7/Esercizio 2/Diagonali.h
Normal file
27
Laboratorio 7/Esercizio 2/Diagonali.h
Normal file
@@ -0,0 +1,27 @@
|
||||
// Laboratorio 7 - Esercizio 2 - Diagonali.h
|
||||
// Matteo Schiff - s295565
|
||||
|
||||
#ifndef DIAG_H_DEFINED
|
||||
#define DIAG_H_DEFINED
|
||||
#include "Elementi.h"
|
||||
#include <stdbool.h>
|
||||
#define MAX_ELEM 5
|
||||
|
||||
typedef struct node_s *link;
|
||||
typedef struct diagonali *Diagonali;
|
||||
|
||||
typedef struct diagonale {
|
||||
Elemento elementi[MAX_ELEM];
|
||||
int N;
|
||||
float punti;
|
||||
int diff;
|
||||
bool hasFront;
|
||||
bool hasBack;
|
||||
bool hasSeq;
|
||||
} Diagonale;
|
||||
|
||||
Diagonali DiagionaliInit();
|
||||
void DiagonaliFree(Diagonali diags);
|
||||
void DiagonaliInsert(Diagonali diags, Diagonale newDiag);
|
||||
link DiagonaliTraverse(Diagonali diags, link prev, Diagonale ** elem);
|
||||
#endif
|
||||
11
Laboratorio 7/Esercizio 2/Elementi.c
Normal file
11
Laboratorio 7/Esercizio 2/Elementi.c
Normal file
@@ -0,0 +1,11 @@
|
||||
// Laboratorio 7 - Esercizio 2 - Elementi.c
|
||||
// Matteo Schiff - s295565
|
||||
|
||||
#include<stdio.h>
|
||||
#include"Elementi.h"
|
||||
|
||||
Elemento leggiElemento(FILE * fp) {
|
||||
Elemento e;
|
||||
fscanf(fp, "%s %d %d %d %d %d %f %d", e.nome, &e.tipo, &e.dirIngresso, &e.dirUscita, (int *) &e.reqPreced, (int *) &e.finale, &e.valore, &e.diff);
|
||||
return e;
|
||||
}
|
||||
25
Laboratorio 7/Esercizio 2/Elementi.h
Normal file
25
Laboratorio 7/Esercizio 2/Elementi.h
Normal file
@@ -0,0 +1,25 @@
|
||||
// Laboratorio 7 - Esercizio 2 - Elementi.h
|
||||
// Matteo Schiff - s295565
|
||||
|
||||
#ifndef ELEM_H_DEFINED
|
||||
#define ELEM_H_DEFINED
|
||||
#include<stdbool.h>
|
||||
#include<stdio.h>
|
||||
#define MAX_LEN 30
|
||||
|
||||
enum tipo { TRANSIZIONE, INDIETRO, AVANTI };
|
||||
enum direzione { SPALLE, FRONTE };
|
||||
|
||||
typedef struct elemento {
|
||||
char nome[MAX_LEN+1];
|
||||
int tipo;
|
||||
int dirIngresso;
|
||||
int dirUscita;
|
||||
bool reqPreced;
|
||||
bool finale;
|
||||
float valore;
|
||||
int diff;
|
||||
} Elemento;
|
||||
|
||||
Elemento leggiElemento(FILE * fp);
|
||||
#endif
|
||||
20
Laboratorio 7/Esercizio 2/elementi.txt
Normal file
20
Laboratorio 7/Esercizio 2/elementi.txt
Normal file
@@ -0,0 +1,20 @@
|
||||
19
|
||||
ruota 0 1 0 0 0 0.0 1
|
||||
rondata 0 1 0 0 0 0.2 1
|
||||
ribaltata_a_2 0 1 1 0 0 0.25 2
|
||||
back_handspring 0 0 0 0 0 0.4 2
|
||||
front_tuck 2 1 1 0 0 1.75 3
|
||||
front_pike 2 1 1 0 0 2.0 4
|
||||
front_layout 2 1 1 0 0 2.25 5
|
||||
back_tuck 1 0 0 0 0 1.75 3
|
||||
back_pike 1 0 0 0 0 2.0 4
|
||||
back_layout 1 0 0 0 0 2.25 5
|
||||
double_front_tuck 2 1 1 1 0 3.0 6
|
||||
double_front_layout 2 1 1 1 1 4.0 7
|
||||
double_back_tuck 1 0 0 1 0 3.0 6
|
||||
double_back_layout 1 0 0 1 1 4.0 7
|
||||
triple_front_tuck 2 1 1 1 1 6.5 8
|
||||
triple_back_tuck 1 0 0 1 1 6.0 8
|
||||
back_layout_double_twist 1 0 0 1 0 8.0 9
|
||||
back_layout_quad_twist 1 0 0 1 1 12.0 10
|
||||
arabian 1 0 1 0 0 2.5 4
|
||||
189
Laboratorio 7/Esercizio 2/main.c
Normal file
189
Laboratorio 7/Esercizio 2/main.c
Normal file
@@ -0,0 +1,189 @@
|
||||
// Laboratorio 7 - Esercizio 2 - main.c
|
||||
// Matteo Schiff - s295565
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include "Elementi.h"
|
||||
#include "Diagonali.h"
|
||||
|
||||
Elemento *leggiFile(char *filename, int *N)
|
||||
{
|
||||
FILE *fin;
|
||||
if ((fin = fopen(filename, "r")) == NULL)
|
||||
{
|
||||
puts("Impossibile aprire il file");
|
||||
return NULL;
|
||||
}
|
||||
fscanf(fin, "%d", N);
|
||||
Elemento *lista = malloc(*N * sizeof(Elemento));
|
||||
for (int i = 0; i < *N; i++)
|
||||
{
|
||||
lista[i] = leggiElemento(fin);
|
||||
}
|
||||
fclose(fin);
|
||||
return lista;
|
||||
}
|
||||
|
||||
float calcolaPunti(Diagonale diag)
|
||||
{
|
||||
float p = 0;
|
||||
for (int i = 0; i < diag.N; i++)
|
||||
{
|
||||
p += diag.elementi[i].valore;
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
void generaDiagonaleR(Diagonale tmp, Elemento *scelte, int N_scelte, int max_len, int DD, Diagonali diags)
|
||||
{
|
||||
if (tmp.N > 0 && tmp.N <= max_len)
|
||||
{
|
||||
bool el_acr = false;
|
||||
tmp.hasBack = false;
|
||||
tmp.hasFront = false;
|
||||
tmp.hasSeq = false;
|
||||
for (int i = 0; i < tmp.N; i++)
|
||||
{
|
||||
if (tmp.elementi[i].tipo == AVANTI)
|
||||
{
|
||||
el_acr = true;
|
||||
tmp.hasFront = true;
|
||||
}
|
||||
if (tmp.elementi[i].tipo == INDIETRO)
|
||||
{
|
||||
el_acr = true;
|
||||
tmp.hasBack = true;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i<tmp.N-1 && !tmp.hasSeq;i++) {
|
||||
if (tmp.elementi[i].tipo != TRANSIZIONE && tmp.elementi[i+1].tipo != TRANSIZIONE)
|
||||
tmp.hasSeq = true;
|
||||
}
|
||||
|
||||
if (el_acr)
|
||||
{
|
||||
tmp.punti = calcolaPunti(tmp);
|
||||
|
||||
DiagonaliInsert(diags, tmp);
|
||||
}
|
||||
}
|
||||
|
||||
if (tmp.N == max_len || (tmp.N > 0 && tmp.elementi[tmp.N - 1].finale))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 0; i < N_scelte; i++)
|
||||
{
|
||||
// Se devo mettere il primo elemento
|
||||
if (tmp.N == 0)
|
||||
{
|
||||
if (scelte[i].reqPreced == true || scelte[i].dirIngresso == SPALLE)
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (scelte[i].dirIngresso != tmp.elementi[tmp.N - 1].dirUscita)
|
||||
continue;
|
||||
}
|
||||
|
||||
if (tmp.diff + scelte[i].diff > DD)
|
||||
continue;
|
||||
|
||||
tmp.elementi[tmp.N++] = scelte[i];
|
||||
tmp.diff += scelte[i].diff;
|
||||
generaDiagonaleR(tmp, scelte, N_scelte, max_len, DD, diags);
|
||||
tmp.diff -= scelte[i].diff;
|
||||
tmp.N--;
|
||||
}
|
||||
}
|
||||
|
||||
Diagonali generaDiagonaliValide(Elemento *elementi, int N, int DD)
|
||||
{
|
||||
Diagonali set = DiagionaliInit();
|
||||
Diagonale tmp;
|
||||
tmp.diff = 0;
|
||||
tmp.N = 0;
|
||||
|
||||
generaDiagonaleR(tmp, elementi, N, 5, DD, set);
|
||||
|
||||
return set;
|
||||
}
|
||||
|
||||
void trovaProgrammaMiglioreR(Diagonali diag, int c, Diagonale **sel, float *max, Diagonale **res, int DP, int diff)
|
||||
{
|
||||
if (c == 3)
|
||||
{
|
||||
bool hasFront = sel[0]->hasFront || sel[1]->hasFront || sel[2]->hasFront;
|
||||
bool hasBack = sel[0]->hasBack || sel[1]->hasBack || sel[2]->hasBack;
|
||||
bool hasSeq = sel[0]->hasSeq || sel[1]->hasSeq || sel[2]->hasSeq;
|
||||
|
||||
float puntiUltimaDiag = (sel[2]->elementi[sel[2]->N - 1].valore >= 8) ? sel[2]->punti * 1.5 : sel[2]->punti;
|
||||
|
||||
float points = sel[0]->punti + sel[1]->punti + puntiUltimaDiag;
|
||||
if (points > *max && diff <= DP && hasFront && hasBack && hasSeq)
|
||||
{
|
||||
*max = points;
|
||||
res[0] = sel[0];
|
||||
res[1] = sel[1];
|
||||
res[2] = sel[2];
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
link p = NULL;
|
||||
Diagonale *curr;
|
||||
do
|
||||
{
|
||||
p = DiagonaliTraverse(diag, p, &curr);
|
||||
|
||||
if (curr->diff + diff > DP)
|
||||
continue;
|
||||
|
||||
sel[c] = curr;
|
||||
diff += curr->diff;
|
||||
trovaProgrammaMiglioreR(diag, c + 1, sel, max, res, DP, diff);
|
||||
diff -= curr->diff;
|
||||
} while (p != NULL);
|
||||
}
|
||||
|
||||
void stampaDiagonale(Diagonale *diag)
|
||||
{
|
||||
printf("Diagonale: [punti=%f], [difficoltà=%d] -", diag->punti, diag->diff);
|
||||
for (int i = 0; i < diag->N; i++)
|
||||
{
|
||||
printf(" %s", diag->elementi[i].nome);
|
||||
}
|
||||
putchar('\n');
|
||||
}
|
||||
|
||||
void trovaProgrammaMigliore(Diagonali diags, int DP)
|
||||
{
|
||||
float max = 0;
|
||||
Diagonale *sel[3];
|
||||
Diagonale *res[3];
|
||||
trovaProgrammaMiglioreR(diags, 0, sel, &max, res, DP, 0);
|
||||
|
||||
printf("Massimo valore: %f\n", max);
|
||||
stampaDiagonale(res[0]);
|
||||
stampaDiagonale(res[1]);
|
||||
stampaDiagonale(res[2]);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int N = 0, DP, DD;;
|
||||
Elemento *l = leggiFile("elementi.txt", &N);
|
||||
printf("Inserisci DD e DP: ");
|
||||
scanf("%d %d", &DD, &DP);
|
||||
Diagonali diags = generaDiagonaliValide(l, N, DD);
|
||||
|
||||
trovaProgrammaMigliore(diags, DP);
|
||||
|
||||
DiagonaliFree(diags);
|
||||
free(l);
|
||||
return 0;
|
||||
}
|
||||
59
Laboratorio 7/Esercizio 2/testset_risulutati.txt
Normal file
59
Laboratorio 7/Esercizio 2/testset_risulutati.txt
Normal file
@@ -0,0 +1,59 @@
|
||||
--- Test Case #1 ---
|
||||
DD = 10 DP = 20
|
||||
TOT = 17.800
|
||||
DIAG #1 > 1.750
|
||||
front_tuck
|
||||
DIAG #2 > 3.750
|
||||
front_tuck front_pike
|
||||
DIAG #3 > 8.200 * 1.5 (BONUS)
|
||||
rondata back_layout_double_twist
|
||||
|
||||
--- Test Case #2 ---
|
||||
DD = 7 DP = 30
|
||||
TOT = 11.200
|
||||
DIAG #1 > 3.700
|
||||
rondata back_tuck back_tuck
|
||||
DIAG #2 > 3.750
|
||||
front_tuck front_pike
|
||||
DIAG #3 > 3.750
|
||||
front_tuck front_pike
|
||||
|
||||
--- Test Case #3 ---
|
||||
DD = 10 DP = 35
|
||||
TOT = 26.250
|
||||
DIAG #1 > 5.750
|
||||
front_tuck double_front_layout
|
||||
DIAG #2 > 8.200
|
||||
rondata back_layout_double_twist
|
||||
DIAG #3 > 8.200 * 1.5 (BONUS)
|
||||
rondata back_layout_double_twist
|
||||
|
||||
--- Test Case #4 ---
|
||||
DD = 12 DP = 28
|
||||
TOT = 34.000
|
||||
DIAG #1 > 12.200
|
||||
rondata back_layout_quad_twist
|
||||
DIAG #2 > 3.500
|
||||
front_tuck front_tuck
|
||||
DIAG #3 > 12.200 * 1.5 (BONUS)
|
||||
rondata back_layout_quad_twist
|
||||
|
||||
--- Test Case #5 ---
|
||||
DD = 12 DP = 30
|
||||
TOT = 34.950
|
||||
DIAG #1 > 4.450
|
||||
rondata arabian front_tuck
|
||||
DIAG #2 > 12.200
|
||||
rondata back_layout_quad_twist
|
||||
DIAG #3 > 12.200 * 1.5 (BONUS)
|
||||
rondata back_layout_quad_twist
|
||||
|
||||
--- Test Case #6 ---
|
||||
DD = 11 DP = 33
|
||||
TOT = 38.750
|
||||
DIAG #1 > 12.200
|
||||
rondata back_layout_quad_twist
|
||||
DIAG #2 > 8.250
|
||||
front_tuck triple_front_tuck
|
||||
DIAG #3 > 12.200 * 1.5 (BONUS)
|
||||
rondata back_layout_quad_twist
|
||||
Reference in New Issue
Block a user