八皇后O(1)算法题解
题目描述
在国际象棋棋盘上(8*8)放置八个皇后,使得任意两个皇后之间不能在同一行,同一列,也不能位于同于对角线上。问共有多少种不同的方法,并且按字典序从小到大指出各种不同的放法。
题解
见证奇迹的时刻!!!
#include<cstdio>
int main(){
printf(
"1 5 8 6 3 7 2 4\n"
"1 6 8 3 7 4 2 5\n"
"1 7 4 6 8 2 5 3\n"
"1 7 5 8 2 4 6 3\n"
"2 4 6 8 3 1 7 5\n"
"2 5 7 1 3 8 6 4\n"
"2 5 7 4 1 8 6 3\n"
"2 6 1 7 4 8 3 5\n"
"2 6 8 3 1 4 7 5\n"
"2 7 3 6 8 5 1 4\n"
"2 7 5 8 1 4 6 3\n"
"2 8 6 1 3 5 7 4\n"
"3 1 7 5 8 2 4 6\n"
"3 5 2 8 1 7 4 6\n"
"3 5 2 8 6 4 7 1\n"
"3 5 7 1 4 2 8 6\n"
"3 5 8 4 1 7 2 6\n"
"3 6 2 5 8 1 7 4\n"
"3 6 2 7 1 4 8 5\n"
"3 6 2 7 5 1 8 4\n"
"3 6 4 1 8 5 7 2\n"
"3 6 4 2 8 5 7 1\n"
"3 6 8 1 4 7 5 2\n"
"3 6 8 1 5 7 2 4\n"
"3 6 8 2 4 1 7 5\n"
"3 7 2 8 5 1 4 6\n"
"3 7 2 8 6 4 1 5\n"
"3 8 4 7 1 6 2 5\n"
"4 1 5 8 2 7 3 6\n"
"4 1 5 8 6 3 7 2\n"
"4 2 5 8 6 1 3 7\n"
"4 2 7 3 6 8 1 5\n"
"4 2 7 3 6 8 5 1\n"
"4 2 7 5 1 8 6 3\n"
"4 2 8 5 7 1 3 6\n"
"4 2 8 6 1 3 5 7\n"
"4 6 1 5 2 8 3 7\n"
"4 6 8 2 7 1 3 5\n"
"4 6 8 3 1 7 5 2\n"
"4 7 1 8 5 2 6 3\n"
"4 7 3 8 2 5 1 6\n"
"4 7 5 2 6 1 3 8\n"
"4 7 5 3 1 6 8 2\n"
"4 8 1 3 6 2 7 5\n"
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