最后一个测试点在devc++上输出是对的,但是过不了,关了O2也不行

P1219 [USACO1.5] 八皇后 Checker Challenge

Joseph__9 @ 2024-05-21 20:36:23

#include <bits/stdc++.h>
using namespace std;

const int N = 20;
int col[N],dg[N],udg[N],g[N];
long long cnt,n;

void dfs(int u)
{
    if(u == n+1)
    {
        if(cnt < 3)
        {
            for(int i = 1;i <= n;i++) cout << g[i] << " ";
            cout << endl;
        }
        cnt++;
        return;
    }
    for(int i = 1;i <= n;i++)
    {
        if(!col[i] && !dg[u+i] && !udg[n-u+i])
        {
            g[u] = i;
            col[i] = dg[u+i] = udg[n-u+i] = true;
            dfs(u+1);
            col[i] = dg[u+i] = udg[n-u+i] = false;
        }
    }

}
int main()
{
    cin >> n;
    dfs(1);
    cout << cnt;
}

by Justin_love_coding @ 2024-05-21 21:07:36

@Joseph__9

const int N=50就行了

最多13行13列,那dg[u+i]可能是dg[26],如果空间是20就越界了


by Joseph__9 @ 2024-05-21 21:19:19

@Justin_love_coding 感谢大佬


by Justin_love_coding @ 2024-05-21 21:23:13

@Joseph__9 客气了,我也是新手


|