Created
September 30, 2016 18:47
-
-
Save schweigert/016cba42885ddf40ea8fe34fb3ee90c8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <stdlib.h> | |
| int** criaMatriz(int *alunos){ | |
| int i, j, valor,x; | |
| int** matriz; | |
| printf("Digite a quantidade de alunos:\n"); | |
| scanf("%d", alunos); | |
| // FILE *p=fopen("alunos.txt", "r+"); | |
| //abre arquivo | |
| matriz=(int**)malloc((*alunos)*sizeof(int*)); | |
| for(i=0;i< (*alunos);i++){ | |
| matriz[i] = (int*) malloc((*alunos)*sizeof(int)); //cria matriz de adjacencia | |
| for(j=0;j<(*alunos);j++){ | |
| matriz[i][j]=0; | |
| } | |
| } | |
| printf("Digite os valores da matriz para cada posicao (1 existe relacao, 0 nao existe): \n"); | |
| for(i=0;i<(*alunos);i++){ //percorre matriz e mostra | |
| for(j=0;j<(*alunos);j++){ | |
| scanf("%d",&valor); | |
| matriz[i][j]=valor; | |
| printf("posicao %i %i : %d \n", i, j, matriz[i][j]); | |
| } | |
| } | |
| for(i=0;i<(*alunos);i++){ //percorre matriz e mostra | |
| for(j=0;j<(*alunos);j++){ | |
| printf("%d ",matriz[i][j]); | |
| } | |
| printf("\n"); | |
| } | |
| return matriz; | |
| } | |
| void Colorindo(int **matriz, int alunos){ | |
| printf("Alunos: %d\n", alunos); | |
| int* cores = malloc(sizeof(int)*alunos); | |
| puts ("Iniciando coloração"); | |
| int i, j, k; | |
| for (i = 0; i < alunos; i ++){ | |
| cores[i] = 0; | |
| } | |
| for (i = 0; i < alunos; i++){ | |
| printf("Colorindo %d\n", i); | |
| while(1){ | |
| printf("Verificando com cor %d\n", cores[i]); | |
| int validade = 1; | |
| for(j = 0; j < alunos; j++){ | |
| printf("Testando com vetores laterais de %d: %d\n",i ,j); | |
| if (!matriz[i][j]) | |
| continue; | |
| if(cores[i] == cores[j]){ | |
| validade = 0; | |
| break; | |
| } | |
| } | |
| if(validade == 0){ | |
| cores[i]++; | |
| } | |
| if(validade == 1){ | |
| break; | |
| } | |
| } | |
| } | |
| puts("Vetor de cores:"); | |
| for (i = 0; i<alunos; i++){ | |
| printf("%d ", cores[i]); | |
| } | |
| } | |
| int main() | |
| { | |
| int materias, alunos; | |
| int **matriz = criaMatriz(&alunos); | |
| Colorindo(matriz, alunos); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment