洛谷 - P1219 - 八皇後 - dfs
阿新 • • 發佈:2019-04-05
sca i++ _id bug space scan get long long fine
https://www.luogu.org/problemnew/show/P1219
一開始樸素檢查對角線就TLE了,給對角線編碼之後壓縮了13倍時間?
找了很久的bug居然是&&寫成了&&&,我噴了。
#include<bits/stdc++.h> using namespace std; #define ll long long bool cused[14]; bool zxused[30]; bool yxused[30]; //bool g[14][14]; int ans[30]; int atop=0; intn; int cnt=0; inline int get_zx_id(int r,int c){ //int ans=r-c+n; //printf("r=%d c=%d zid=%d\n",r,c,ans); return r-c+n; } inline int get_yx_id(int r,int c){ int rc=n+1-c; //int ans=r-rc+n; //printf("r=%d c=%d yid=%d\n",r,c,ans); return r-rc+n; } void dfs(int r){if(r>n){ cnt++; if(cnt<=3){ for(int i=0;i<atop;i++){ printf("%d%c",ans[i]," \n"[i==atop-1]); } } } for(int j=1;j<=n;j++){ if(cused[j]==0&&zxused[get_zx_id(r,j)]==0&&yxused[get_yx_id(r,j)]==0){ cused[j]=1; zxused[get_zx_id(r,j)]=1; yxused[get_yx_id(r,j)]=1; //g[r][j]=1; //ans[atop++]=r; ans[atop++]=j; dfs(r+1); atop-=1; //g[r][j]=0; zxused[get_zx_id(r,j)]=0; yxused[get_yx_id(r,j)]=0; cused[j]=0; } } } int main(){ scanf("%d",&n); /*for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ printf("%2d ",get_zx_id(i,j)); } printf("\n"); } printf("\n"); for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ printf("%2d ",get_yx_id(i,j)); } printf("\n"); } printf("\n");*/ int r=1; for(int j=1;j<=n;j++){ cused[j]=1; zxused[get_zx_id(r,j)]=1; yxused[get_yx_id(r,j)]=1; ans[atop++]=j; dfs(r+1); atop-=1; yxused[get_yx_id(r,j)]=0; zxused[get_zx_id(r,j)]=0; cused[j]=0; } printf("%d\n",cnt); }
洛谷 - P1219 - 八皇後 - dfs