// Pre-condition: perm stores a permutation of queens from index 0 to location-1 // that is valid for a set of location number of non-conflicting // queens. location represents the column we are placing the next // queen, and usedList keeps track of the rows in which queens // have already been placed. void solveItRec(int perm[], int location, int usedList[]) { int i; // We've found a solution to the problem, so print it! if (location == SIZE) { printSol(perm); } // Loop through possible locations for the next queen to place. for (i=0; i<SIZE; i++) { // Only try this row if it hasn't already been used. if (usedList[i] == 0) { // We can actually place this particular queen without conflict! if (!conflict(perm, location, i)) { // Place the new queen! perm[location] = i; // We've used this row now, so mark that. usedList[i] = 1; // Recursively solve this board. solveItRec(perm, location+1, usedList); // Unselect this square, so that we can continue trying to // fill it with the next possible choice. usedList[i] = 0; } } } }
void solveQ(int queen) { int **board; board = (int**)malloc(sizeof(int*)*queen); for(int i = 0;i<queen;i++) { board[i] = (int*)malloc(sizeof(int)*queen); } for(int i =0;i<queen;i++) { for(int j = 0;j<queen;j++) { board[i][j] = 0; } } if(solveQUtil(board , 0 , queen) == false) { printf("solution is not present\n"); } else { printSol(board, queen); } }
int main(int argc,char** argv) { int rank,np; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Comm_size(MPI_COMM_WORLD, &np); double epsilon; int n; if(argc < 3) { printf("Not enough arguments, using default values for epsilon and n\n"); epsilon = pow(2,-30); n = 100; }else { double epsilon = atof(argv[1]); int n = atoi(argv[2]); } double startTime; if(rank == 0) { startTime = MPI_Wtime(); } double start = 0.0; double end = 1.0; while(end-start > epsilon) { double width = (end-start); start = start +(rank*width)/np; end = start + width/np; double a = f(start,n); double b = f(end,n); if( (a > 0) != (b > 0) ) { int i; for(i=0; i<np; i++) { if(i != rank) { double tmp[2]; tmp[0] = start; tmp[1] = end; MPI_Send(tmp, 2, MPI_DOUBLE, i, 0, MPI_COMM_WORLD); } } }else { double tmp[2]; MPI_Status status; MPI_Recv(tmp, 2, MPI_DOUBLE, MPI_ANY_SOURCE, 0, MPI_COMM_WORLD, &status); start = tmp[0]; end = tmp[1]; } } if(rank == 0) { double stopTime = MPI_Wtime(); printSol(start,end); printf("This took %.5f seconds\n", stopTime-startTime); } MPI_Finalize(); return 0; }
void Transfairs::encontraSolucao() { chegadaMaisTarde = getEarlierArriveTime(); calculaKruskal_R(); dfsCalc(); partidaMaisTarde = chegadaMaisTarde - calcTempViagem(); if ((numPassageirosTotal() > capacidadeVan)) { cout << "\nExcesso de Passageiro para utilizar apenas a uma carrinha/viagem\n" << endl; //dfsCalcPRIM_case2(); //printSol(solucao_P2); dfsCalcPRIM_case3(); for (int i = 0 ; i < solucao_P3.size(); i++) { printSol(solucao_P3[i]); } } else { calculaPrim(); dfsCalcPRIM(); if (solucao_P.size() == (numServices()+2)) { cout << "\nSituação resolvida com apenas uma carrinha.\n" << endl; printSol(solucao_P); } else { cout << "\nViagem longa demais. É necessário mais do que uma viagem\n" << endl <<endl; //dfsCalcPRIM_case2(); //printSol(solucao_P2); } } }
void nQueens(int i, int n, int *x){ int j; if(i>n){ printSol(x,n); // exit(0); } for(j=1; j<=n; j++){ if(place(i,j,x)){ x[i] = j; nQueens(i+1,n,x); } } }
void solveKt () { unsigned int i,j; for (i=0;i<8;i++) for (j=0;j<8;j++) sol[i][j] = -1; if (solveKtUtil (0, 0, 0) == 0) { printf ("no solution \n"); } else printSol(); }
void main() { int i; int G[V][V] = { {0,1,1,1}, {1,0,1,0}, {1,1,1,1}, {1,0,1,0}}; int C[V]; for (i = 0; i < V; i++) C[i] = 0; int mC = 3; if ( gColor(G, C, mC, 0) == 1) { printSol(C); } else { printf("Not possible to color with %d colors.\n", mC); } }
void dijkstra (unsigned int nV, int s) { unsigned int i , j, u, v; unsigned int *visited = calloc (1, sizeof (int)); unsigned int *dist = calloc (nV, sizeof (int)); /** set distances as max and visited as zero */ for (i=1;i<=nV;i++) { visited[i] = 0; dist[i] = DIST_MIN; } /** mark distance of source vertex from itself as zero */ dist[s] = 0; /** Run 1st loop to calculate shortest path for all vertices using source as source * Run 2nd loop for neighbors for the vertex selected above */ for (i=1; i<=nV;i++) { /** Select the vertex with min distance */ u = min(s, nV, visited, dist); visited[u] = 1; //printf ("min is %u\n",u); /** traverse the graph for that particular vertex neighbors */ for (v=1;v<=nV;v++) { if ((visited[v]==0) && (G1[u][v] != 0) && (dist[u] != DIST_MIN) && (dist[u] + G1[u][v] < dist[v])) { dist[v] = dist[u] + G1[u][v]; //printf ("vertex %u, dist %u\n",v, dist[v]); } } } printSol(dist, nV); }
int main() { int sol[N][N] = { {0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0} }; int maze[N][N] = { {1, 1, 1, 1, 1, 1}, {1, 0, 0, 0, 0, 1}, {1, 0, 0, 0, 0, 1}, {1, 0, 0, 0, 0, 1}, {1, 0, 0, 0, 0, 1}, {1, 1, 1, 1, 1, 1} }; if(!isSafe(maze,0,0,sol)) { printf("\nSolution does not exist!!"); getchar(); exit(0); } sol[0][0]=1; if(maze[1][0]==1) down(maze,1,0,sol); else right(maze,0,1,sol); if(sol[N-1][N-1]==1) printSol(sol); else printf("\nSolution does not exist!!"); getchar(); return 0; }