diff --git a/Makefile b/Makefile index a630cdc891b76398f40a8c20afe916127fef6fcf..1cf6acae27c0ce231e755d34089f9f52fe451ba5 100644 --- a/Makefile +++ b/Makefile @@ -1,2 +1,11 @@ -all: - gcc main.c -o main +CFLAGS = -std=c99 -O2 -W -Wall -g + +.PHONY: all clean + +all: main + +main: main.c board.o + $(CC) $(CFLAGS) -o $@ $^ + +clean: + $(RM) *.o diff --git a/board.c b/board.c new file mode 100644 index 0000000000000000000000000000000000000000..0dfb7d0b02ea8993958f8a71e0c286f00f0883cf --- /dev/null +++ b/board.c @@ -0,0 +1,55 @@ +#include <stdio.h> +#include <stdlib.h> +#include <stdbool.h> +#include "board.h" + +struct Cell { + // Cor da célula + int color; + // Se a célula já foi visitada + bool visited; +}; + +struct Board { + int x, y, colors; + Cell *cells; +}; + +Board createBoard() { + Board b = malloc(sizeof(struct Board)); + if(!b) return NULL; + b->x = b->y = b->colors = 0; + return b; +} + +int readBoard(Board b) { + int x, y, colors; + scanf("%d %d %d", &x, &y, &colors); + b->x = x; + b->y = y; + b->colors = colors; + // Aloca um único array em vez de um "array de arrays" + b->cells = malloc(sizeof(struct Cell) * x * y); + if(b->cells == NULL) return 0; + for(int i=0; i < x; ++i) + for(int j=0; j < y; ++j) { + // Para acessar uma matrix [X][Y] em um vetor fazemos i*Y + j + Cell c = malloc(sizeof(struct Cell)); + c->visited = false; + scanf("%d", &(c->color)); + b->cells[i * y + j] = c; + } + return 1; +} + +void printBoard(Board b) { + printf("%d ", b->x); + printf("%d ", b->y); + printf("%d\n", b->colors); + for(int i=0; i < b->x; ++i) { + for(int j=0; j < b->y; ++j) + printf("%d ", b->cells[i * b->y + j]->color); + puts(""); + } + return; +} diff --git a/board.h b/board.h new file mode 100644 index 0000000000000000000000000000000000000000..e8556cbbb648cdbcdb8dace2459e8eb4dd20e473 --- /dev/null +++ b/board.h @@ -0,0 +1,15 @@ +#ifndef _BOARD_ +#define _BOARD_ +#include <stdbool.h> + +typedef struct Cell *Cell; + +typedef struct Board *Board; + +Board createBoard(); + +int readBoard(Board b); + +void printBoard(Board b); + +#endif diff --git a/main.c b/main.c index fbfae3d7af3cdd939e7ad63d670111f02d0fb7d7..085bac5a137e8f4d0f5112d77dc43cfe158732b0 100644 --- a/main.c +++ b/main.c @@ -1,8 +1,17 @@ +#include <stdio.h> #include <stdlib.h> #include <stdio.h> #include <time.h> +#include "board.h" + +int main() { + Board b = createBoard(); + if(!readBoard(b)) { + puts("Erro na leitura do tabuleiro"); + return -1; + } + printBoard(b); -int main(int argc, char const *argv[]) { printf("Ler o tabuleiro (em matriz?)\n"); printf("Enquanto pilha não vazia:\n"); printf("\tPega os adjacentes do grupo (retorna os pesos dos filhos = montar o grafo)\n");