±±¾©Ê¯ÓÍ»¯¹¤Ñ§Ôº2026ÄêÑо¿ÉúÕÐÉú½ÓÊÕµ÷¼Á¹«¸æ
²é¿´: 378  |  »Ø¸´: 0

qdfzl

½ð³æ (СÓÐÃûÆø)

[ÇóÖú] ÀûÓÃ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

» ²ÂÄãϲ»¶

ÒÑÔÄ   »Ø¸´´ËÂ¥   ¹Ø×¢TA ¸øTA·¢ÏûÏ¢ ËÍTAºì»¨ TAµÄ»ØÌû
Ïà¹Ø°æ¿éÌø×ª ÎÒÒª¶©ÔÄÂ¥Ö÷ qdfzl µÄÖ÷Ìâ¸üÐÂ
×î¾ßÈËÆøÈÈÌûÍÆ¼ö [²é¿´È«²¿] ×÷Õß »Ø/¿´ ×îºó·¢±í
[¿¼ÑÐ] 319Çóµ÷¼Á +11 Ì«ÈÝÒ×1018 2026-04-01 11/550 2026-04-02 10:32 by ²»³Ôô~µÄ؈
[¿¼ÑÐ] 301Çóµ÷¼Á +5 ÂæÍÕÄÐÈË 2026-04-02 5/250 2026-04-02 10:18 by longlotian
[¿¼ÑÐ] 070305¸ß·Ö×Ó»¯Ñ§ÓëÎïÀí 304·ÖÇóµ÷¼Á +14 c297914 2026-03-28 14/700 2026-04-02 09:35 by ÐÇ¿ÕÐÇÔÂ
[¿¼ÑÐ] 348»·¾³¹¤³Ìµ÷¼Á +3 ÎâÑå׿24k 2026-04-01 3/150 2026-04-02 09:14 by nanaliuyun
[¿¼ÑÐ] 805600ר˶²ÄÁÏÓ뻯¹¤348·ÖÇóµ÷¼Á +6 ÉÏѧÀ²£¡ 2026-04-01 6/300 2026-04-02 07:49 by 2026²ÄÁϵ÷¼Á
[¿¼ÑÐ] Ó¢Ò»ÊýÒ»408£¬×Ü·Ö284£¬¶þÕ½Õæ³ÏÇóµ÷¼Á +12 12.27 2026-03-30 14/700 2026-04-02 00:18 by ÐÀϲ777
[¿¼ÑÐ] 265Çóµ÷¼Á +3 ÁºÁºÐ£Ð£ 2026-04-01 3/150 2026-04-01 22:36 by barlinike
[¿¼ÑÐ] 0856£¬269·ÖÇóµ÷¼Á +8 ÓÐѧÉϾÍÐÐÇóÇóÁ 2026-03-30 11/550 2026-04-01 22:33 by 2026²ÄÁϵ÷¼Á
[¿¼ÑÐ] 0710ÉúÎïѧ¿¼Ñе÷¼Á +3 Àî¶àÃ×lee. 2026-03-27 4/200 2026-04-01 16:21 by zzchen2000
[¿¼ÑÐ] »·¾³¹¤³Ìµ÷¼Á +9 hyzzzzzzz. 2026-04-01 9/450 2026-04-01 14:20 by salamander`
[¿¼ÑÐ] 267Çóµ÷¼Á +13 uiybh 2026-03-31 13/650 2026-04-01 10:25 by ̽123
[¿¼ÑÐ] 326Çóµ÷¼Á +4 áÌáÌ×Ð 2026-03-31 4/200 2026-04-01 09:58 by ÎҵĴ¬Îҵĺ£
[¿¼ÑÐ] 288×ÊÔ´Óë»·¾³×¨Ë¶Çóµ÷¼Á£¬²»ÏÞרҵ£¬ÓÐѧÉϾÍÐÐ +25 lllllos 2026-03-30 26/1300 2026-04-01 09:52 by Ò»Ö»ºÃ¹û×Ó?
[¿¼ÑÐ] 085701»·¾³¹¤³Ì£¬267Çóµ÷¼Á +17 minht 2026-03-26 17/850 2026-04-01 09:11 by xiayizhi
[¿¼ÑÐ] ¸´ÊÔµ÷¼Á +7 Ë«ÂíβƦÀϰå2 2026-03-31 7/350 2026-03-31 19:49 by Dyhoer
[¿¼ÑÐ] 083000»·¾³¿ÆÑ§Ó빤³Ìµ÷¼Á£¬×Ü·Ö281 +4 ³È×Ó£¨Ê¤Ò⣩ 2026-03-30 4/200 2026-03-31 00:44 by Linzejun
[¿¼ÑÐ] 291Çóµ÷¼Á +5 Y-cap 2026-03-29 6/300 2026-03-29 13:18 by mumin1990
[¿¼ÑÐ] ¸´ÊÔµ÷¼Á +3 raojunqi0129 2026-03-28 3/150 2026-03-28 15:27 by ÂäÉ˼
[¿¼ÑÐ] ²ÄÁÏÓ뻯¹¤£¨0856£©304ÇóBÇøµ÷¼Á +8 Çñgl 2026-03-27 8/400 2026-03-28 12:42 by ÌÆãå¶ù
[¿¼ÑÐ] 0856µ÷¼Á +5 ÇóÇóÈÃÎÒÓÐÊé¶Á° 2026-03-26 6/300 2026-03-27 15:12 by caszguilin
ÐÅÏ¢Ìáʾ
ÇëÌî´¦ÀíÒâ¼û