递归训练1.2-N皇后(被大神作死)

#include <iostream>
using namespace std;

int con[8] = {-1, -1, -1, 1, 1, -1, 1, 1};
const int N = 15;
int c[N][N];
int ans[N];
int n;

int row[N][N];
int col[N][N];
int bev[N][N];

int fun(int, int, int);
int res;
int main()
{
    for (int i=0; i<N; i++)
    {
        ans[i] = -1;
    }
    while ((cin >> n) && n)
    {
        res = 0;
        for (int i=0; i<n; i++)
        {
            for (int j=0; j<n; j++)
            {
                c[i][j] = 0;
                row[i][j] = 0;
                col[i][j] = 0;
                bev[i][j] = 0;
            }
        }
        if (ans[n] == -1)
        {
            ans[n] = fun(0, 0, n);
        }
        cout << ans[n] << endl;
    }
    return 0;
}

int fun(int i, int j, int chenum)
{
   /* for (int u=0; u<n; u++)
    {
        for (int v=0; v<n; v++)
        {
            cout << c[u][v];
        }
        cout << "       ";
        for (int v=0; v<n; v++)
        {
            cout << row[u][v];
        }
        cout << "       ";
        for (int v=0; v<n; v++)
        {
            cout << col[u][v];
        }
        cout << "       ";
        for (int v=0; v<n; v++)
        {
            cout << bev[u][v];
        }
        cout << endl;
    }
    cout << endl;*/
    if (!chenum)
    {
        res++;
        //cout << "rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrres: " << res << endl;
        return res;
    }
    if ((i<n) && (j==n))
    {
        j = 0;
        i++;
    }
    if (i == n)
    {
        return res;
    }
    c[i][j] = 1;
    int minu, sum;
    if ((!row[i][j]) && (!col[i][j]) && (!bev[i][j]))
    {
        minu = j-i;
        sum = i+j;
        for (int p=0; p<n; p++)
        {
            row[i][p]++;
            col[p][j]++;
            if (((sum-p)<n) && ((sum-p)>=0))
            {
                bev[p][sum-p]++;
            }
            if (((p+minu)<n) && ((p+minu)>=0))
            {
                bev[p][p+minu]++;
            }
        }
        res = fun(i+1, 0, chenum-1);
        for (int p=0; p<n; p++)
        {
            row[i][p]--;
            col[p][j]--;
            if (((sum-p)<n) && ((sum-p)>=0))
            {
                bev[p][sum-p]--;
            }
            if (((p+minu)<n) && ((p+minu)>=0))
            {
                bev[p][p+minu]--;
            }
        }
    }
    c[i][j] = 0;
    if ((j == n-1) && (i+chenum > n-1))
    {
        return res;
    }
    res = fun(i, j+1, chenum);
    return res;
}
//此乃降低空间和时间复杂度的优化代码

#include <iostream>
using namespace std;

const int N = 15;
int c[N][N];
int ans[N];
int n;

int row[N];
int col[N];
int bev[4*N];

int fun(int, int, int);
int res;
int main()
{
    for (int i=0; i<N; i++)
    {
        ans[i] = -1;
    }
    while ((cin >> n) && n)
    {
        res = 0;
        for (int i=0; i<n; i++)
        {
            row[i] = 0;
            col[i] = 0;
            for (int j=0; j<n; j++)
            {
                c[i][j] = 0;
            }
        }
        for (int i=0; i<4*N; i++)
        {
            bev[i] = 0;
        }
        if (ans[n] == -1)
        {
            ans[n] = fun(0, 0, n);
        }
        cout << ans[n] << endl;
    }
    return 0;
}

int fun(int i, int j, int chenum)
{
    if (!chenum)
    {
        res++;
        return res;
    }
    if ((i<n) && (j==n))
    {
        j = 0;
        i++;
    }
    if (i == n)
    {
        return res;
    }
    c[i][j] = 1;
    int minu, sum;
    if ((!row[i]) && (!col[j]) && (!bev[j-i+3*n]) && !(bev[i+j]))
    {
        row[i] = 1;
        col[j] = 1;
        bev[i+j] = 1;
        bev[j-i+3*n] = 1;
        res = fun(i+1, 0, chenum-1);
        row[i] = 0;
        col[j] = 0;
        bev[i+j] = 0;
        bev[j-i+3*n] = 0;
    }
    c[i][j] = 0;
    if ((j == n-1) && (i+chenum > n-1))
    {
        return res;
    }
    res = fun(i, j+1, chenum);
    return res;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值