Description
Examine the checkerboard below and note that the six checkers is arranged on the board so that one and Only one are placed in each row and each column, and there are never more than one in any diagonal. (Diagonals run from southeast to northwest and southwest to northeast and include all diagonals, not just the major.)
1 2 3 4 5 6-------------------------1 | | O | | | | | -------------------------2 | | | | O | | | -------------------------3 | | | | | | O | -------------------------4 | O | | | | | | -------------------------5 | | | O | | | | -------------------------6 | | | | | O | | -------------------------
The solution shown above 2 4 6 1 3 5
is described by the sequence, which gives the column positions of the checkers for each Row from to :
ROW 1 2 3 4 5 6COLUMN 2 4 6 1 3
This was one solution to the Checker challenge. Write A program This finds all unique solution sequences to the Checker challenge (with ever growing values of ). Print the solutions using the column notation described above. Print the first three solutions in numerical order, as if the checker positions form the digits of a large number, and Then a line with the total number of solutions.
Input
A single line, contains a single integer() That's the dimension of the checkerboard.
Output
The first three lines show the first three solutions found, presented as numbers with a single space bet Ween them. The fourth line shows the total number of solutions found.
Sample Input
6
Sample Output
2 4 6 1 3 5
3 6 2 5 1 4
4 1 5 2 6 3
4
Test instructions: N Queens question, the number of ways to output the first three emissions and the available emissions
Problem-solving ideas: only need to count the output three times, the other and the same as the N queen ... (Here we use a two-dimensional array to determine if the same column is the same slash ...) Anti-timeout)
The code is as follows:
1#include <stdio.h>2 intn,t=0, vis[ -][ -],c[ -];3 voidDfsintcur)4 {5 if(cur==n+1)6 {7t++;8 if(t<4)9 {Ten for(intI=1; i<=n-1; i++) Oneprintf"%d", C[i]); Aprintf"%d\n", C[n]); - } - } the Else - { - for(intI=1; i<=n; i++) - { + if(!vis[0][i]&&!vis[1][cur+i]&&!vis[2][cur-i+n])//To determine if the conflict with the previous Queen - { +c[cur]=i; Avis[0][i]=vis[1][cur+i]=vis[2][cur-i+n]=1; atDFS (cur+1); -vis[0][i]=vis[1][cur+i]=vis[2][cur-i+n]=0; - } - } - } - } in intMain () - { toscanf"%d",&n); +Dfs1); -printf"%d\n", T); the return 0; *}
N Queen's question 2