int rodaSolucao() { int i, j, first; int retorno; Input teste; qtdThreads = 0; for(i = 0; i<N_THR; i++) { addToStack(i); interesse[i] = -1; } first = getFromStack(); for(i = 0; i< 9; i++) for(j = 0; j< 9; j++) scanf("%c ",&thrInput[first].matriz[i][j]); thrInput[first].x = 0; thrInput[first].y = 0; thrInput[first].id = first; pthread_create(&threadPool[first],NULL,f_thread,&thrInput[first]); while(freeCount < N_THR); // printf("|%d|",freeCount); //printf("total:%d",qtdThreads); if(flag_solucao == 0) printf("Sudoku invalido.\n"); return 0; }
int main() { Stack *stack = stack_create(); assertEqual(true, stack_empty(stack)); stack_push(stack, integerWithNumber(0)); stack_push(stack, integerWithNumber(1)); stack_push(stack, integerWithNumber(2)); stack_push(stack, integerWithNumber(3)); stack_push(stack, integerWithNumber(4)); assertEqual(false, stack_empty(stack)); stack_push(stack, integerWithNumber(5)); assertEqual(5, getFromStack(stack)->number); assertEqual(4, getFromStack(stack)->number); assertEqual(3, getFromStack(stack)->number); assertEqual(2, getFromStack(stack)->number); assertEqual(1, getFromStack(stack)->number); assertEqual(0, getFromStack(stack)->number); assertEqual(true, stack_empty(stack)); return 0; }
void* backtrack_thread(int id) { int i, flag, nextTry = -1; int x = thrInput[id].x; int y = thrInput[id].y; //imprime(thrInput[id].matriz); flag = 1; if(thrInput[id].matriz[x][y] == 'X') { flag = 0; for(i = 1; i<10; i++){ if(testaPossibilidade(thrInput[id].matriz,x,y,i)) { //printf("POSSIVEL: %d",i); /*a thread pega a primeira possibilidade para si mesma para diminuir a quantidade de threads e tenta passar as outras possibilidades para outras threads if(nextTry == -1) { nextTry = i; continue; }*/ // !INICIO DA ZONA CRITICA! int f,k; for (f = 0; f < N_THR-1; f++) { interesse[id] = f; ultimo[f] = id; for (k = 0; k < N_THR && ultimo[f] == id; k++) if (k != id) while (f <= interesse[k] && ultimo[f] == id); } int next = getFromStack(); interesse[id] = -1; // !FIM DA ZONA CRITICA! if(next != -1) { //printf("next:%d\n",next); memcpy(thrInput[next].matriz,thrInput[id].matriz, 81*sizeof(char)); thrInput[next].matriz[x][y] = i+48; thrInput[next].id = next; if(x < 9) { thrInput[next].x = x+1; thrInput[next].y = y; pthread_create(&threadPool[next],NULL,f_thread,&thrInput[next]); } else if(y < 9) { thrInput[next].x = 0; thrInput[next].y = y+1; pthread_create(&threadPool[next],NULL,f_thread,&thrInput[next]); } else { printf("Solucao encontrada:\n"); imprime(thrInput[id].matriz); flag_solucao = 1; } } else { thrInput[id].matriz[x][y] = i+48; if(x < 9) { thrInput[id].x = x+1; thrInput[id].y = y; backtrack_thread(id); } else if(y < 9) { thrInput[id].x = 0; thrInput[id].y = y+1; backtrack_thread(id); } else { printf("Solucao encontrada:\n"); imprime(thrInput[id].matriz); flag_solucao = 1; } //thrInput[id].x = x; //thrInput[id].y = y; thrInput[id].matriz[x][y] = 'X'; } } } } else{ if(x < 9) { thrInput[id].x = x+1; thrInput[id].y = y; backtrack_thread(id); } else if(y < 9) { thrInput[id].x = 0; thrInput[id].y = y+1; backtrack_thread(id); } else { printf("Solucao encontrada:\n"); imprime(thrInput[id].matriz); flag_solucao = 1; } } //pthread_exit((void*)flag); return flag; }