24小时热门版块排行榜    

查看: 444  |  回复: 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 的主题更新
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[硕博家园] 售一区SCI文章T0P,我:8O.551.O54,科目全,可十急 +3 ELBZ6WwEaRj6 2026-11-04 6/300 2026-11-05 18:11 by My20PsvIExYO
[考博] 售一区SCI文章T0P,我:8O.551.O54,科目全,可十急 +3 ttH3f1oA6ace 2026-11-04 6/300 2026-11-05 18:00 by My20PsvIExYO
[公派出国] 售SCI一区文章,我:8.O.551.O.5.4,科目全,可伽急 +3 ttH3f1oA6ace 2026-11-04 7/350 2026-11-05 17:58 by My20PsvIExYO
[考研] 售SCI-T0P文章,我:8O.5.5.1.O.54,科目齐全,可+急 +3 ttH3f1oA6ace 2026-11-04 11/550 2026-11-05 17:58 by My20PsvIExYO
[公派出国] 售SCI一区T0P文章,我:8.O.55.1.O54,科目全,可伽急 +4 kvewwGtKktqB 2026-11-04 11/550 2026-11-05 17:47 by My20PsvIExYO
[硕博家园] 售SCI一区T0P文章,我:8O.55.1.O.5.4,科目齐全,可+急 +5 kvewwGtKktqB 2026-11-04 13/650 2026-11-05 17:47 by My20PsvIExYO
[考博] 售SCI一区T0P文章,我:8.O.55.1.O54,科目全,可伽急 +3 kvewwGtKktqB 2026-11-04 8/400 2026-11-05 17:47 by My20PsvIExYO
[考博] 售SCI一区T0P文章,我:8.O.55.1.O.5.4,科目全,可+急 +5 ncTVBM6AOJHF 2026-11-04 8/400 2026-11-05 17:38 by My20PsvIExYO
[考博] 售SCI一区T0P文章,我:8.O55.1.O.54,科目全,可十急 +5 ncTVBM6AOJHF 2026-11-04 14/700 2026-11-05 17:36 by My20PsvIExYO
[公派出国] 售SCI一区T0P文章,我:8O.55.1.O.5.4,科目齐全,可+急 +4 ncTVBM6AOJHF 2026-11-04 11/550 2026-11-05 17:36 by My20PsvIExYO
[考研] 售SCI一区T0P文章,我:8O.55.1.O.5.4,科目齐全,可+急 +5 ncTVBM6AOJHF 2026-11-04 10/500 2026-11-05 17:36 by My20PsvIExYO
[硕博家园] 售SCI一区文章,我:8.O.551.O.5.4,科目全,可伽急 +4 ncTVBM6AOJHF 2026-11-04 9/450 2026-11-05 17:26 by My20PsvIExYO
[考博] 售SCI一区文章,我:8.O.55.1.O.54,科目齐全,可伽急 +6 ncTVBM6AOJHF 2026-11-04 16/800 2026-11-05 17:25 by My20PsvIExYO
[公派出国] 售SCI文章,我:8O5.5.1.O.54,科目齐全,可+急 +6 ncTVBM6AOJHF 2026-11-04 19/950 2026-11-05 17:25 by My20PsvIExYO
[公派出国] 售SCI-T0P文章,我:8O.5.5.1.O.54,科目齐全,可+急 +3 ELBZ6WwEaRj6 2026-11-04 7/350 2026-11-05 16:08 by My20PsvIExYO
[考研] 售SCI一区T0P文章,我:8.O55.1.O.54,科目全,可十急 +3 ttH3f1oA6ace 2026-11-04 9/450 2026-11-05 15:46 by My20PsvIExYO
[考研] 售一区SCI文章T0P,我:8O.551.O54,科目全,可十急 +4 ncTVBM6AOJHF 2026-11-04 10/500 2026-11-05 15:24 by My20PsvIExYO
[公派出国] 售SCI-T0P文章,我:8O.5.5.1.O.54,科目齐全,可+急 +5 ncTVBM6AOJHF 2026-11-04 9/450 2026-11-05 15:13 by My20PsvIExYO
[硕博家园] 售SCI一区T0P文章,我:8O.55.1.O.54,科目全,可伽急 +4 ncTVBM6AOJHF 2026-11-04 10/500 2026-11-05 10:39 by PYZmcHr5kSaq
[基金申请] 各位基础老师们,看完中央《从严治党》的决定后,对学术界更加有信心了吧 +5 iaeyuan 2026-11-01 5/250 2026-11-04 14:43 by jinchengzi
信息提示
请填处理意见