默寫八皇后

2021-06-19 17:59:36 字數 859 閱讀 8588

於是開啟ide開始練習

共計20分鐘不到

其中有十分鐘是在寫如何判斷某一點上能否放乙個皇后(橫豎斜)

這個速度還算是可以

#include #include #include "string"

#include "iostream"

#include "fstream"

#include "string.h"

#include "assert.h"

#include #include "algorithm"

#include "vector"

#define n 8

using namespace std;

int chessboard[n][n];

int qcount = 0;

void init() }}

void set(int x, int y)

void unset(int x, int y)

// whether queen can set in this position

bool testq(int x, int y)

} for (int i = 0; i < n; i++) }

for (int i = 0; i < n; i++) }

return true;

}void printchessboard()

printf("\n");

} printf("\n");

}void queen(int nowrow)

else

unset(nowrow, i);

} }}int main()

八皇后(n皇后)

n皇后問題是經典的遞迴型問題。輸入說明 n代表皇后的數量 輸出說明 每行n個數字,一行代表乙個解。例如2413代表第1行皇后放在第2列,第2行皇后放在第4列,第3行皇后放在第1列,第4行皇后放在第3列。數字都是從1開始的。樣例輸入 4樣例輸出 2413 3142 include include us...

八皇后問題

八皇后問題 ackarlix 八皇后問題是乙個古老而著名的問題,是回溯演算法的典型例題。該問題是十九世紀著名的數學家高斯 1850 年提出 在 8x8格的西洋棋上擺放八個皇后,使其不能互相攻擊,即任意兩個皇后都不能處於同一行 同一列或同一斜線上,問有多少種擺法。高斯認為有 76種方案。1854 年在...

八皇后問題

include iostream.h int a 8 8 棋盤 int r 8 結果 int i,j int count 0 void init i j 0 int judge int x,int y for int mi x 1,mj y mi 1 mi for int ri x 1,rj y 1...