八皇后問題 c 實現

2021-04-24 22:00:48 字數 2951 閱讀 2182

**:

using system;

using system.collections.generic;

using system.text;

namespace eightqueen

static int n=8;

static char[,] board = new char[n, n];

private static int hasplacedqueen(int locx, int locy, int queennum)

}if (result > 0)                      

return 1;                       

else

}else               

return 0;              

}private static bool canplacequeen(int locx, int locy)

上方for (j = locy - 1; j >= 0; j--)}下方

for (j = locy + 1; j < n; j++)}左方

for (i = locx - 1; i >= 0; i--)}右邊

for (i = locx + 1; i < n; i++)

}左上方

i=locx-1;

j = locy - 1;

while (i >= 0 && j >= 0)

}右上方

i = locx + 1;

j = locy - 1;

while (i < n && j >= 0)

}左下方

i = locx - 1;

j = locy + 1;

while (i >= 0 && j < n)

i = locx + 1;

j = locy + 1;

while (i < n && j < n)

}return true;        

}private static void initboard(char [,] board)}}

private static void printboard()

console.writeline();

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

console.writeline();

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

-|", board[i, j]);

}console.writeline();

//console.write("+");

//for (int k = 0; k < n; k++)

////console.writeline();}}

}}//非遞迴

using system;

using system.collections.generic;

using system.text;

namespace eightqueen

}static void main(string args)

private static void tryplacequeen()

else

nextpair = getnext(current);                   }}

while (queenstack.count > 0)

private static pair getnext(pair current)

else

return target;

}private static pair getnextline(pair current)

private static int hasplacedqueen(int locx, int locy, int queennum)

}if (result > 0)                      

return 1;                       

else

}else               

return 0;              

}private static bool canplacequeen(int locx, int locy)

上方for (j = locy - 1; j >= 0; j--)}下方

for (j = locy + 1; j < n; j++)}左方

for (i = locx - 1; i >= 0; i--)}右邊

for (i = locx + 1; i < n; i++)

}左上方

i=locx-1;

j = locy - 1;

while (i >= 0 && j >= 0)

}右上方

i = locx + 1;

j = locy - 1;

while (i < n && j >= 0)

}左下方

i = locx - 1;

j = locy + 1;

while (i >= 0 && j < n)

右下i = locx + 1;

j = locy + 1;

while (i < n && j < n)

}return true;        

}private static void initboard(char [,] board)}}

private static void printboard()

console.writeline();

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

console.writeline();

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

-|", board[i, j]);

}console.writeline();}}

}}

八皇后問題遞迴實現(C )

八皇后的遞迴實現,執行程式後,平台只能顯示少部分解,在此,為了方便解集合的檢視,將解集儲存在了 f result.txt 檔案中。實現 如下 include include include using namespace std define n 8 define solu size 100 str...

八皇后問題的c 實現

八皇后問題的實現 include include using namespace std queenchess類宣告 class queenchess 建構函式,將棋盤初始化 queenchess queenchess 求解八皇后問題,並給出放置成功的棋盤總個數 void queenchess so...

回溯實現八皇后問題(C )

以下利用回溯解決八皇后問題。總共有92種結果。printqueen 是列印函式,isconfict 是用來判斷是否衝突 在同一直線和斜線則有衝突 eightqueen 為主函式,演算法思路 先把第乙個皇后放在 0,0 的位置。然後從第一行往下找,如果成功,則繼續往下探索。一直到第八行,然後回溯。如果...