TypechoJoeTheme

IT技术分享

统计

[LeetCode 52] N-Queens II [C] [Runtime : 3 MS]

2017-06-29
/
0 评论
/
898 阅读
/
正在检测是否收录...
06/29

1. Runtime Distribution

2. Submission Details

3. Description

Follow up for N-Queens problem.

Now, instead outputting board configurations, return the total number of distinct solutions.

4. Example

There exist two distinct solutions to the 4-queens puzzle

6. Code

char ** board;
int* row;
int* col;
int** diagonal;

void label(int i, int j, int n, int mark) {
    row[i] += mark;
    col[j] += mark;

    for (int x = i, y = j; x >= 0 && y < n; x--, y++)
    {
        diagonal[x][y] += mark;
    }

    for (int x = i + 1, y = j - 1; x < n && y >= 0; x++, y--)
    {
        diagonal[x][y] += mark;
    }

    for (int x = i - 1, y = j - 1; x >= 0 && y >= 0; x--, y--)
    {
        diagonal[x][y] += mark;
    }

    for (int x = i + 1, y = j + 1; x < n && y < n; x++, y++)
    {
        diagonal[x][y] += mark;
    }
}

void palce(int* returnSize, int i, int j, int n)
{
    if (i == n)
    {
        (*returnSize)++;
        return;
    }

    for (int y = j; y < n; y++)
    {
        if (row[i] == 0 && col[y] == 0 && diagonal[i][y] == 0)
        {
            board[i][y] = 'Q';
            label(i, y, n, 1);
            palce(returnSize, i + 1, 0, n);
            label(i, y, n, -1);
            board[i][y] = '.';
        }
    }
}

int totalNQueens(int n) {

    board = (char **)malloc(sizeof(char*) * n);
    for (int i = 0; i< n; i++)
    {
        board[i] = (char *)malloc(sizeof(char) * n);
    }

    row = (int *)malloc(sizeof(int) * n);
    col = (int *)malloc(sizeof(int) * n);

    diagonal = (int **)malloc(sizeof(int *) * n);
    for (int i = 0; i< n; i++)
    {
        diagonal[i] = (int *)malloc(sizeof(int) * n);
    }

    for (int i = 0; i < n; i++)
    {
        row[i] = 0; col[i] = 0;
        for (int j = 0; j< n; j++)
        {
            board[i][j] = '.';
            diagonal[i][j] = 0;
        }
    }

    int returnSize = 0;
    palce(&returnSize, 0, 0, n);
    return returnSize;
}

7.Test

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

char ** board;
int* row;
int* col;
int** diagonal;

void label(int i, int j, int n, int mark) {
    row[i] += mark;
    col[j] += mark;

    for (int x = i, y = j; x >= 0 && y < n; x--, y++)
    {
        diagonal[x][y] += mark;
    }

    for (int x = i + 1, y = j - 1; x < n && y >= 0; x++, y--)
    {
        diagonal[x][y] += mark;
    }

    for (int x = i - 1, y = j - 1; x >= 0 && y >= 0; x--, y--)
    {
        diagonal[x][y] += mark;
    }

    for (int x = i + 1, y = j + 1; x < n && y < n; x++, y++)
    {
        diagonal[x][y] += mark;
    }
}

void palce(int* returnSize, int i, int j, int n)
{
    if (i == n)
    {
        (*returnSize)++;
        return;
    }

    for (int y = j; y < n; y++)
    {
        if (row[i] == 0 && col[y] == 0 && diagonal[i][y] == 0)
        {
            board[i][y] = 'Q';
            label(i, y, n, 1);
            palce(returnSize, i + 1, 0, n);
            label(i, y, n, -1);
            board[i][y] = '.';
        }
    }
}

int totalNQueens(int n) {

    board = (char **)malloc(sizeof(char*) * n);
    for (int i = 0; i< n; i++)
    {
        board[i] = (char *)malloc(sizeof(char) * n);
    }

    row = (int *)malloc(sizeof(int) * n);
    col = (int *)malloc(sizeof(int) * n);

    diagonal = (int **)malloc(sizeof(int *) * n);
    for (int i = 0; i< n; i++)
    {
        diagonal[i] = (int *)malloc(sizeof(int) * n);
    }

    for (int i = 0; i < n; i++)
    {
        row[i] = 0; col[i] = 0;
        for (int j = 0; j< n; j++)
        {
            board[i][j] = '.';
            diagonal[i][j] = 0;
        }
    }

    int returnSize = 0;
    palce(&returnSize, 0, 0, n);
    return returnSize;
}

int main()
{
    int n = 8;
    printf("size = %d\n", totalNQueens(n));
    system("pause");
    return 0;
}
BT
朗读
赞 · 0
版权属于:

IT技术分享

本文链接:

https://idunso.com/archives/447/(转载时请注明本文出处及文章链接)