-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprojeto1.cpp
48 lines (43 loc) · 2.16 KB
/
projeto1.cpp
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
#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;
// compile g++ -std=c++11 -O3 -Wall projeto1.cpp -lm ./a.out
int maximizeValue(int X, int Y, vector<vector<int>> pieceValues) { // receives the pointer to the vector
vector<vector<int>> tabela(X + 1, vector<int>(Y + 1, 0)); // vetor de tamanho X+1 que contem vetores de tamanho Y+1
// fills the matrix (bottom-up method)
for (int length = 1; length <= X; length++) { // somatorio
for (int height = 1; height <= Y; height++) { // somatorio
tabela[length][height] = pieceValues[length][height]; // inicializa a posicao com o maior dos vetores anteriores
for (int lengthPiece = 1; lengthPiece < length; lengthPiece++){
tabela[length][height] = max(tabela[length][height], tabela[length - lengthPiece][height] + tabela[lengthPiece][height]);
}
for (int heightPiece = 1; heightPiece < height; heightPiece++){
tabela[length][height] = max(tabela[length][height], tabela[length][height - heightPiece] + tabela[length][heightPiece]);
}
}
}
return tabela[X][Y]; // o resultado optimo e o resultado final da tabela
}
int main() {
int X, Y, n;
scanf("%d %d", &X, &Y); // reads the variables for length and height
scanf("%d", &n); // reads the variable that represents the number of different pieces
if(X < 1 || Y < 1 || n < 1){
return 0;
}
int pieceLength, pieceHeight, piecePrice;
vector<vector<int>> pieceValues(X+1, vector<int>(Y+1, 0));
for (int i = 0; i < n; i++) {
scanf("%d %d %d", &pieceLength, &pieceHeight, &piecePrice); // reads the length, height and price of every piece
if (pieceLength <= X && pieceHeight <= Y){
pieceValues[pieceLength][pieceHeight] = piecePrice;
}
if (pieceHeight <= X && pieceLength <= Y ){
pieceValues[pieceHeight][pieceLength] = piecePrice;
}
}
int result = maximizeValue(X, Y,pieceValues ); // calls the auxiliary function that calculates the best deal
printf("%d\n", result); // prints to the terminal the answer
return 0;
}