52wyd @ 2023-02-15 15:49:30
我的AC代码,开了O2优化后,最后两个测试点WA了。。。 我多次提交了这个代码,分别是不开O2和开O2,佬们可以在我的提交中看见。 这是我的代码:
#include <cstdio>
#include <algorithm>
using namespace std;
int n;
int res[5][15], num;
int zl_ed_lie[30], zl_ed_zuoshang[30], zl_ed_youxia[30];
int now[15];
void dfs(int hang_start)
{
if (hang_start == n + 1)
{
num ++;
if (num <= 3)
for (int i = 1; i <= n; i ++)
res[num][i] = now[i];
return;
}
for (int j = 1; j <= n; j ++)
{
if (zl_ed_lie[j] || zl_ed_zuoshang[hang_start-j+1] || zl_ed_youxia[hang_start+j])
continue;
now[hang_start] = j;
zl_ed_lie[j] = 1;
zl_ed_zuoshang[hang_start-j+1] = 1;
zl_ed_youxia[hang_start+j] = 1;
dfs(hang_start + 1);
zl_ed_lie[j] = 0;
zl_ed_zuoshang[hang_start-j+1] = 0;
zl_ed_youxia[hang_start+j] = 0;
}
}
int main()
{
scanf("%d", &n);
dfs(1);
for (int i = 1; i <= num && i <= 3; i ++)
{
for (int j = 1; j <= n; j ++)
{
if (j != 1)
printf(" ");
printf("%d", res[i][j]);
}
printf("\n");
}
printf("%d\n", num);
return 0;
}
by 编码落寞 @ 2023-02-15 16:03:49
@52wyd
dfs函数的结尾加个return试一下
by 52wyd @ 2023-02-15 18:36:06
@编码落寞 加了return后还是不能AC