lizhehao2009 @ 2023-08-23 15:44:42
本蒟蒻在调试代码时发现当
#include<bits/stdc++.h>
using namespace std;
struct node
{
int x,y,cnt;
};
bool visx[15],visy[15],visleft[30],visright[30];
int ans[15],n,ansnum=0;
void print()
{
for (int i=1;i<=n;i++)
{
printf("%d ",ans[i]);
}
printf("\n");
}
void dfs(int x,int y,int cnt)
{
if (cnt==n)
{
ansnum++;
if (ansnum<=3)
{
print();
}
return;
}
if (x==n+1)
{
return;
}
if (y==n+1)
{
dfs(x+1,1,cnt);
return;
}
if (visx[x]==0&&visy[y]==0&&visleft[x+y]==0&&visright[x-y+13]==0)
{
visx[x]=1;
visy[y]=1;
visleft[x+y]=1;
visright[x-y+13]=1;
ans[cnt+1]=y;
dfs(x,y+1,cnt+1);
visx[x]=0;
visy[y]=0;
visleft[x+y]=0;
visright[x-y+13]=0;
}
dfs(x,y+1,cnt);
}
int main()
{
scanf("%d",&n);
dfs(1,1,0);
printf("%d",ansnum);
return 0;
}
by yaohongjia @ 2023-08-23 15:49:38
#include<cstdio>
#include<iostream>
using namespace std;
int ans[14],check[3][28]={0},sum=0,n;
void eq(int line)
{
if(line>n)
{
sum++;
if(sum>3) return;
else
{
for(int i=1;i<=n;i++) printf("%d ",ans[i]);
printf("\n");
return;
}
}
for(int i=1;i<=n;i++)
{
if((!check[0][i])&&(!check[1][line+i])&&(!check[2][line-i+n]))
{
ans[line]=i;
check[0][i]=1; check[1][line+i]=1; check[2][line-i+n]=1;
eq(line+1);
check[0][i]=0; check[1][line+i]=0; check[2][line-i+n]=0;
}
}
}
int main()
{
scanf("%d",&n);
eq(1);
printf("%d",sum);
return 0;
}
by jiangjiangQwQ @ 2023-08-23 15:51:05
@lizhehao2009 你的程序在
by jiangjiangQwQ @ 2023-08-23 15:52:29
@yaohongjia 粘个题解干嘛。这并不能解决 lz 代码的TLE。
by yaohongjia @ 2023-08-23 18:17:47
对不起
by lizhehao2009 @ 2023-08-23 22:21:02
@Super_excavator 好像还是会TLE三个点~
#include<bits/stdc++.h>
using namespace std;
struct node
{
int x,y,cnt;
};
bool visx[15],visy[15],visleft[30],visright[30];
int ans[15],n,ansnum=0;
void print()
{
for (int i=1;i<=n;i++)
{
printf("%d ",ans[i]);
}
printf("\n");
}
void dfs(int x,int y,int cnt)
{
if (cnt==n)
{
ansnum++;
if (ansnum<=3)
{
print();
}
return;
}
if (x==n+1)
{
return;
}
if (y==n+1)
{
dfs(x+1,1,cnt);
return;
}
if (visx[x]==0&&visy[y]==0&&visleft[x+y]==0&&visright[x-y+13]==0)
{
visx[x]=1;
visy[y]=1;
visleft[x+y]=1;
visright[x-y+13]=1;
ans[cnt+1]=y;
dfs(x+1,1,cnt+1);//已修改
visx[x]=0;
visy[y]=0;
visleft[x+y]=0;
visright[x-y+13]=0;
}
dfs(x,y+1,cnt);
}
int main()
{
scanf("%d",&n);
dfs(1,1,0);
printf("%d",ansnum);
return 0;
}
by jiangjiangQwQ @ 2023-08-24 16:04:34
@Super_excavator 没事。