diff --git a/firstModel/Makefile b/firstModel/Makefile index 0fa8f85..604a6c3 100644 --- a/firstModel/Makefile +++ b/firstModel/Makefile @@ -12,10 +12,10 @@ MAIN= main $(MAIN): $(AI).o $(MAIN).o Makefile $(CXX) $(MAIN).o $(AI).o -o $(MAIN) $(FLAGS) -$(MAIN).o: $(MAIN).c $(AI).c Makefile +$(MAIN).o: $(MAIN).c $(AI).c Makefile $(AI).h $(CXX) -c $(MAIN).c $(FLAGS) -$(AI).o: $(AI).c Makefile +$(AI).o: $(AI).c Makefile $(AI).h $(CXX) -c $(AI).c $(FLAGS) clean: diff --git a/firstModel/exemplo b/firstModel/exemplo new file mode 100755 index 0000000..bd477fe Binary files /dev/null and b/firstModel/exemplo differ diff --git a/firstModel/exemplo.c b/firstModel/exemplo.c index 979fa16..6acd136 100644 --- a/firstModel/exemplo.c +++ b/firstModel/exemplo.c @@ -144,25 +144,33 @@ int main(int argc, char **argv) { int cor; tmapa m; - int semente; + int semente = -1; int steps = 0; - int parada=0; + int parada = 0; - if (argc < 4 || argc > 5) + if ((argc < 4 || argc > 5) && argc != 1) { printf("uso: %s []\n", argv[0]); exit(1); } - - m.nlinhas = atoi(argv[1]); - m.ncolunas = atoi(argv[2]); - m.ncores = atoi(argv[3]); - - if (argc == 5) - semente = atoi(argv[4]); else - semente = -1; - gera_mapa(&m, semente); + { + if (argc == 1) + { + carrega_mapa(&m); + } + else + { + m.nlinhas = atoi(argv[1]); + m.ncolunas = atoi(argv[2]); + m.ncores = atoi(argv[3]); + if (argc == 5) + semente = atoi(argv[4]); + else + semente = -1; + gera_mapa(&m, semente); + } + } mostra_mapa_cor(&m); // for (int i = 0; i < min(m.ncolunas, m.nlinhas) && parada == 0; i++) // { @@ -171,8 +179,8 @@ int main(int argc, char **argv) // steps++; // parada = condicaoDeParada(&m); // } - cor =1; - while (cor > 0 && cor <= m.ncores && parada==0) + cor = 1; + while (cor > 0 && cor <= m.ncores && parada == 0) { scanf("%d", &cor); pinta_mapa(&m, cor); diff --git a/firstModel/flooditAi.c b/firstModel/flooditAi.c index a9172d5..c613184 100644 --- a/firstModel/flooditAi.c +++ b/firstModel/flooditAi.c @@ -11,29 +11,49 @@ bool admissivel = true; bool consistente = true; // -void paint(Board *b, int l, int c, int currentColor, int nextColor) +bool paint(Board *b, int l, int c, int currentColor, int nextColor, bool **painted) { + bool hasChanges = false; + painted[l][c] = true; b->fields[l][c] = nextColor; - if (l < b->lines - 1 && b->fields[l + 1][c] == currentColor) - paint(b, l + 1, c, currentColor, nextColor); - if (l < b->lines - 1 && c < b->columns - 1 && b->fields[l + 1][c + 1] == currentColor) - paint(b, l + 1, c + 1, currentColor, nextColor); - if (c < b->columns - 1 && b->fields[l][c + 1] == currentColor) - paint(b, l, c + 1, currentColor, nextColor); - if (l > 0 && c < b->columns - 1 && b->fields[l - 1][c + 1] == currentColor) - paint(b, l - 1, c + 1, currentColor, nextColor); - if (l > 0 && b->fields[l - 1][c] == currentColor) - paint(b, l - 1, c, currentColor, nextColor); - if (l > 0 && c > 0 && b->fields[l - 1][c - 1] == currentColor) - paint(b, l - 1, c - 1, currentColor, nextColor); - if (c > 0 && b->fields[l][c - 1] == currentColor) - paint(b, l, c - 1, currentColor, nextColor); - if (l < b->lines - 1 && c > 0 && b->fields[l + 1][c - 1] == currentColor) - paint(b, l + 1, c - 1, currentColor, nextColor); + + for (int i = 1; i >= -1; i--) + { + if ((l + i >= 0) && (l + i < b->lines)) + { + for (int j = 1; j >= -1; j--) + { + if (((c + j >= 0) && (c + j < b->columns)) && !(j == 0 && i == 0)) + { + if (b->fields[l + i][c + j] == currentColor) + { + hasChanges = hasChanges || paint(b, l + i, c + j, currentColor, nextColor, painted); + } + if (b->fields[l + i][c + j] == nextColor && !painted[l + i][c + j]) + { + hasChanges = true; + } + } + } + } + } + + return hasChanges; } Board *paint_board(Board *b, int nextColor) { + bool **painted; + + painted = calloc(b->lines + 1, sizeof(bool *)); + for (int i = 0; i < b->lines; i++) + { + painted[i] = calloc(b->columns + 1, sizeof(bool)); + for (int j = 0; j < b->columns; j++) + { + painted[i][j] = false; + } + } if (nextColor == b->fields[0][0]) return NULL; Board *newBoard = calloc(1, sizeof(Board)); @@ -47,8 +67,15 @@ Board *paint_board(Board *b, int nextColor) for (int j = 0; j < b->columns; j++) newBoard->fields[i][j] = b->fields[i][j]; } - paint(newBoard, 0, 0, newBoard->fields[0][0], nextColor); - return newBoard; + bool hasChanges = paint(newBoard, 0, 0, newBoard->fields[0][0], nextColor, painted); + if (hasChanges) + { + return newBoard; + } + else + { + return NULL; + } } //Heuristc @@ -79,7 +106,7 @@ inlineF Neighbor *takeNeighbors(Board *b, int line, int column, bool **checkedFi Neighbor *neighbor = calloc(1, sizeof(Neighbor)); neighbor->color = false; neighbor->searchColor = b->fields[line][column]; - + neighbor->setCounted = false; if (checkedField[line][column] == 0) { searchField(neighbor, b, line, column, checkedField); @@ -92,17 +119,17 @@ void searchField(Neighbor *neighbor, Board *b, int line, int column, bool **chec { if (neighbor->searchColor == b->fields[line][column]) { - if (!checkedField[line][column]) - { - checkedField[line][column] = true; + checkedField[line][column] = true; - for (int i = -1; i < 2; i++) + for (int i = 1; i >= -1; i--) + { + if ((line + i >= 0) && (line + i < b->lines)) { - if ((line + i >= 0) && (line + i <= b->lines)) + for (int j = 1; j >= -1; j--) { - for (int j = -1; j < 2; j++) + if (((column + j >= 0) && (column + j < b->columns)) && !(j == 0 && i == 0)) { - if ((column + j >= 0) && (column + j <= b->columns)) + if (!checkedField[line + i][column + j]) { searchField(neighbor, b, line + i, column + j, checkedField); } @@ -118,15 +145,19 @@ void searchField(Neighbor *neighbor, Board *b, int line, int column, bool **chec } } -int neighborCalculator(Board *b, int numColors) +int neighborCalculator(Board *b, int numColors, int currentNumColors) { //TODO free Neighbor **neighbors bool **checkedField; - checkedField = calloc(b->lines+1, sizeof(bool)); - for (int i = 0; i < b->columns; i++) + checkedField = calloc(b->lines + 1, sizeof(bool *)); + for (int i = 0; i < b->lines; i++) { - checkedField[i] = calloc(b->columns+1, sizeof(bool)); + checkedField[i] = calloc(b->columns + 1, sizeof(bool)); + for (int j = 0; j < b->columns; j++) + { + checkedField[i][j] = false; + } } Neighbor **neighbors = calloc(1, sizeof(Neighbor *)); int neighborsAmount = 0; @@ -147,20 +178,58 @@ int neighborCalculator(Board *b, int numColors) for (int i = 0; i < neighborsAmount; i++) { bool isSubSet = false; + for (int j = 0; (j < neighborsAmount) && !isSubSet; j++) { boolVector colorUnion = neighbors[i]->color | neighbors[j]->color; - if ((colorUnion == neighbors[j]->color)) + + if ((colorUnion == neighbors[j]->color) && (neighbors[i]->color != neighbors[j]->color)) { isSubSet = true; } + else + { + if ((neighbors[i]->color == neighbors[j]->color) && (neighbors[i]->setCounted || neighbors[j]->setCounted)) + { + isSubSet = true; + } + } } if (!isSubSet) + { result++; + // boolVector actualSet = neighbors[i]->color; + // for (int i = 0; i < numColors + 2; i++) + // { + // if (((actualSet >> i) % 2)) + // { + // result++; + // } + // } + } + + neighbors[i]->setCounted = true; } - freeMatrix(neighbors, 1); + freeMatrix(neighbors, 1); // boolVector rootColors = neighbors[0]->color; + // for (int i = 0; i < numColors + 2; i++) + // { + // if (((rootColors >> i) % 2)) + // { + // result--; + // } + // } freeMatrix(checkedField, b->lines); - return (result - 1); + + // boolVector rootColors = neighbors[0]->color; + // for (int i = 0; i < numColors + 2; i++) + // { + // if (((rootColors >> i) % 2)) + // { + // result--; + // } + // } + + return (result + currentNumColors -2); } inlineF int max(int a, int b) @@ -172,18 +241,29 @@ inlineF int max(int a, int b) } int h(Board *b, int numColors, int currentNumColors) { - int n =neighborCalculator(b, numColors); + int n; + n = neighborCalculator(b, numColors, currentNumColors); int c = currentNumColors - 1; - return max(n, c); + + IF_DEBUG + printf("\nn = %d\n", n); + printf("c = %d\n", c); + END_IF_DEBUG; + if(c==0 && n>0){ + printf("ERRO, heuristica inadimissivel"); + return 0; + } + return max(max(n, c), 0); } int *callback(Step *finalStep) { - - int *result = calloc(finalStep->f+1, sizeof(int)); - Step* aux=finalStep; - for(int i=finalStep->f-1; aux->prevStep!=NULL ; i--){ - result[i]=aux->colorStep; - aux= aux->prevStep; + + int *result = calloc(finalStep->f + 1, sizeof(int)); + Step *aux = finalStep; + for (int i = finalStep->f - 1; aux->prevStep != NULL; i--) + { + result[i] = aux->colorStep; + aux = aux->prevStep; } return result; } @@ -197,13 +277,17 @@ void expandNode(Step *step, int gameColors, StepQueue *q) if (colorStep != step->colorStep) { nextSteps[i] = calloc(1, sizeof(Step)); - nextSteps[i]->board = paint_board(step->board, colorStep); - nextSteps[i]->prevStep = step; - nextSteps[i]->colorStep = colorStep; - nextSteps[i]->g = step->g + 1; - nextSteps[i]->h = h(nextSteps[i]->board, gameColors, colorsCalculator(nextSteps[i]->board, gameColors)); - nextSteps[i]->f = nextSteps[i]->g + nextSteps[i]->h; - enqueueStep(nextSteps[i], q); + Board *newBoard = paint_board(step->board, colorStep); + if (newBoard != NULL) + { + nextSteps[i]->board = newBoard; + nextSteps[i]->prevStep = step; + nextSteps[i]->colorStep = colorStep; + nextSteps[i]->g = step->g + 1; + nextSteps[i]->h = h(nextSteps[i]->board, gameColors, colorsCalculator(nextSteps[i]->board, gameColors)); + nextSteps[i]->f = nextSteps[i]->g + nextSteps[i]->h; + enqueueStep(nextSteps[i], q); + } } } freeBoard(step->board); @@ -226,7 +310,7 @@ void enqueueStep(Step *step, StepQueue *q) else { bool enqueued; - for (QueueNode *node = q->first->next; (node->next->next != NULL) && !enqueued; node = node->next) + for (QueueNode *node = q->first->next; (node->next != NULL) && !enqueued; node = node->next) { if ((node->value->f < weight) && (node->next->value->f > weight)) { @@ -280,8 +364,8 @@ Step *dequeueStep(StepQueue *q) void freeBoard(Board *b) { - // freeMatrix(b->fields, b->lines); - // free(b); + freeMatrix(b->fields, b->lines); + free(b); } void freeMatrix(void **m, int lines) { diff --git a/firstModel/flooditAi.h b/firstModel/flooditAi.h index 20f3342..bf13f50 100644 --- a/firstModel/flooditAi.h +++ b/firstModel/flooditAi.h @@ -4,6 +4,10 @@ #include #include +#define IF_DEBUG if (0) { +#define END_IF_DEBUG } + + #define boolVector long int #define bool char #define false 0 @@ -28,6 +32,7 @@ typedef struct Neighbor { int searchColor; boolVector color; + bool setCounted; } Neighbor; typedef struct QueueNode @@ -52,7 +57,7 @@ void freeBoard(Board *b); void freeMatrix(void** m, int lines ); //Find result int colorsCalculator(Board *b, int gameColors); -int neighborCalculator(Board *b, int numColors); +int neighborCalculator(Board *b, int numColors, int currentNumColors); void searchField(Neighbor *neighbor, Board *b, int line, int column, bool **checkedField); int h(Board *b, int numColors, int currentNumColors); int *callback(Step *finalStep); diff --git a/firstModel/main b/firstModel/main index d89452c..2e8c265 100755 Binary files a/firstModel/main and b/firstModel/main differ diff --git a/firstModel/main.c b/firstModel/main.c index 35636cc..836760f 100644 --- a/firstModel/main.c +++ b/firstModel/main.c @@ -7,6 +7,8 @@ * apenas algumas adaptações para utilizar as funções e estruturas criadas para a solução */ + + void createBoard(Board *b, int numColors) { int i, j; @@ -113,9 +115,6 @@ void pinta_mapa(Board* b, int numColors, int cor) int main(int argc, char **argv) { - int num; - int steps = 0; - int parada = 3; Board* firstBoard= calloc(1, sizeof(Board)); int numColors; @@ -150,13 +149,14 @@ int main(int argc, char **argv) Step* aux= dequeueStep( stepQueue);; int tester =0; - char c; while (aux->h > 0){ tester++; - printf("%d ", tester); - if(tester%100==0){ - mostra_mapa_cor((aux)->board,numColors); + if(tester%1 == 0){ + IF_DEBUG // scanf("%c",&c); + mostra_mapa_cor((aux)->board,numColors); + printf("%d",tester); + END_IF_DEBUG } expandNode(aux, numColors, stepQueue); aux = dequeueStep( stepQueue); @@ -172,6 +172,6 @@ int main(int argc, char **argv) printf("%d ",result[i]); } - mostra_mapa_cor(firstBoard, numColors); + // mostra_mapa_cor(firstBoard, numColors); return 0; } \ No newline at end of file diff --git a/firstModel/valoresTestados b/firstModel/valoresTestados index 6b5c98e..1b46231 100644 --- a/firstModel/valoresTestados +++ b/firstModel/valoresTestados @@ -4,4 +4,14 @@ 2 1 3 3 1 2 1 2 1 1 2 3 2 3 2 -h { 1,3} {1,2} {3,2} \ No newline at end of file +h { 1,3} {1,2} {3,2} + + + +1 2 1 7 2 3 4 5 6 +5 5 7 +3 7 7 6 3 +2 1 3 1 2 +5 1 3 3 1 +6 3 1 6 2 +4 1 4 2 7 diff --git a/flooditAi.c b/flooditAi.c index 686c0e7..855677f 100644 --- a/flooditAi.c +++ b/flooditAi.c @@ -18,7 +18,7 @@ FieldListNode *mergeNodes(FieldListNode *root, FieldListNode **affectedNodes, in FieldListNode *newRoot = calloc(1, sizeof(FieldListNode)); newRoot->value = calloc(1, sizeof(FieldNode)); newRoot->value->color = affectedNodes[0]->value->color; - newRoot->value->neighbors = totalNodes; + newRoot->value->neighborsSize = totalNodes; newRoot->value->neighbors = calloc(newRoot->value->neighborsSize, sizeof(FieldNode *)); int realSize = 0; for (int i = 0; i < root->value->neighborsSize; i++) @@ -26,7 +26,7 @@ FieldListNode *mergeNodes(FieldListNode *root, FieldListNode **affectedNodes, in bool isNotAffected = true; for (int j = 0; isNotAffected && (j < MAX_AFFECT_NODE); j++) { - if ((affectedNodes[j] != NULL) && (root->value->neighbors[i] == affectedNodes[j])) + if ((affectedNodes[j] != NULL) && (root->value->neighbors[i] == affectedNodes[j]->value)) { isNotAffected = false; } @@ -51,8 +51,10 @@ FieldListNode *mergeNodes(FieldListNode *root, FieldListNode **affectedNodes, in } } } - newRoot->value->neighbors = realloc(realSize, sizeof(FieldNode *)); + newRoot->value->neighbors = realloc(newRoot->value->neighbors, realSize * sizeof(FieldNode *)); newRoot->value->neighborsSize = realSize; + + return newRoot; } //Create and remap an new board field list @@ -72,9 +74,15 @@ FieldList *paintBoard(FieldList *b, int nextColor) { if (b->first->value->neighbors[i]->color == newColor) { - changeBoard = true; - affectedNodes[affectedSize] = b->first->value->neighbors[i]; - affectedSize++; + for (FieldListNode *node = b->first; node->next != NULL; node = node->next) + { + if (node->value == b->first->value->neighbors[i]) + { + affectedNodes[affectedSize] = node; + changeBoard = true; + affectedSize++; + } + } } } if (!changeBoard) @@ -115,21 +123,32 @@ FieldList *paintBoard(FieldList *b, int nextColor) for (int i = 1; i < b->size; i++) { int newNeighborsSize = 0; - FieldNode **remappedNeighbors = calloc(nodePairs[1][i]->value->neighborsSize, sizeof(FieldNode)); + FieldNode **remappedNeighbors = calloc(nodePairs[0][i]->value->neighborsSize, sizeof(FieldNode)); for (int j = 0; j < nodePairs[0][i]->value->neighborsSize; j++) { bool haveRoot = false; for (int k = 0; k < b->size; k++) { - if (nodePairs[0][i]->value->neighbors[j] == nodePairs[0][k]) + if (nodePairs[0][i]->value->neighbors[j] == nodePairs[0][k]->value) { if (nodePairs[1][i] != nodePairs[1][0]) { - remappedNeighbors[newNeighborsSize] = nodePairs[1][k]; + remappedNeighbors[newNeighborsSize] = nodePairs[1][k]->value; + newNeighborsSize++; + } + else + { + if (!haveRoot) + { + remappedNeighbors[newNeighborsSize] = nodePairs[1][k]->value; + newNeighborsSize++; + } } } } } + + nodePairs[1][i] = realloc(remappedNeighbors, newNeighborsSize * sizeof(FieldNode)); } FieldList *newBoard = calloc(1, sizeof(FieldList)); @@ -194,7 +213,6 @@ int neighborsCalculator(FieldList *b, int gameColors) return colorGroups; } - int max(int a, int b) { if (a > b) @@ -219,7 +237,7 @@ int colorsCalculator(FieldList *fieldList, int gameColorsNumber) } return result; } -int h(FieldListNode *b, int numColors, int currentNumColors) +int h(FieldList *b, int numColors, int currentNumColors) { int n = neighborsCalculator(b, numColors); int c = currentNumColors - 1; @@ -260,11 +278,11 @@ void expandNode(Step *step, int gameColors, StepQueue *q) } else { - freeFieldList(); //TODO freeFieldList + freeFieldList(newBoard); //TODO freeFieldList } } } - freeBoard(step->board); + freeFieldList(step->board); } void enqueueStep(Step *step, StepQueue *q) { @@ -283,7 +301,7 @@ void enqueueStep(Step *step, StepQueue *q) } else { - bool enqueued; + bool enqueued = false; for (QueueNode *node = q->first->next; (node->next->next != NULL) && !enqueued; node = node->next) { if ((node->value->f < weight) && (node->next->value->f > weight)) @@ -292,6 +310,7 @@ void enqueueStep(Step *step, StepQueue *q) node->next = newNode; newNode->next = aux; q->size++; + enqueued = true; } } } @@ -334,4 +353,40 @@ Step *dequeueStep(StepQueue *q) } return NULL; } +} +FieldList* convertBoardToGraph(int **boartM) +{ + FieldList* convertedBoard= calloc(1, sizeOf(FieldList)); + convertedBoard- +} +FieldNode** searchField(int line, int column,int prevColor, FieldNode ***checkedField) +{ + if (neighbor->searchColor == b->fields[line][column]) + { + if (checkedField[line][column]!=NULL) + { + checkedField[line][column] = true; + + int neighborSize=0; + for (int i = -1; i < 2; i++) + { + if ((line + i >= 0) && (line + i < b->lines)) + { + for (int j = -1; j < 2; j++) + { + if ((column + j >= 0) && (column + j < b->columns)) + { + + searchField(neighbor, b, line + i, column + j, checkedField); + } + } + } + } + } + } + else + { + boolVector colorBit = 1 << (b->fields[line][column]); + (neighbor->color) = (neighbor->color) | colorBit; + } } \ No newline at end of file diff --git a/flooditAi.h b/flooditAi.h index bd80cde..1f29e0b 100644 --- a/flooditAi.h +++ b/flooditAi.h @@ -66,7 +66,7 @@ void expandNode(Step *step, int gameColors, StepQueue *q); void enqueueStep(Step *step, StepQueue *q); Step *dequeueStep(StepQueue *q); -// void freeBoard(BoardList *b); +void freeFieldList(FieldList *b); // void freeMatrix(void **m, int lines); // //Find result // int colorsCalculator(BoardList *b, int gameColors); diff --git a/flooditAi.o b/flooditAi.o deleted file mode 100644 index 6e0006b..0000000 Binary files a/flooditAi.o and /dev/null differ diff --git a/main.c b/main.c new file mode 100644 index 0000000..35636cc --- /dev/null +++ b/main.c @@ -0,0 +1,177 @@ +#include +#include +#include +#include "./flooditAi.h" + +/* Mesmas funções utilizadas no exemplo do professor, +* apenas algumas adaptações para utilizar as funções e estruturas criadas para a solução +*/ + +void createBoard(Board *b, int numColors) +{ + int i, j; + srand(time(NULL)); + + b->fields = (int **)malloc(b->lines * sizeof(int *)); + for (i = 0; i < b->lines; i++) + { + b->fields[i] = (int *)malloc(b->columns * sizeof(int)); + for (j = 0; j < b->columns; j++) + b->fields[i][j] = 1 + rand() % numColors; + } +} + + +void loadBoard(Board *b, int* numColors) +{ + int i, j; + + scanf("%d", &(b->lines)); + scanf("%d", &(b->columns)); + scanf("%d", numColors); + b->fields = (int **)malloc(b->lines * sizeof(int *)); + for (i = 0; i < b->lines; i++) + { + b->fields[i] = (int *)malloc(b->columns * sizeof(int)); + for (j = 0; j < b->columns; j++) + scanf("%d", &(b->fields[i][j])); + } +} + + + +void mostra_mapa(Board* b, int numColors) +{ + int i, j; + + printf("%d %d %d\n", b->lines, b->columns, numColors); + for (i = 0; i < b->lines; i++) + { + for (j = 0; j < b->columns; j++) + if (numColors > 10) + printf("%02d ", b->fields[i][j]); + else + printf("%d ", b->fields[i][j]); + printf("\n"); + } +} + +void mostra_mapa_cor(Board* b, int numColors) +{ + int i, j; + char *cor_ansi[] = {"\x1b[0m", + "\x1b[31m", "\x1b[32m", "\x1b[33m", + "\x1b[34m", "\x1b[35m", "\x1b[36m", + "\x1b[37m", "\x1b[30;1m", "\x1b[31;1m", + "\x1b[32;1m", "\x1b[33;1m", "\x1b[34;1m", + "\x1b[35;1m", "\x1b[36;1m", "\x1b[37;1m"}; + + if (numColors > 15) + { + mostra_mapa(b, numColors); + return; + } + printf("\n%d %d %d\n", b->lines, b->columns, numColors); + for (i = 0; i < b->lines; i++) + { + for (j = 0; j < b->columns; j++) + if (numColors > 10) + printf("%s%02d%s ", cor_ansi[b->fields[i][j]], b->fields[i][j], cor_ansi[0]); + else + printf("%s%d%s ", cor_ansi[b->fields[i][j]], b->fields[i][j], cor_ansi[0]); + printf("\n"); + } +} + +void pinta(Board* b, int l, int c, int fundo, int cor) +{ + b->fields[l][c] = cor; + if (l < b->lines - 1 && b->fields[l + 1][c] == fundo) + pinta(b, l + 1, c, fundo, cor); + if (l < b->lines - 1 && c < b->columns - 1 && b->fields[l + 1][c + 1] == fundo) + pinta(b, l + 1, c + 1, fundo, cor); + if (c < b->columns - 1 && b->fields[l][c + 1] == fundo) + pinta(b, l, c + 1, fundo, cor); + if (l > 0 && c < b->columns - 1 && b->fields[l - 1][c + 1] == fundo) + pinta(b, l - 1, c + 1, fundo, cor); + if (l > 0 && b->fields[l - 1][c] == fundo) + pinta(b, l - 1, c, fundo, cor); + if (l > 0 && c > 0 && b->fields[l - 1][c - 1] == fundo) + pinta(b, l - 1, c - 1, fundo, cor); + if (c > 0 && b->fields[l][c - 1] == fundo) + pinta(b, l, c - 1, fundo, cor); + if (l < b->lines - 1 && c > 0 && b->fields[l + 1][c - 1] == fundo) + pinta(b, l + 1, c - 1, fundo, cor); +} + +void pinta_mapa(Board* b, int numColors, int cor) +{ + if (cor == b->fields[0][0]) + return; + pinta(b, 0, 0, b->fields[0][0], cor); +} + +int main(int argc, char **argv) +{ + int num; + int steps = 0; + int parada = 3; + + Board* firstBoard= calloc(1, sizeof(Board)); + int numColors; + if (argc != 1) + { + + if ((argc < 4 || argc > 4)) + { + printf("uso: %s ", argv[0]); + exit(1); + } + + firstBoard->lines = atoi(argv[1]); + firstBoard->columns = atoi(argv[2]); + numColors = atoi(argv[3]); + createBoard(firstBoard, numColors); + }else{ + loadBoard(firstBoard, &numColors); + } + + mostra_mapa_cor(firstBoard, numColors); + + + StepQueue* stepQueue= calloc(1, sizeof(StepQueue)); + stepQueue->size = 0; + Step* firstStep= calloc (1, sizeof(Step)); + firstStep->board = firstBoard; + firstStep->prevStep= NULL; + firstStep->h=1; + firstStep->colorStep= (firstStep->board->fields)[0][0]; + enqueueStep( firstStep,stepQueue); + + Step* aux= dequeueStep( stepQueue);; + int tester =0; + char c; + while (aux->h > 0){ + tester++; + printf("%d ", tester); + if(tester%100==0){ + mostra_mapa_cor((aux)->board,numColors); + // scanf("%c",&c); + } + expandNode(aux, numColors, stepQueue); + aux = dequeueStep( stepQueue); + + } + mostra_mapa_cor((aux)->board,numColors); + + int* result = callback(aux); + + + printf("\nnumero de passos: %d\n", aux->f); + for (int i=0; i < aux->f; i++){ + printf("%d ",result[i]); + } + + mostra_mapa_cor(firstBoard, numColors); + return 0; +} \ No newline at end of file