24小时热门版块排行榜    

查看: 366  |  回复: 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 的主题更新
信息提示
请填处理意见