Checker ChallengeThe challenge of checkers
Description
Check a 6x6 checkers board, where six pawns are placed on the board, so that each row and column have only one, each diagonal line (including all diagonal lines of two main diagonal lines) has at most one pawn.
See the usaco source image.
The above layout can be described using sequence 2 4 6 1 3 5. the number I represents a piece at the corresponding position of line I, as shown below:
Row number 1 2 3 4 5 6 column number 2 4 6 1 3 5
This is just a solution for checkers. Compile a program to find the solutions for all checkers. And output the sequence methods above. The solution is ordered in alphabetical order. Output the first three solutions. The last row is the total number of solutions.
Note: For a larger N (board size N x N), your program should be improved to be more effective. Do not calculate all solutions in advance and only output them. This is cheating. If you insist on cheating, the account you log on to USACO Training will be deleted without warning.
Format
PROGRAM NAME: Checker
INPUT FORMAT:
(Checker. in)
A number N (6 <= N <= 13) indicates that the board is N x N.
OUTPUT FORMAT:
(Checker. out)
The first three actions are the first three solutions. The two numbers of each solution are separated by a space. The fourth row has only one number, indicating the total number of solutions.
SAMPLE INPUT
6
SAMPLE OUTPUT
2 4 6 1 3 5 3 6 2 5 1 4 4 1 5 2 6 3 4
Compiling...
Compile: OK
Executing...
Test 1: test OK [0.000 secs, 276 KB]
Test 2: test OK [0.000 secs, 276 KB]
Test 3: test OK [0.000 secs, 276 KB]
Test 4: test OK [0.000 secs, 276 KB]
Test 5: test OK [0.000 secs, 276 KB]
Test 6: test OK [0.000 secs, 276 KB]
Test 7: test OK [0.054 secs, 276 KB]
Test 8: test OK [0.162 secs, 276 KB]
All tests OK.
Your program ('checker ') produced all correct answers! This is your submission #4 for this problem. Congratulations!
Analysis:
Amount. Classic N queen question
It makes me very depressed.
Start hitting the bare dfs,
Result The Last vertex times out,
I tried a lot of pruning and optimization,
Not very useful,
I don't know much about the methods I read online,
In particular, the symmetric optimization left me speechless...
So I used bitwise operations to calculate the number of solutions,
For more information about bit operations, see the M67 article,
There is basically no change. The specific principle is unclear...
For bit operations, refer to M67.
Http://www.matrix67.com/blog/archives/266
{ID: codeway3PROG: checkerLANG: PASCAL}program checker; type ll=array[1..15,1..15]of integer; var i,j,n,bb,m,k,l,js:longint; a:ll; b:array[1..13]of longint; procedure doing(x,y:longint;aa:ll); var i,j:longint; begin if m>3 then exit; for i:=1 to n do begin aa[x,i]:=1; aa[i,y]:=1; if i+y-x in [1..n] then aa[i,i+y-x]:=1; if x+y-i in [1..n] then aa[i,x+y-i]:=1; end; b[x]:=y; if x=n then begin if m<3 then begin for i:=1 to n-1 do write(b[i],' '); writeln(b[n]); end; inc(m); b[x]:=0; exit; end; for i:=1 to n do if aa[x+1,i]=0 then doing(x+1,i,aa); b[x]:=0; end; procedure test(row,ld,rd:longint); var pos,p:longint; begin if row<>js then begin pos:=js and not (row or ld or rd); while pos<>0 do begin p:=pos and -pos; pos:=pos-p; test(row+p,(ld+p)<<1,(rd+p)>>1); end; end else inc(m); end; begin assign(input,'checker.in'); reset(input); assign(output,'checker.out'); rewrite(output); readln(n); m:=0; for i:=1 to n do doing(1,i,a); m:=0; js:=(1<<n)-1; test(0,0,0); writeln(m); close(input); close(output); end.