From f0a4beae8badcf08de30ff67a68fd18accd4057f Mon Sep 17 00:00:00 2001 From: Vytor Calixto <vytorcalixto@gmail.com> Date: Sun, 4 Jun 2017 13:46:33 -0300 Subject: [PATCH] =?UTF-8?q?Add=20fun=C3=A7=C3=A3o=20para=20calcular=20altu?= =?UTF-8?q?ra=20do=20grafo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/filha.c | 9 +++++++++ libs/grafo.c | 34 ++++++++++++++++++++++++++++++++-- libs/grafo.h | 2 ++ libs/jogador.c | 27 ++++++++++++++++----------- libs/no.c | 4 ++++ libs/vertice.c | 1 + libs/vertice.h | 1 + main.c | 2 +- 8 files changed, 66 insertions(+), 14 deletions(-) diff --git a/libs/filha.c b/libs/filha.c index 841081d..29a6673 100644 --- a/libs/filha.c +++ b/libs/filha.c @@ -26,6 +26,7 @@ No primeiroNoFilha(Filha f) { } No n = f->primeiro; f->primeiro = getSucessorNo(f->primeiro); + --f->tamanho; setSucessorNo(n, NULL); setAntecessorNo(n, NULL); return n; @@ -37,6 +38,7 @@ No ultimoNoFilha(Filha f) { } No n = f->ultimo; f->ultimo = getAntecessorNo(f->ultimo); + --f->tamanho; setSucessorNo(n, NULL); setAntecessorNo(n, NULL); return n; @@ -48,7 +50,14 @@ No insereFilha(void *conteudo, Filha f) { if(!novo) return NULL; setConteudo(novo, conteudo); + + if(f->tamanho == 0) { + f->primeiro = novo; + } + setAntecessorNo(novo, f->ultimo); + setSucessorNo(f->ultimo, novo); + ++f->tamanho; return f->ultimo = novo; } diff --git a/libs/grafo.c b/libs/grafo.c index 160a15e..79bbc3a 100644 --- a/libs/grafo.c +++ b/libs/grafo.c @@ -127,6 +127,36 @@ void floodFill(Tblr t, Vertice v, int i, int j){ return; } +#include "filha.h" +void calculaAltura(Grafo g, Lista raiz) { + for(No n = primeiroNoLista(g->vertices); n; n = getSucessorNo(n)) { + Vertice v = (Vertice) getConteudo(n); + v->altura = -1; + } + + Filha fila = constroiFilha(); + for(No n = primeiroNoLista(raiz); n; n = getSucessorNo(n)) { + Vertice v = (Vertice) getConteudo(n); + v->altura = 0; + insereFilha(v, fila); + } + + while(tamanhoFilha(fila) > 0) { + No n = primeiroNoFilha(fila); + if(!n) { + continue; + }; + Vertice v = (Vertice) getConteudo(n); + for(No m = primeiroNoLista(v->filhos); m; m = getSucessorNo(m)) { + Vertice filho = (Vertice) getConteudo(m); + if(filho->altura == -1) { + filho->altura = v->altura+1; + insereFilha(filho, fila); + } + } + } +} + void destroiGrafo(Grafo g) { destroiLista(g->vertices, destroiVertice); free(g); @@ -144,10 +174,10 @@ void grafoParaDot(Grafo g, Lista grupo, FILE* fp) { // Imprime o grafo for(No n = primeiroNoLista(g->vertices); n; n = getSucessorNo(n)) { Vertice pai = (Vertice) getConteudo(n); - fprintf(fp, "\t\"%p\" [label=\"cor=%d,peso=%d\"];\n", pai, pai->cor, pai->peso); + fprintf(fp, "\t\"%p\" [label=\"cor=%d\npeso=%d\naltura=%d\"];\n", pai, pai->cor, pai->peso, pai->altura); for(No m = primeiroNoLista(pai->filhos); m; m = getSucessorNo(m)) { Vertice filho = (Vertice) getConteudo(m); - fprintf(fp, "\t\"%p\" [label=\"cor=%d,peso=%d\"];\n", filho, filho->cor, filho->peso); + fprintf(fp, "\t\"%p\" [label=\"cor=%d\npeso=%d\naltura=%d\"];\n", filho, filho->cor, filho->peso, filho->altura); fprintf(fp, "\t\"%p\" -- \"%p\";\n", pai, filho); } } diff --git a/libs/grafo.h b/libs/grafo.h index ad9a4a2..847f6f7 100644 --- a/libs/grafo.h +++ b/libs/grafo.h @@ -19,6 +19,8 @@ void tabuleiroParaGrafo(Tblr t, Grafo g); void floodFill(Tblr t, Vertice v, int i, int j); +void calculaAltura(Grafo g, Lista raiz); + void destroiGrafo(Grafo g); void grafoParaDot(Grafo g, Lista grupo, FILE* fp); diff --git a/libs/jogador.c b/libs/jogador.c index ba91ac2..310b94e 100644 --- a/libs/jogador.c +++ b/libs/jogador.c @@ -29,8 +29,12 @@ Lista Joga(Grafo g, Lista grupo){ for(No n = primeiroNoLista(coresFilhos); n; n = getSucessorNo(n)) { Vertice v = (Vertice) getConteudo(n); // TODO: tratar empates! - if((v->peso + v->bonus) > (maior->peso + maior->bonus)) { + if((v->bonus) > (maior->bonus)) { maior = v; + } else if(v->bonus == maior->bonus) { + if(v->peso > maior->peso) { + maior = v; + } } } insereLista(maior->cor, jogadas); @@ -46,16 +50,17 @@ Lista Joga(Grafo g, Lista grupo){ // Limpa as coisas destroiLista(filhos, NULL); destroiLista(coresFilhos, destroiVertice); - - // // PARA DEBUG!! Imprime as últimas 10 jogadas em um arquivo - // char str[32]; - // sprintf(str, "./jogada%d.out", counter ); - // FILE* debug = fopen(str, "w+"); - // if(debug) { - // grafoParaDot(g, grupo, debug); - // } - // fclose(debug); - // ++counter; + + calculaAltura(g, grupo); + // PARA DEBUG!! Imprime as últimas 10 jogadas em um arquivo + char str[32]; + sprintf(str, "./jogada%d.out", counter ); + FILE* debug = fopen(str, "w+"); + if(debug) { + grafoParaDot(g, grupo, debug); + } + fclose(debug); + ++counter; } while(tamanhoLista(grupo) < tamanhoLista(g->vertices)); return jogadas; diff --git a/libs/no.c b/libs/no.c index b88cfce..7627f4e 100644 --- a/libs/no.c +++ b/libs/no.c @@ -15,18 +15,22 @@ No criaNo() { } No getSucessorNo(No n) { + if(!n) return NULL; return n->proximo; } void setSucessorNo(No n, No p) { + if(!n) return; n->proximo = p; } No getAntecessorNo(No n) { + if(!n) return NULL; return n->anterior; } void setAntecessorNo(No n, No p) { + if(!n) return; n->anterior = p; } diff --git a/libs/vertice.c b/libs/vertice.c index 35fb0e5..2dd4590 100644 --- a/libs/vertice.c +++ b/libs/vertice.c @@ -9,6 +9,7 @@ Vertice criaVertice() { v->cor = -1; v->peso = 0; v->bonus = 0; + v->altura = -1; v->grupo = false; v->pais = constroiLista(); v->filhos = constroiLista(); diff --git a/libs/vertice.h b/libs/vertice.h index 9ee344e..c740ffc 100644 --- a/libs/vertice.h +++ b/libs/vertice.h @@ -7,6 +7,7 @@ struct Vertice { int cor; int peso; int bonus; + int altura; bool grupo; Lista pais; Lista filhos; diff --git a/main.c b/main.c index 8d59c2b..64059a9 100644 --- a/main.c +++ b/main.c @@ -17,7 +17,7 @@ int main(int argc, char *argv[]) { puts("Erro na leitura do tabuleiro"); return -1; } - + // Se colocar UMA e APENAS UMA coisa depois do main if(argc == 2) { // Calcula e imprime o número mÃnimo e máximo de jogadas -- GitLab