N Queen 在 C 中求解,用文件中的数据替换定义

N Queen Solve in C, replace define with data from file

我正在尝试用 C 语言解决 n 皇后问题,但使用动态分配并从文件中获取数据。

所以我找到了这段代码:

/* C/C++ program to solve N Queen Problem using
backtracking */
#include<bits/stdc++.h>
#define N 4

/* A utility function to print solution */
void printSolution(int board[N][N])
{
    static int k = 1;
    printf("%d-\n",k++);
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < N; j++)
            printf(" %d ", board[i][j]);
        printf("\n");
    }
    printf("\n");
}

/* A utility function to check if a queen can
be placed on board[row][col]. Note that this
function is called when "col" queens are
already placed in columns from 0 to col -1.
So we need to check only left side for
attacking queens */
bool isSafe(int board[N][N], int row, int col)
{
    int i, j;

    /* Check this row on left side */
    for (i = 0; i < col; i++)
        if (board[row][i])
            return false;

    /* Check upper diagonal on left side */
    for (i=row, j=col; i>=0 && j>=0; i--, j--)
        if (board[i][j])
            return false;

    /* Check lower diagonal on left side */
    for (i=row, j=col; j>=0 && i<N; i++, j--)
        if (board[i][j])
            return false;

    return true;
}

/* A recursive utility function to solve N
Queen problem */
bool solveNQUtil(int board[N][N], int col)
{
    /* base case: If all queens are placed
    then return true */
    if (col == N)
    {
        printSolution(board);
        return true;
    }

    /* Consider this column and try placing
    this queen in all rows one by one */
    bool res = false;
    for (int i = 0; i < N; i++)
    {
        /* Check if queen can be placed on
        board[i][col] */
        if ( isSafe(board, i, col) )
        {
            /* Place this queen in board[i][col] */
            board[i][col] = 1;

            // Make result true if any placement
            // is possible
            res = solveNQUtil(board, col + 1) || res;

            /* If placing queen in board[i][col]
            doesn't lead to a solution, then
            remove queen from board[i][col] */
            board[i][col] = 0; // BACKTRACK
        }
    }

    /* If queen can not be place in any row in
        this column col then return false */
    return res;
}

/* This function solves the N Queen problem using
Backtracking. It mainly uses solveNQUtil() to
solve the problem. It returns false if queens
cannot be placed, otherwise return true and
prints placement of queens in the form of 1s.
Please note that there may be more than one
solutions, this function prints one of the
feasible solutions.*/
void solveNQ()
{
    int board[N][N];
    memset(board, 0, sizeof(board));

    if (solveNQUtil(board, 0) == false)
    {
        printf("Solution does not exist");
        return ;
    }

    return ;
}

// driver program to test above function
int main()
{
    solveNQ();
    return 0;
}

但我不知道如何将 #define N 转换为从 .txt 文件中获取 N,这是动态分配吗? (用memset,sizeof?) 我是C/C++的新手,有没有人能帮帮我?

下面是一个示例,说明如何使用从文本文件中读取的数字来更改 N。您现在可以 运行 使用您喜欢的任何号码的程序。

如果您从文件中读取,则该文件必须已创建并已调用 "myFile.txt",您只能从中读取 1 个整数。

您可以删除 main() 中从文件中读取数字的行,例如声明 int number = 4

我已经用4测试过了,还需要你进一步优化这段代码。这是非常原始的版本。

#include <stdio.h>
#include <stdlib.h>

#define true    1
#define false   0

void destroyBoard(int **board, int number)
{
for (int i = 0; i < number; ++i)
    free(board[i]);

free(board);
}
/* A utility function to print solution */
void printSolution(int **board, int number)
{
static int k = 1;
printf("%d-\n",k++);
for (int i = 0; i < number; i++)
{
    for (int j = 0; j < number; j++)
        printf(" %d ", board[i][j]);
    printf("\n");
}
printf("\n");
}

/* A utility function to check if a queen can
be placed on board[row][col]. Note that this
function is called when "col" queens are
already placed in columns from 0 to col -1.
So we need to check only left side for
attacking queens */
int isSafe(int **board, int row, int col, int number)
{
int i, j;

/* Check this row on left side */
for (i = 0; i < col; i++)
    if (board[row][i])
        return false;

/* Check upper diagonal on left side */
for (i=row, j=col; i>=0 && j>=0; i--, j--)
    if (board[i][j])
        return false;

/* Check lower diagonal on left side */
for (i=row, j=col; j>=0 && i<number; i++, j--)
    if (board[i][j])
        return false;

return true;
}

/* A recursive utility function to solve N
Queen problem */
int solveNQUtil(int **board, int col, int number)
{
/* base case: If all queens are placed
then return true */
if (col == number)
{
    printSolution(board, number);
    return true;
}

/* Consider this column and try placing
this queen in all rows one by one */
int res = false;
for (int i = 0; i < number; i++)
{
    /* Check if queen can be placed on
    board[i][col] */
    if ( isSafe(board, i, col, number) )
    {
        /* Place this queen in board[i][col] */
        board[i][col] = 1;

        // Make result true if any placement
        // is possible
        res = solveNQUtil(board, col + 1, number) || res;

        /* If placing queen in board[i][col]
        doesn't lead to a solution, then
        remove queen from board[i][col] */
        board[i][col] = 0; // BACKTRACK
    }
}

/* If queen can not be place in any row in
    this column col then return false */
return res;
}

/* This function solves the N Queen problem using
Backtracking. It mainly uses solveNQUtil() to
solve the problem. It returns false if queens
cannot be placed, otherwise return true and
prints placement of queens in the form of 1s.
Please note that there may be more than one
solutions, this function prints one of the
feasible solutions.*/
void solveNQ(int number)
{
int **board = malloc(number * sizeof(*board));
if (board == NULL)
{
    return;
}

for (int i = 0; i < number; ++i)
{
    board[i] = malloc(number * sizeof(**board));
    if (board[i] == NULL)
    {
        destroyBoard(board, i);
        return;
    }
}

for (int i = 0; i < number; ++i)
    for (int j = 0; j < number; ++j)
         board[i][j] = 0;


if (solveNQUtil(board, 0, number) == false)
{
    printf("Solution does not exist");
}

destroyBoard(board, number);

return ;
}

// driver program to test above function
int main(void)
{
int number = 0;
FILE *fp = NULL;

if ( (fp  = fopen("myFile.txt", "r")) == NULL)
{
    printf("Error opening file!\n");

    return -1;
}

fscanf(fp, "%d", &number);

printf("number: %d\n", number);

solveNQ(number);

fclose(fp);

return 0;
}