loli的測試——搜索
阿新 • • 發佈:2018-05-24
ont sed 位運算 重復元素 CI ret 搜索 col 斜線
今天是2018.5.24,loli給我們說要考搜索,本來以為是給初學者們考的就沒準備,然而老師說我們也要考。
T1:N皇後問題
。。。這個沒什麽好說的,真的就是普通的N皇後問題,輸出方案(n<=10),本來想用位運算保存行和斜線,但是怕在簡單題上寫炸於是就寫了很樸素的做法。
# include <cstdio> # include <iostream> # include <cstring> using namespace std; int n,A=0; bool a[12]; bool b[30]; bool c[30]; int ans[12N皇後問題]; void write() { A++; for (int i=1;i<n;i++) printf("%d ",ans[i]); printf("%d\n",ans[n]); } void dfs(int x) { if(x==n+1) { write(); return ; } for (int i=1;i<=n;i++) { if(a[i]) continue; if(b[i+x]) continue; if(c[i-x+n]) continue; a[i]=true; b[i+x]=true; c[i-x+n]=true; ans[x]=i; dfs(x+1); a[i]=false; b[i+x]=false; c[i-x+n]=false; } } int main() { freopen("Queen.in","r",stdin); freopen("Queen.out","w",stdout); scanf("%d",&n); dfs(1);if(A==0) printf("no solute!\n"); fclose(stdin); fclose(stdout); return 0; }
T2:有重復元素的排列問題
和luogu上同名題目一模一樣。然而考試時我問老師元素是不是只有‘a‘-‘z‘, 他說不一定。。。那也好辦,就按ascii碼存一下,最大到300總夠了吧,然而會很慢,於是。。。再記錄一下最大最小值就OK啦,雖然還可以離散化一下,然而對於這道題不用。最氣的是考完發了評測包發現數據中並沒有任何奇怪的東西,就是只有‘a‘-‘z‘。
loli的測試——搜索