N Queen Solve in C,用文件中的数据替换define

时间:2018-05-06 19:34:41

标签: c file dynamic allocation

我试图在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 ++的新手,如果有人可以帮助我吗?

1 个答案:

答案 0 :(得分:2)

以下是如何使用从文本文件中读取的数字更改N的示例。您可以使用您现在喜欢的任何数字运行该程序。

如果从文件中读取,该文件必须已创建并调用&#34; myFile.txt&#34;,您只能从中读取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;
}