|
|
[ÇóÖú]
ÀûÓÃpointer-to-pointer to½â¾öN-queens
Çó½â Óà ָÕë¶ÔÖ¸Õ룬backtracking recursion½â¾öN-queensµÄÎÊÌ⣬ÏÂÃæÊǽÌÊÚдµÄcode ¿ò¼ÜºÍ¸øµÄÌáʾ¡£
¸Õ½Ó´¥C++ûÓм¸Ìì¡£¡£¡£½ÌÊÚ¸øÁËÕâôһ¸ö×÷Òµ ÍêÈ«ãÂÁË¡¡Çó´óÉñ°ïæ×öһϠ»òÕß½âÊÍһϣ¡£¡»¹Óм¸¸öСʱ¾ÍÒª½»ÁË£¬ÍêȫûÏë·¨¡£¡£¡£
#ifndef N_queens
#define N_queens
using namespace std;
class Board
{
// private data member: size of the board
int size;
// pointer-to-pointer initialization of the board
int **chess_board;
// private member function: returns 'false' if
// the (row, col) position is not safe.
bool is_this_position_safe(int row, int col)
{
// write the appropriate code on your own that returns
// "true" if the (row,col) position is safe. If it is
// unsafe (i.e. some other queen can threaten this position)
// return "false"
}
// private member function: initializes the (n x n) chessboard
void initialize(int n)
{
size = n;
// write the appropriate code that uses the pointer-to-pointer
// method to initialize the (n x n) chessboard. Once initialized,
// put zeros in all entries. Later on, if you placed a queen in
// the (i,j)-th position, then chessboard[j] will be 1.
}
// private member function: prints the board position
void print_board()
{
std::cout << size << "-Queens Problem Solution" << std::endl;
// write the appropriate code here to print out the solved
// board as shown in the assignment description
}
// private member function: recursive backtracking
bool solve(int col)
{
// implement the recursive backtracking procedure described in
// pseudocode format in figure 1 of the description of the first
// programming assignment
}
public:
// Solves the n-Queens problem by (recursive) backtracking
void nQueens(int n)
{
initialize(n);
if (solve(0))
print_board();
else
std::cout << "There is no solution to the " << n << "-Queens Problem" << std::endl;
}
};
#endif
ÌáʾÓÐ ÀïÃæbool solve(int col) ÓÃÒÔϽṹ £¨¸½¼þ1£©
È»ºóдÍêÕâ¸öÖ®ºóÐèÒªÓÃcommand lineÈ¥Ö´ÐС¡Á½¸öcode¿ò¼Ü¶¼ÔÚwordÀÏêϸµÄ½âÊÍÔÙpdfÎļþÀï¡£¡£¡£Êýѧת¹¤³ÌÍêÈ«±±à³Ì´ò°ÜÁË¡¡»¹Óм¸¸öСʱ¾Í½»ÁË£¬Çó¾È£¡
Íò·Ö¸Ðл
command line:
// N Queens Problem via (Backtracking, which is implemented by) Recursion
// Written by Prof. Sreenivas for IE523: Financial Computing
#include <iostream>
#include "N_queens.h"
int main (int argc, char * const argv[])
{
Board x;
int board_size;
sscanf (argv[1], "%d", &board_size);
x.nQueens(board_size);
return 0;
}![ÀûÓÃpointer-to-pointer to½â¾öN-queens]()
ÆÁÄ»¿ìÕÕ 2015-09-15 14.13.16.png |
» ±¾Ìû¸½¼þ×ÊÔ´Áбí
-
»¶Ó¼à¶½ºÍ·´À¡£ºÐ¡Ä¾³æ½öÌṩ½»Á÷ƽ̨£¬²»¶Ô¸ÃÄÚÈݸºÔð¡£
±¾ÄÚÈÝÓÉÓû§×ÔÖ÷·¢²¼£¬Èç¹ûÆäÄÚÈÝÉæ¼°µ½ÖªÊ¶²úȨÎÊÌ⣬ÆäÔðÈÎÔÚÓÚÓû§±¾ÈË£¬Èç¶Ô°æÈ¨ÓÐÒìÒ飬ÇëÁªÏµÓÊÏ䣺xiaomuchong@tal.com
- ¸½¼þ 1 : f15_prog2_1_hint.docx
2015-09-16 03:14:41, 56.6 K
- ¸½¼þ 2 : f15_prog2_hint.docx
2015-09-16 03:14:43, 96.46 K
- ¸½¼þ 3 : prog2(2).pdf
2015-09-16 03:14:57, 384.16 K
» ²ÂÄãϲ»¶
|