Download code

Jump to: navigation, search

Back to Eight_queens_puzzle_(C)

Download for Windows: zip

Download for UNIX: zip, tar.gz, tar.bz2

one_queen_per_row_search_queens.c

 1 /* The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/Eight_queens_puzzle_(C)?oldid=15660
14 */
15 
16 #include <stdlib.h>   /* calloc() */
17 #include <stdio.h>
18 
19 void search_for_queens_solutions(int n) {
20     int queen_num_1, queen_num_2;
21     int is_valid;
22 
23     int queen_num;
24     int* queen_rows = (int*)calloc(n, sizeof(int));
25     int* queen_cols = (int*)calloc(n, sizeof(int));
26 
27     for(queen_num=0; queen_num < n; queen_num++) {
28         queen_rows[queen_num] = queen_num;
29         queen_cols[queen_num] = 0;
30     }
31     while(1) {
32         is_valid = 1; /* Assume initially is valid, search for disproof */
33 	for(queen_num_1=0; is_valid && queen_num_1 < n; queen_num_1++) {
34 	    for(queen_num_2=queen_num_1 + 1; is_valid && queen_num_2 < n; queen_num_2++) {
35 	        if (queen_cols[queen_num_1] == queen_cols[queen_num_2] ||
36 	            queen_rows[queen_num_1] - queen_rows[queen_num_2] ==
37 		      queen_cols[queen_num_1] - queen_cols[queen_num_2] ||
38 		    queen_rows[queen_num_1] - queen_rows[queen_num_2] ==
39 		      -(queen_cols[queen_num_1] - queen_cols[queen_num_2])@ text
40 )
41 	        {
42 	            is_valid = 0;
43 	        }
44 	    }
45 	}
46 	if (is_valid) {
47 	    for(queen_num=0; queen_num < n; queen_num++) {
48 	        printf("(%d,%d) ", queen_rows[queen_num], queen_cols[queen_num]);
49 	    }
50 	    printf("\n");
51 	}
52         for(queen_num = n-1; queen_num >= 0; queen_num--) {
53 	    queen_cols[queen_num]++;
54 	    if (queen_cols[queen_num] >= n) {
55 	        queen_cols[queen_num] = 0;
56 	    } else {
57 	        break;
58 	    }
59 	}
60 	if (queen_num < 0) {
61 	    free(queen_rows);
62 	    free(queen_cols);
63 
64 	    return;
65 	}
66     }
67 }
68 
69 int main(int argc, char* argv[]) {
70     search_for_queens_solutions(atoi(argv[1]));
71     return 0;
72 }


hijacker
hijacker
hijacker
hijacker

brute_force_search_queens.c

 1 /* The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/Eight_queens_puzzle_(C)?oldid=15660
14 */
15 
16 #include <stdlib.h>   /* calloc() */
17 #include <stdio.h>
18 
19 void search_for_queens_solutions(int n) {
20     int queen_num_1, queen_num_2;
21     int is_valid;
22 
23     int queen_num;
24     int* queen_rows = (int*)calloc(n, sizeof(int));
25     int* queen_cols = (int*)calloc(n, sizeof(int));
26 
27     while(1) {
28         is_valid = 1; /* Assume initially is valid, search for disproof */
29 	for(queen_num_1=0; is_valid && queen_num_1 < n; queen_num_1++) {
30 	    for(queen_num_2=queen_num_1 + 1; is_valid && queen_num_2 < n; queen_num_2++) {
31 	        if (queen_rows[queen_num_1] == queen_rows[queen_num_2] ||
32 	            queen_cols[queen_num_1] == queen_cols[queen_num_2] ||
33 	            queen_rows[queen_num_1] - queen_rows[queen_num_2] ==
34 		      queen_cols[queen_num_1] - queen_cols[queen_num_2] ||
35 		    queen_rows[queen_num_1] - queen_rows[queen_num_2] ==
36 		      -(queen_cols[queen_num_1] - queen_cols[queen_num_2])@ text
37 )
38 	        {
39 	            is_valid = 0;
40 	        }
41 	    }
42 	}
43 	if (is_valid) {
44 	    for(queen_num=0; queen_num < n; queen_num++) {
45 	        printf("(%d,%d) ", queen_rows[queen_num], queen_cols[queen_num]);
46 	    }
47 	    printf("\n");
48 	}
49         for(queen_num = n-1; queen_num >= 0; queen_num--) {
50 	    queen_cols[queen_num]++;
51 	    if (queen_cols[queen_num] >= n) {
52 	        queen_cols[queen_num] = 0;
53 	        queen_rows[queen_num]++;
54 	    }
55 	    if (queen_rows[queen_num] >= n) {
56 	        queen_rows[queen_num] = queen_cols[queen_num] = 0;
57 	    } else {
58 	        break;
59 	    }
60 	}
61 	if (queen_num < 0) {
62 	    free(queen_rows);
63 	    free(queen_cols);
64 
65 	    return;
66 	}
67     }
68 }
69 
70 int main(int argc, char* argv[]) {
71     search_for_queens_solutions(atoi(argv[1]));
72     return 0;
73 }


hijacker
hijacker
hijacker
hijacker

build.log

 1 /tmp/litprog1150296/one_queen_per_row_search_queens.c: In function 'search_for_queens_solutions':
 2 /tmp/litprog1150296/one_queen_per_row_search_queens.c:39:9: error: stray '@' in program
 3 /tmp/litprog1150296/one_queen_per_row_search_queens.c:39:63: error: expected ')' before 'text'
 4 /tmp/litprog1150296/brute_force_search_queens.c: In function 'search_for_queens_solutions':
 5 /tmp/litprog1150296/brute_force_search_queens.c:36:9: error: stray '@' in program
 6 /tmp/litprog1150296/brute_force_search_queens.c:36:63: error: expected ')' before 'text'
 7 /tmp/litprog1150296/combination_brute_force_search_queens.c: In function 'search_for_queens_solutions':
 8 /tmp/litprog1150296/combination_brute_force_search_queens.c:40:9: error: stray '@' in program
 9 /tmp/litprog1150296/combination_brute_force_search_queens.c:40:63: error: expected ')' before 'text'
10 /tmp/litprog1150296/permutation_search_queens.c: In function 'search_for_queens_solutions':
11 /tmp/litprog1150296/permutation_search_queens.c:46:9: error: stray '@' in program
12 /tmp/litprog1150296/permutation_search_queens.c:46:63: error: expected ')' before 'text'
13 /tmp/litprog1150296/backtracking_search_queens.c: In function 'search_for_queens_solutions':
14 /tmp/litprog1150296/backtracking_search_queens.c:62:9: error: stray '@' in program
15 /tmp/litprog1150296/backtracking_search_queens.c:62:63: error: expected ')' before 'text'


hijacker
hijacker
hijacker
hijacker

combination_brute_force_search_queens.c

 1 /* The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/Eight_queens_puzzle_(C)?oldid=15660
14 */
15 
16 #include <stdlib.h>   /* calloc() */
17 #include <stdio.h>
18 
19 void search_for_queens_solutions(int n) {
20     int queen_num_1, queen_num_2;
21     int is_valid;
22 
23     int queen_num;
24     int* queen_rows = (int*)calloc(n, sizeof(int));
25     int* queen_cols = (int*)calloc(n, sizeof(int));
26 
27     for(queen_num=0; queen_num < n; queen_num++) {
28         queen_rows[queen_num] = 0;
29         queen_cols[queen_num] = queen_num;
30     }
31     while(1) {
32         is_valid = 1; /* Assume initially is valid, search for disproof */
33 	for(queen_num_1=0; is_valid && queen_num_1 < n; queen_num_1++) {
34 	    for(queen_num_2=queen_num_1 + 1; is_valid && queen_num_2 < n; queen_num_2++) {
35 	        if (queen_rows[queen_num_1] == queen_rows[queen_num_2] ||
36 	            queen_cols[queen_num_1] == queen_cols[queen_num_2] ||
37 	            queen_rows[queen_num_1] - queen_rows[queen_num_2] ==
38 		      queen_cols[queen_num_1] - queen_cols[queen_num_2] ||
39 		    queen_rows[queen_num_1] - queen_rows[queen_num_2] ==
40 		      -(queen_cols[queen_num_1] - queen_cols[queen_num_2])@ text
41 )
42 	        {
43 	            is_valid = 0;
44 	        }
45 	    }
46 	}
47 	if (is_valid) {
48 	    for(queen_num=0; queen_num < n; queen_num++) {
49 	        printf("(%d,%d) ", queen_rows[queen_num], queen_cols[queen_num]);
50 	    }
51 	    printf("\n");
52 	}
53         for(queen_num = n-1; queen_num >= 0; queen_num--) {
54 	    queen_cols[queen_num]++;
55 	    if (queen_cols[queen_num] >= n) {
56 	        queen_cols[queen_num] = 0;
57 	        queen_rows[queen_num]++;
58 	    }
59 	    /* This condition makes sure there is room for all following queens */
60 	    if (queen_rows[queen_num] < n - 1 ||
61 	        (queen_rows[queen_num] == n - 1 && queen_cols[queen_num] <= queen_num))
62 	    {
63 	        for(queen_num_2 = queen_num + 1; queen_num_2 < n; queen_num_2++) {
64 	            queen_cols[queen_num_2] = queen_cols[queen_num_2 - 1] + 1;
65 	            if (queen_cols[queen_num_2] < n) {
66 	                queen_rows[queen_num_2] = queen_rows[queen_num_2 - 1];
67 	            } else {
68 	                queen_cols[queen_num_2] = 0;
69 	                queen_rows[queen_num_2] = queen_rows[queen_num_2 - 1] + 1;
70 	            }
71 	        }
72 	        break;
73 	    }
74 	}
75 	if (queen_num < 0) {
76 	    free(queen_rows);
77 	    free(queen_cols);
78 
79 	    return;
80 	}
81     }
82 }
83 
84 int main(int argc, char* argv[]) {
85     search_for_queens_solutions(atoi(argv[1]));
86     return 0;
87 }


hijacker
hijacker
hijacker
hijacker

permutation_search_queens.c

 1 /* The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/Eight_queens_puzzle_(C)?oldid=15660
14 */
15 
16 #include <stdlib.h>   /* calloc() */
17 #include <stdio.h>
18 
19 void search_for_queens_solutions(int n) {
20     int queen_num_1, queen_num_2;
21     int is_valid;
22 
23     int queen_num;
24     int col_num;
25 
26 
27     int* queen_rows = (int*)calloc(n, sizeof(int));
28     int* queen_cols = (int*)calloc(n, sizeof(int));
29 
30     int* col_in_use = (int*)calloc(n + 1, sizeof(int));
31 
32     for(queen_num=0; queen_num < n; queen_num++) {
33         queen_rows[queen_num] = queen_cols[queen_num] = queen_num;
34     }
35     for(queen_num=0; queen_num < n; queen_num++) {
36         col_in_use[queen_num] = 1;
37     }
38 
39     while(1) {
40         is_valid = 1; /* Assume initially is valid, search for disproof */
41 	for(queen_num_1=0; is_valid && queen_num_1 < n; queen_num_1++) {
42 	    for(queen_num_2=queen_num_1 + 1; is_valid && queen_num_2 < n; queen_num_2++) {
43 	        if (queen_rows[queen_num_1] - queen_rows[queen_num_2] ==
44 		      queen_cols[queen_num_1] - queen_cols[queen_num_2] ||
45 		    queen_rows[queen_num_1] - queen_rows[queen_num_2] ==
46 		      -(queen_cols[queen_num_1] - queen_cols[queen_num_2])@ text
47 )
48 	        {
49 	            is_valid = 0;
50 	        }
51 	    }
52 	}
53 	if (is_valid) {
54 	    for(queen_num=0; queen_num < n; queen_num++) {
55 	        printf("(%d,%d) ", queen_rows[queen_num], queen_cols[queen_num]);
56 	    }
57 	    printf("\n");
58 	}
59         for(queen_num = n-1; queen_num >= 0; queen_num--) {
60 	    col_in_use[queen_cols[queen_num]] = 0;
61 	    do {
62 	        queen_cols[queen_num]++;
63 	    } while(col_in_use[queen_cols[queen_num]]);
64 	    if (queen_cols[queen_num] < n) {
65 	        col_in_use[queen_cols[queen_num]] = 1;
66 	        break;
67 	    }
68 	}
69 	if (queen_num < 0) {
70 	    free(col_in_use);
71 
72 	    free(queen_rows);
73 	    free(queen_cols);
74 
75 	    return;
76 	}
77 	/* Put the remaining queens in the remaining free columns in order */
78 	queen_num++;
79 	for(col_num = 0; queen_num < n && col_num < n; col_num++) {
80 	    if (col_in_use[col_num] == 0) {
81 	        col_in_use[col_num] = 1;
82 	        queen_cols[queen_num] = col_num;
83 	        queen_num++;
84 	    }
85 	}
86     }
87 }
88 
89 int main(int argc, char* argv[]) {
90     search_for_queens_solutions(atoi(argv[1]));
91     return 0;
92 }


hijacker
hijacker
hijacker
hijacker

backtracking_search_queens.c

  1 /* The authors of this work have released all rights to it and placed it
  2 in the public domain under the Creative Commons CC0 1.0 waiver
  3 (http://creativecommons.org/publicdomain/zero/1.0/).
  4 
  5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 12 
 13 Retrieved from: http://en.literateprograms.org/Eight_queens_puzzle_(C)?oldid=15660
 14 */
 15 
 16 #include <stdlib.h>   /* calloc() */
 17 #include <stdio.h>
 18 
 19 void search_for_queens_solutions(int n) {
 20     int queen_num_1, queen_num_2;
 21     int is_valid;
 22 
 23     int queen_num;
 24     int current_queen = 0;
 25     int num_solutions = 0;
 26     int went_off_right_side;
 27 
 28 
 29     int* queen_rows = (int*)calloc(n, sizeof(int));
 30     int* queen_cols = (int*)calloc(n, sizeof(int));
 31 
 32     int* col_in_use = (int*)calloc(n + 1, sizeof(int));
 33 
 34     for(queen_num=0; queen_num < n; queen_num++) {
 35         queen_rows[queen_num] = queen_num;
 36         queen_cols[queen_num] = -1;
 37     }
 38 
 39     while(current_queen >= 0) {
 40         if (queen_cols[current_queen] != -1) {
 41             col_in_use[queen_cols[current_queen]] = 0;
 42         }
 43         do {
 44             /* Find next open column */
 45             do {
 46                 queen_cols[current_queen]++;
 47             } while(col_in_use[queen_cols[current_queen]]);
 48 
 49             went_off_right_side = (queen_cols[current_queen] >= n) ||
 50                                   (current_queen == 0 && queen_cols[0] > (n-1)/2);
 51             if (went_off_right_side) {
 52                 break;
 53             }
 54 
 55             /* Check for diagonals */
 56             is_valid = 1;
 57             queen_num_1 = current_queen;
 58             for(queen_num_2 = 0; queen_num_2 < queen_num_1; queen_num_2++) {
 59                 if (queen_rows[queen_num_1] - queen_rows[queen_num_2] ==
 60 		      queen_cols[queen_num_1] - queen_cols[queen_num_2] ||
 61 		    queen_rows[queen_num_1] - queen_rows[queen_num_2] ==
 62 		      -(queen_cols[queen_num_1] - queen_cols[queen_num_2])@ text
 63 ) {
 64                     is_valid = 0;
 65                     break;
 66                 }
 67             }
 68         } while(!is_valid);
 69 
 70         if (!went_off_right_side) {
 71             col_in_use[queen_cols[current_queen]] = 1;
 72             if (current_queen + 1 < n) {
 73                 current_queen++;
 74                 queen_cols[current_queen] = -1;
 75             } else {
 76                 if (num_solutions == 0) {
 77                     for(queen_num=0; queen_num < n; queen_num++) {
 78 		        printf("(%d,%d) ", queen_rows[queen_num], queen_cols[queen_num]);
 79 		    }
 80 		    printf("\n");
 81                 }
 82                 num_solutions++;
 83                 if (queen_cols[0] < n/2) {                
 84                     num_solutions++; /* Add mirrored solution */
 85                 }
 86             }
 87         } else {
 88             /* Backtrack */
 89             queen_cols[current_queen] = -1;
 90             current_queen--;
 91         }
 92     }
 93     free(col_in_use);
 94 
 95     free(queen_rows);
 96     free(queen_cols);
 97 
 98 
 99     printf("Number of solutions: %d\n", num_solutions);
100 }
101 
102 int main(int argc, char* argv[]) {
103     search_for_queens_solutions(atoi(argv[1]));
104     return 0;
105 }


hijacker
hijacker
hijacker
hijacker