标签:oid mamicode 图片 ++ code inf 开始 pause nbsp
1 #include <iostream> 2 3 using namespace std; 4 5 const int N = 10; 6 int path[N], n; 7 bool state[N]; 8 9 void dfs(int x) 10 { 11 if(x == n) 12 { 13 for(int i = 0; i < n; i++) printf("%d ", path[i]); 14 puts(""); 15 return; 16 } 17 18 for(int i = 1; i <= n; i++) 19 { 20 if(!state[i]) 21 { 22 path[x] = i; 23 state[i] = true; 24 dfs(x+1); 25 state[i] = false; 26 } 27 } 28 } 29 30 int main() 31 { 32 cin >> n; 33 34 dfs(0); 35 36 system("pause"); 37 return 0; 38 }
1 #include <iostream> 2 3 using namespace std; 4 5 const int N = 20; 6 char g[N][N]; 7 bool dg[N], udg[N], col[N]; //dg表示正斜边 udg表示反斜边 8 int n; 9 10 void dfs(int x) 11 { 12 if(x == n) 13 { 14 for(int i = 0; i < n; i++) puts(g[i]); 15 puts(""); 16 return; 17 } 18 19 for(int i = 0; i < n; i++) 20 { 21 if(!col[i] && !dg[x+i] && !udg[n-x+i]) 22 { 23 col[i] = dg[x+i] = udg[n-x+i] = true; 24 g[x][i] = ‘Q‘; 25 dfs(x+1); 26 g[x][i] = ‘.‘; 27 col[i] = dg[x+i] = udg[n-x+i] = false; 28 } 29 } 30 } 31 32 int main() 33 { 34 cin >> n; 35 for(int i = 0; i < n; i++) 36 { 37 for(int j = 0; j < n; j++) 38 { 39 g[i][j] = ‘.‘; 40 } 41 } 42 43 dfs(0); 44 45 system("pause"); 46 return 0; 47 }
标签:oid mamicode 图片 ++ code inf 开始 pause nbsp
原文地址:https://www.cnblogs.com/ZhengLijie/p/13391481.html