SHOW:
|
|
- or go back to the newest paste.
1 | //------------------------SELECTION SORT------------------------// | |
2 | ||
3 | ||
4 | #include <stdio.h> | |
5 | #include <stdlib.h> | |
6 | #include <time.h> | |
7 | ||
8 | void selection_sort(int arr[], int n) { | |
9 | int i, j, min_idx, temp; | |
10 | for(i = 0; i < n - 1; i++) { | |
11 | min_idx = i; | |
12 | for(j = i + 1; j < n; j++) { | |
13 | if(arr[j] < arr[min_idx]) | |
14 | min_idx = j; | |
15 | } | |
16 | if(min_idx != i) { | |
17 | temp = arr[min_idx]; | |
18 | arr[min_idx] = arr[i]; | |
19 | arr[i] = temp; | |
20 | } | |
21 | } | |
22 | } | |
23 | void printArray(int arr[], int size) { | |
24 | int i; | |
25 | for(i = 0; i < size; i++) | |
26 | printf("%d ", arr[i]); | |
27 | printf("\n"); | |
28 | } | |
29 | int main(void) { | |
30 | int i, n; | |
31 | clock_t start, end; | |
32 | double cpu_time_used; | |
33 | printf("Enter the value of n: "); | |
34 | scanf("%d", &n); | |
35 | int arr[n]; | |
36 | ||
37 | for(i = 0; i < n; i++) { | |
38 | arr[i] = rand() % 1000; | |
39 | printf("%d", arr[i]); | |
40 | } | |
41 | ||
42 | start = clock(); | |
43 | selection_sort(arr, n); | |
44 | end = clock(); | |
45 | cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC; | |
46 | printf("Sorted array:\n"); | |
47 | printArray(arr, n); | |
48 | printf("Execution time: %f seconds\n", cpu_time_used); | |
49 | return 0; | |
50 | } | |
51 | ||
52 | ||
53 | //------------------------QUICK SORT------------------------// | |
54 | ||
55 | ||
56 | #include <stdio.h> | |
57 | #include <stdlib.h> | |
58 | #include <time.h> | |
59 | ||
60 | void swap(int *a, int *b) | |
61 | { | |
62 | int temp = *a; | |
63 | *a = *b; | |
64 | *b = temp; | |
65 | } | |
66 | ||
67 | int partition(int arr[], int low, int high) { | |
68 | int pivot = arr[low]; | |
69 | int i =low; | |
70 | int j = high; | |
71 | while(i < j) { | |
72 | while(arr[i] <= pivot && i <= high - 1) | |
73 | i++; | |
74 | while(arr[j] > pivot && j >= low + 1) | |
75 | j--; | |
76 | if(i < j) | |
77 | swap(&arr[i], &arr[j]); | |
78 | } | |
79 | swap(&arr[low], &arr[j]); | |
80 | return j; | |
81 | } | |
82 | ||
83 | void quickSort(int arr[], int low, int high) { | |
84 | if(low < high) { | |
85 | int partitionIndex = partition(arr, low, high); | |
86 | quickSort(arr, low, partitionIndex - 1); | |
87 | quickSort(arr, partitionIndex + 1, high); | |
88 | } | |
89 | } | |
90 | ||
91 | int main(void) { | |
92 | int i, n; | |
93 | clock_t start, end; | |
94 | double cpu_time_used; | |
95 | printf("Enter the value of n: "); | |
96 | scanf("%d", &n); | |
97 | int arr[n]; | |
98 | for(i = 0; i < n; i++) { | |
99 | arr[i] = rand() % 1000; | |
100 | printf("%d", arr[i]); | |
101 | } | |
102 | start = clock(); | |
103 | quickSort(arr, 0, n - 1); | |
104 | end = clock(); | |
105 | cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC; | |
106 | printf("\nSorted array: "); | |
107 | for(i = 0; i < n; i++) | |
108 | printf("%d", arr[i]); | |
109 | ||
110 | printf("\nExecution time: %f seconds\n", cpu_time_used); | |
111 | return 0; | |
112 | } | |
113 | ||
114 | ||
115 | //------------------------MERGE SORT------------------------// | |
116 | ||
117 | ||
118 | #include <stdio.h> | |
119 | #include <stdlib.h> | |
120 | #include <time.h> | |
121 | ||
122 | void merge(int arr[], int l, int m, int r) | |
123 | { | |
124 | int i, j, k; | |
125 | int n1 = m - l + 1; | |
126 | int n2 = r - m; | |
127 | ||
128 | int L[n1], R[n2]; | |
129 | for(i = 0; i < n1; i++) | |
130 | L[i] = arr[l + i]; | |
131 | for(j = 0; j < n2; j++) | |
132 | R[j] = arr[m + 1 + j]; | |
133 | ||
134 | i = 0, j = 0, k = l; | |
135 | ||
136 | while(i < n1 && j < n2) { | |
137 | if(L[i] <= R[j]) { | |
138 | arr[k++] = L[i++]; | |
139 | } else { | |
140 | arr[k++] = R[j++]; | |
141 | } | |
142 | } | |
143 | ||
144 | while(i < n1) | |
145 | arr[k++] = L[i++]; | |
146 | while(j < n2) | |
147 | arr[k++] = R[j++]; | |
148 | } | |
149 | ||
150 | void mergeSort(int arr[], int l, int r) { | |
151 | if(l < r) { | |
152 | int m = l + (r - l) / 2; | |
153 | mergeSort(arr, l, m); | |
154 | mergeSort(arr, m + 1, r); | |
155 | merge(arr, l, m, r); | |
156 | } | |
157 | } | |
158 | ||
159 | int main(void) { | |
160 | int i, n; | |
161 | clock_t start, end; | |
162 | double cpu_time_used; | |
163 | printf("Enter the value of n: "); | |
164 | scanf("%d", &n); | |
165 | int arr[n]; | |
166 | for(i = 0; i < n; i++) { | |
167 | arr[i] = rand() % 1000; | |
168 | printf("%d ", arr[i]); | |
169 | } | |
170 | start = clock(); | |
171 | mergeSort(arr, 0, n - 1); | |
172 | end = clock(); | |
173 | cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC; | |
174 | printf("\nSorted array: "); | |
175 | for(i = 0; i < n; i++) printf("%d ", arr[i]); | |
176 | printf("\nExecution time: %f seconds\n", cpu_time_used); | |
177 | return 0; | |
178 | } | |
179 | ||
180 | ||
181 | //------------------------PRIM'S ALGORITHM------------------------// | |
182 | ||
183 | ||
184 | #include <stdio.h> | |
185 | ||
186 | void my_prim(int adj[][10], int N) { | |
187 | int i, j, nv, min, min_cost = 0, u = 0, v = 0; | |
188 | int visit[10] = {0}; | |
189 | visit[0] = 1; | |
190 | nv = 1; | |
191 | while (nv < N) { | |
192 | min = 999; | |
193 | for (i = 0; i < N; i++) { | |
194 | if (visit[i] == 1) { | |
195 | for (j = 0; j < N; j++) { | |
196 | if (!visit[j] && adj[i][j] < min) { | |
197 | min = adj[i][j]; | |
198 | u = i; | |
199 | v = j; | |
200 | printf("\nConsidering edge (%d, %d) with weight %d", i, j, adj[i][j]); | |
201 | } | |
202 | } | |
203 | } | |
204 | } | |
205 | adj[u][v] = 999; | |
206 | adj[v][u] = 999; | |
207 | ||
208 | if (visit[u] == 1 && visit[v] == 0) { | |
209 | visit[v] = 1; | |
210 | min_cost += min; | |
211 | nv++; | |
212 | printf("\nEdge %d --> %d : (%d)\n", u, v, min); | |
213 | } | |
214 | } | |
215 | printf("Minimum cost : %d\n", min_cost); | |
216 | } | |
217 | ||
218 | int main() { | |
219 | int adj[10][10], N, i, j; | |
220 | printf("Enter number of nodes in the graph: "); | |
221 | scanf("%d", &N); | |
222 | ||
223 | printf("Enter the adjacency matrix\n"); | |
224 | printf("Enter 0 for no connection and weights for connection\n"); | |
225 | ||
226 | for (i = 0; i < N; i++) { | |
227 | for (j = 0; j < N; j++) { | |
228 | scanf("%d", &adj[i][j]); | |
229 | if (adj[i][j] == 0) { | |
230 | adj[i][j] = 999; | |
231 | } | |
232 | } | |
233 | } | |
234 | my_prim(adj, N); | |
235 | return 0; | |
236 | } | |
237 | ||
238 | ||
239 | //------------------------KRUSKAL'S ALGORITHM------------------------// | |
240 | ||
241 | ||
242 | #include <stdio.h> | |
243 | ||
244 | int ne = 1, min_cost = 0; | |
245 | ||
246 | int main(void) { | |
247 | int n, i, j, min, a, b, u, v, cost[20][20], parent[20]; | |
248 | printf("Enter the number of vertices: "); | |
249 | scanf("%d", &n); | |
250 | printf("\nEnter the cost matrix: "); | |
251 | for (i = 1; i <= n; i++) | |
252 | for (j = 1; j <= n; j++) | |
253 | scanf("%d", &cost[i][j]); | |
254 | for (i = 1; i <= n; i++) | |
255 | parent[i] = 0; | |
256 | printf("\nThe edges of spanning tree are\n"); | |
257 | ||
258 | while (ne < n) { | |
259 | min = 999; | |
260 | for (i = 1; i <= n; i++) { | |
261 | for (j = 1; j <= n; j++) { | |
262 | if (cost[i][j] < min && cost[i][j] != 0) { | |
263 | min = cost[i][j]; | |
264 | a = u = i; | |
265 | b = v = j; | |
266 | } | |
267 | } | |
268 | } | |
269 | while (parent[u]) u = parent[u]; | |
270 | while (parent[v]) v = parent[v]; | |
271 | if (u != v) { | |
272 | printf("Edge %d\t(%d --> %d) = %d\n", ne++, a, b, min); | |
273 | min_cost += min; | |
274 | parent[v] = u; | |
275 | } | |
276 | cost[a][b] = cost[b][a] = 999; | |
277 | } | |
278 | printf("\nMinimum cost = %d\n", min_cost); | |
279 | } | |
280 | ||
281 | ||
282 | //------------------------DIJKSTRA'S ALGORITHM------------------------// | |
283 | ||
284 | ||
285 | #include <stdio.h> | |
286 | #define infinity 999 | |
287 | ||
288 | void dij(int n, int v, int cost[10][10], int dist[10]) { | |
289 | int i, u, count, w, flag[10], min; | |
290 | for (i = 1; i <= n; i++) { | |
291 | flag[i] = 0; | |
292 | dist[i] = cost[v][i]; | |
293 | } | |
294 | count = 2; | |
295 | while (count <= n) { | |
296 | min = infinity; | |
297 | for (w = 1; w <= n; w++) { | |
298 | if (dist[w] < min && !flag[w]) { | |
299 | min = dist[w]; | |
300 | u = w; | |
301 | } | |
302 | } | |
303 | flag[u] = 1; | |
304 | count++; | |
305 | for (w = 1; w <= n; w++) { | |
306 | if ((dist[u] + cost[u][w] < dist[w]) && !flag[w]) | |
307 | dist[w] = dist[u] + cost[u][w]; | |
308 | } | |
309 | } | |
310 | } | |
311 | ||
312 | int main(void) { | |
313 | int n, v, i, j, cost[10][10], dist[10]; | |
314 | printf("\nEnter the number of nodes: "); | |
315 | scanf("%d", &n); | |
316 | ||
317 | printf("Enter the cost matrix:\n"); | |
318 | for (i = 1; i <= n; i++) { | |
319 | for (j = 1; j <= n; j++) { | |
320 | scanf("%d", &cost[i][j]); | |
321 | if (cost[i][j] == 0) cost[i][j] = infinity; | |
322 | } | |
323 | } | |
324 | printf("\nEnter the source vertex: "); | |
325 | scanf("%d", &v); | |
326 | dij(n, v, cost, dist); | |
327 | printf("\nShortest Path:\n"); | |
328 | for (i = 1; i <= n; i++) { | |
329 | if (i != v) | |
330 | printf("%d --> %d, cost = %d\n", v, i, dist[i]); | |
331 | } | |
332 | } | |
333 | ||
334 | ||
335 | //-----------------PROGRAM 3a: Floyd's Algo.-----------------// | |
336 | ||
337 | #include <stdio.h> | |
338 | ||
339 | int min(int a, int b) { | |
340 | return (a < b) ? a : b; | |
341 | } | |
342 | void floyd(int p[10][10], int n) { | |
343 | int i, j, k; | |
344 | for (k = 1; k <= n; k++) { | |
345 | for (i = 1; i <= n; i++) { | |
346 | for (j = 1; j <= n; j++) { | |
347 | p[i][j] = min(p[i][j], p[i][k] + p[k][j]); | |
348 | } | |
349 | } | |
350 | } | |
351 | } | |
352 | int main(void) { | |
353 | int a[10][10], n, i, j; | |
354 | printf("Enter value of n: "); | |
355 | scanf("%d", &n); | |
356 | printf("Enter the graph data:\n"); | |
357 | for (i = 1; i <= n; i++) { | |
358 | for (j = 1; j <= n; j++) { | |
359 | scanf("%d", &a[i][j]); | |
360 | if (i != j && a[i][j] == 0) a[i][j] = 999; | |
361 | } | |
362 | } | |
363 | floyd(a, n); | |
364 | printf("Shortest path matrix:\n"); | |
365 | for (i = 1; i <= n; i++) { | |
366 | for (j = 1; j <= n; j++) { | |
367 | printf("%d ", a[i][j]); | |
368 | } | |
369 | printf("\n"); | |
370 | } | |
371 | } | |
372 | ||
373 | //-----------------PROGRAM 3b: Warshall's Algo.-----------------// | |
374 | ||
375 | #include <stdio.h> | |
376 | ||
377 | void warshall(int[10][10], int); | |
378 | ||
379 | int main(void) { | |
380 | int a[10][10], i, j, n; | |
381 | printf("Enter the no. of nodes: "); | |
382 | scanf("%d", &n); | |
383 | printf("Enter the adjacency matrix:\n"); | |
384 | for (i = 1; i <= n; i++) | |
385 | for (j = 1; j <= n; j++) | |
386 | scanf("%d", &a[i][j]); | |
387 | printf("The adjacency matrix is:\n"); | |
388 | for (i = 1; i <= n; i++) { | |
389 | for (j = 1; j <= n; j++) { | |
390 | printf("%d ", a[i][j]); | |
391 | } | |
392 | printf("\n"); | |
393 | } | |
394 | warshall(a, n); | |
395 | } | |
396 | ||
397 | void warshall(int p[10][10], int n) { | |
398 | int i, j, k; | |
399 | for (k = 1; k <= n; k++) { | |
400 | for (j = 1; j <= n; j++) { | |
401 | for (i = 1; i <= n; i++) { | |
402 | if ((p[i][j] == 0) && (p[i][k] == 1) && (p[k][j] == 1)) | |
403 | p[i][j] = 1; | |
404 | } | |
405 | } | |
406 | } | |
407 | printf("The path matrix is:\n"); | |
408 | for (i = 1; i <= n; i++) { | |
409 | for (j = 1; j <= n; j++) { | |
410 | printf("%d ", p[i][j]); | |
411 | } | |
412 | printf("\n"); | |
413 | } | |
414 | } | |
415 | ||
416 | //-----------------PROGRAM 5: Topological Sorting-----------------// | |
417 | ||
418 | #include <stdio.h> | |
419 | int a[10][10], n, indegree[10]; | |
420 | ||
421 | void find_indegree() { | |
422 | int j, i, sum; | |
423 | for (j = 0; j < n; j++) { | |
424 | sum = 0; | |
425 | for (i = 0; i < n; i++) | |
426 | sum += a[i][j]; | |
427 | indegree[j] = sum; | |
428 | } | |
429 | } | |
430 | ||
431 | void topology() { | |
432 | int i, u, v, t[10], s[10], top = -1, k = 0; | |
433 | find_indegree(); | |
434 | for (i = 0; i < n; i++) { | |
435 | if (indegree[i] == 0) s[++top] = i; | |
436 | } | |
437 | ||
438 | while (top != -1) { | |
439 | u = s[top--]; | |
440 | t[k++] = u; | |
441 | for (v = 0; v < n; v++) { | |
442 | if (a[u][v] == 1) { | |
443 | indegree[v]--; | |
444 | if (indegree[v] == 0) s[++top] = v; | |
445 | } | |
446 | } | |
447 | } | |
448 | printf("The topological sequence is:\n"); | |
449 | for (i = 0; i < n; i++) | |
450 | printf("%d ", t[i]); | |
451 | printf("\n"); | |
452 | } | |
453 | ||
454 | int main(void) { | |
455 | int i, j; | |
456 | printf("Enter number of nodes: "); | |
457 | scanf("%d", &n); | |
458 | printf("Enter adjacency matrix:\n"); | |
459 | for (i = 0; i < n; i++) | |
460 | for (j = 0; j < n; j++) | |
461 | scanf("%d", &a[i][j]); | |
462 | topology(); | |
463 | } | |
464 | ||
465 | //-----------------PROGRAM 6: Knapsack 0/1-----------------// | |
466 | ||
467 | #include <stdio.h> | |
468 | ||
469 | int w[10], p[10], v[10][10], n, i, j, cap, x[10] = {0}; | |
470 | ||
471 | int max(int i, int j) { | |
472 | return (i > j ? i : j); | |
473 | } | |
474 | ||
475 | int knap(int i, int j) { | |
476 | int value; | |
477 | if (v[i][j] < 0) { | |
478 | if (j < w[i]) | |
479 | value = knap(i - 1, j); | |
480 | else | |
481 | value = max(knap(i - 1, j), p[i] + knap(i - 1, j - w[i])); | |
482 | v[i][j] = value; | |
483 | } | |
484 | return v[i][j]; | |
485 | } | |
486 | ||
487 | int main(void) { | |
488 | int profit, count = 0; | |
489 | printf("\nEnter the number of elements: "); | |
490 | scanf("%d", &n); | |
491 | printf("Enter the profits and weights of the elements:\n"); | |
492 | for (i = 1; i <= n; i++) { | |
493 | printf("For item no. %d\n", i); | |
494 | scanf("%d%d", &p[i], &w[i]); | |
495 | } | |
496 | printf("\nEnter the capacity: "); | |
497 | scanf("%d", &cap); | |
498 | for (i = 0; i <= n; i++) { | |
499 | for (j = 0; j <= cap; j++) { | |
500 | if ((i == 0) || (j == 0)) | |
501 | v[i][j] = 0; | |
502 | else | |
503 | v[i][j] = -1; | |
504 | } | |
505 | } | |
506 | ||
507 | profit = knap(n, cap); | |
508 | i = n; | |
509 | j = cap; | |
510 | while (j != 0 && i != 0) { | |
511 | if (v[i][j] != v[i - 1][j]) { | |
512 | x[i] = 1; | |
513 | j = j - w[i]; | |
514 | i--; | |
515 | } | |
516 | else | |
517 | i--; | |
518 | } | |
519 | printf("Items included are:\n"); | |
520 | printf("S.no.\tWeight\tProfit\n"); | |
521 | for (i = 1; i <= n; i++) | |
522 | if (x[i]) | |
523 | printf("%d\t%d\t%d\n", ++count, w[i], p[i]); | |
524 | printf("Total profit = %d\n", profit); | |
525 | } | |
526 | ||
527 | //-----------------PROGRAM 7: Knapsack Continuous & Discrete-----------------// | |
528 | ||
529 | #include <stdio.h> | |
530 | void knapsack(int n, float weight[], float profit[], float capacity) | |
531 | { | |
532 | float x[20], tp = 0; | |
533 | int i, j, u; | |
534 | u = capacity; | |
535 | ||
536 | for(i = 0; i < n; i++) x[i] = 0.0; | |
537 | ||
538 | for (i = 0; i < n; i++) { | |
539 | if (weight[i] > u) | |
540 | break; | |
541 | else { | |
542 | x[i] = 1.0; | |
543 | tp = tp + profit[i]; | |
544 | u = u - weight[i]; | |
545 | } | |
546 | } | |
547 | if (i < n) | |
548 | x[i] = u / weight[i]; | |
549 | ||
550 | tp = tp + (x[i] * profit[i]); | |
551 | printf("\nThe result vector is: "); | |
552 | for (i = 0; i < n; i++) | |
553 | printf("%f\t", x[i]); | |
554 | ||
555 | printf("\nMaximum profit is: %f\n", tp); | |
556 | } | |
557 | ||
558 | void Dknapsack(int n, float weight[], float profit[], float capacity) { | |
559 | float x[20], tp = 0; | |
560 | int i, j, u; | |
561 | u = capacity; | |
562 | for (i = 0; i < n; i++) x[i] = 0.0; | |
563 | for (i = 0; i < n; i++) { | |
564 | if (weight[i] > u) | |
565 | continue; | |
566 | else { | |
567 | x[i] = 1.0; | |
568 | tp = tp + profit[i]; | |
569 | u = u - weight[i]; | |
570 | } | |
571 | } | |
572 | printf("\nThe result vector is: "); | |
573 | for (i = 0; i < n; i++) printf("%f\t", x[i]); | |
574 | printf("\nMaximum profit is: %f\n", tp); | |
575 | } | |
576 | ||
577 | int main(void) { | |
578 | float weight[20], profit[20], capacity; | |
579 | int num, i, j; | |
580 | float ratio[20], temp; | |
581 | ||
582 | printf("\nEnter the no. of objects: "); | |
583 | scanf("%d", &num); | |
584 | printf("\nEnter the weights of each object:\n"); | |
585 | for (i = 0; i < num; i++) scanf("%f", &weight[i]); | |
586 | ||
587 | printf("Enter the profits of each object:\n"); | |
588 | for (i = 0; i < num; i++) scanf("%f", &profit[i]); | |
589 | ||
590 | printf("Enter the capacity of knapsack: "); | |
591 | scanf("%f", &capacity); | |
592 | ||
593 | for (i = 0; i < num; i++) ratio[i] = profit[i] / weight[i]; | |
594 | ||
595 | for (i = 0; i < num; i++) { | |
596 | for (j = i + 1; j < num; j++) { | |
597 | if (ratio[i] < ratio[j]) { | |
598 | temp = ratio[j]; | |
599 | ratio[j] = ratio[i]; | |
600 | ratio[i] = temp; | |
601 | ||
602 | temp = weight[j]; | |
603 | weight[j] = weight[i]; | |
604 | weight[i] = temp; | |
605 | ||
606 | temp = profit[j]; | |
607 | profit[j] = profit[i]; | |
608 | profit[i] = temp; | |
609 | } | |
610 | } | |
611 | } | |
612 | knapsack(num, weight, profit, capacity); | |
613 | Dknapsack(num, weight, profit, capacity); | |
614 | return 0; | |
615 | } | |
616 | ||
617 | //-----------------PROGRAM 8: Subset Sum-----------------// | |
618 | ||
619 | #include <stdio.h> | |
620 | ||
621 | int s[10], x[10], d; | |
622 | void sumofsub(int, int, int); | |
623 | ||
624 | int main(void) { | |
625 | int i, n, sum = 0; | |
626 | printf("Enter the size of the set: "); | |
627 | scanf("%d", &n); | |
628 | printf("Enter the set in increasing order:\n"); | |
629 | for (i = 1; i <= n; i++) | |
630 | scanf("%d", &s[i]); | |
631 | printf("Enter the value of d: "); | |
632 | scanf("%d", &d); | |
633 | ||
634 | for (i = 1; i <= n; i++) sum = sum + s[i]; | |
635 | if (sum < d || s[1] > d) | |
636 | printf("\nNo subset possible"); | |
637 | else { | |
638 | sumofsub(0, 1, sum); | |
639 | } | |
640 | } | |
641 | ||
642 | void sumofsub(int m, int k, int r) { | |
643 | int i = 1; | |
644 | x[k] = 1; | |
645 | if ((m + s[k]) == d) | |
646 | { | |
647 | printf("Subset: "); | |
648 | for(i = 1; i <= k; i++) | |
649 | if (x[i] == 1) | |
650 | printf("%d ", s[i]); | |
651 | printf("\n"); | |
652 | } else { | |
653 | if (m + s[k] + s[k + 1] <= d) | |
654 | sumofsub(m + s[k], k + 1, r - s[k]); | |
655 | } | |
656 | if ((m + r - s[k] >= d) && (m + s[k + 1] <= d)) { | |
657 | x[k] = 0; | |
658 | sumofsub(m, k + 1, r - s[k]); | |
659 | } | |
660 | } | |
661 | ||
662 | //-----------------PROGRAM 12: N Queens-----------------// | |
663 | ||
664 | #include <stdio.h> | |
665 | #include <stdlib.h> | |
666 | #define MAX 50 | |
667 | int can_place(int c[], int r) { | |
668 | int i; | |
669 | for (i = 0; i < r; i++) { | |
670 | if (c[i] == c[r] || (abs(c[i] - c[r]) == abs(i - r))) | |
671 | return 0; | |
672 | } | |
673 | return 1; | |
674 | } | |
675 | ||
676 | void display(int c[], int n) { | |
677 | int i, j; | |
678 | char cb[10][10]; | |
679 | for (i = 0; i < n; i++ ) | |
680 | for (j = 0; j < n; j++) | |
681 | cb[i][j] = '-'; | |
682 | for (i = 0; i < n; i++) | |
683 | cb[i][c[i]] = 'Q'; | |
684 | ||
685 | for (i = 0; i < n; i++) { | |
686 | for (j = 0; j < n; j++) | |
687 | printf("%c", cb[i][j]); | |
688 | printf("\n"); | |
689 | } | |
690 | } | |
691 | ||
692 | void n_queens(int n) { | |
693 | int r; | |
694 | int c[MAX]; | |
695 | c[0] = -1; | |
696 | r = 0; | |
697 | while ( r >= 0) { | |
698 | c[r]++; | |
699 | while (c[r] < n && !can_place(c, r)) | |
700 | c[r]++; | |
701 | if (c[r] < n) { | |
702 | if (r == n - 1) { | |
703 | display(c, n); | |
704 | printf("\n\n"); | |
705 | } | |
706 | else { | |
707 | r++; | |
708 | c[r] = -1; | |
709 | } | |
710 | } | |
711 | else r--; | |
712 | } | |
713 | } | |
714 | ||
715 | int main(void) { | |
716 | int n; | |
717 | printf("\nEnter the number of queens: "); | |
718 | scanf("%d", &n); | |
719 | n_queens(n); | |
720 | } |