Skip to content

Instantly share code, notes, and snippets.

@rogerioagjr
Created May 9, 2016 23:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rogerioagjr/301f78edc160528badc45c69ca168570 to your computer and use it in GitHub Desktop.
Save rogerioagjr/301f78edc160528badc45c69ca168570 to your computer and use it in GitHub Desktop.
Pilha de Dados
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
#define endl '\n'
#define ends ' '
#define MAXN 10100
int n, dado[MAXN][10];
int solve(int pos, int cima){
int baixo=0, best_face=0;
// sabendo a face de cima, descubro a de baixo
if(cima==1) baixo=6;
if(cima==2) baixo=4;
if(cima==3) baixo=5;
if(cima==4) baixo=2;
if(cima==5) baixo=3;
if(cima==6) baixo=1;
// olho qual a melhor face que estará voltada
// para o lado de soma máxima da pilha
for(int i=1; i<=6; i++)
if(i!=cima and i!=baixo)
best_face=max(best_face,dado[pos][i]);
// se o dado olhado for o último, esta face representa
// o lado de maior soma pois só ele constitui sua sub-pilha
if(pos==n) return best_face;
int new_cima=0;
// descubro qual o lado do próximo lado que está voltado apra cima
for(int i=1; i<=6; i++)
if(dado[pos+1][i]==dado[pos][baixo])
new_cima=i;
// e chamo a recursão para o próximo dado
return best_face+solve(pos+1,new_cima);
}
int main(){
ios_base::sync_with_stdio(false), cin.tie(0);
cin >> n;
// leio os lados de cada dado
for(int i=1; i<=n; i++) cin >> dado[i][1] >> dado[i][2] >> dado[i][3] >> dado[i][4] >> dado[i][5] >> dado[i][6];
// e olho qual o melhor lado do primeiro dado para estar para cima
int maior=0;
for(int i=1; i<=6; i++) maior=max(maior,solve(1,i));
// imprimo o valor obtido na melhor recursão
cout << maior << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment