#include <bits/stdc++.h> using namespace std; #define maxn 20 int map[maxn][maxn]; bool vis[3][2*maxn]; int ans, C[maxn]; int N; int tot; void print() { printf("第%d种摆放方法: ", ++tot); for(int i=0; i<N; i++) cout<<C[i]+1<<" "; cout<<endl; } void solve(int cnt) { if(cnt == N) print(); else { for(int i=0; i<N; i++) { if(!vis[0][i] && !vis[1][cnt+i] && !vis[2][cnt-i+N]) { vis[0][i] = vis[1][cnt+i] = vis[2][cnt-i+N] = true; C[cnt] = i; solve(cnt + 1); vis[0][i] = vis[1][cnt+i] = vis[2][cnt-i+N] = false; } } } } int main() { while(cin>>N && N) { tot = 0; memset(vis, false, sizeof(vis)); solve(0); printf("总共有%d种摆放方法\n", tot); } return 0; }
时间: 2024-10-05 05:31:21