排列数字
给定一个整数 n,将数字 1∼n排成一排,将会有很多种排列方法。现在,请你按照字典序将所有的排列方法输出。
输入格式:
共一行,包含一个整数 n。
输出格式:
按字典序输出所有排列方案,每个方案占一行。
数据范围:
1≤n≤7
输入样例:
输出样例:
1 2 3 4 5 6
| 1 2 3 1 3 2 2 1 3 2 3 1 3 1 2 3 2 1
|
代码示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| #include<iostream>
using namespace std;
const int N = 10; int n; int state[N], path[N];
void dfs(int u) { if(u > n) { for(int i = 1; i <= n; i ++) { cout << path[i] << " "; } cout << endl; } for(int i = 1; i <= n; i ++) { if(!state[i]) { state[i] = 1; path[u] = i; dfs(u + 1); state[i] = 0; } } }
int main() { cin >> n; dfs(1); return 0; }
|
n-皇后问题
n−皇后问题是指将 n 个皇后放在 n × n 的国际象棋棋盘上,使得皇后不能相互攻击到,即任意两个皇后都不能处于同一行、同一列或同一斜线上。
现在给定整数 n ,请你输出所有的满足条件的棋子摆法。
输入格式:
共一行,包含一个整数 n。
输出格式:
每个解决方案占 n 行,每行输出一个长度为 n 的字符串,用来表示完整的棋盘状态。
其中 . 表示某一个位置的方格状态为空,Q表示某一个位置的方格上摆着皇后。
每个方案输出完成后,输出一个空行。
数据范围:
1≤n≤9
输入样例:
输出样例:
1 2 3 4 5 6 7 8 9
| .Q.. ...Q Q... ..Q.
..Q. Q... ...Q .Q..
|
代码示例:
第一种方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| #include<iostream>
using namespace std;
const int N = 11; int n; char q[N][N]; bool dg[N * 2], udg[N * 2], col[N];
void dfs(int r) { if(r == n + 1) { for(int i = 1; i <= n; i ++) { for(int j = 1; j <= n; j ++) cout << q[i][j]; cout << endl; } cout << endl; return; }
for(int i = 1; i <= n; i ++) { if(!col[i] && !dg[r + i] && !udg[n - i + r]) { q[r][i] = 'Q'; col[i] = dg[r + i] = udg[n - i + r] = true; dfs(r + 1); col[i] = dg[r + i] = udg[n - i + r] = false; q[r][i] = '.'; } } }
int main() {
cin >> n;
for(int i = 1; i <= n; i ++) { for(int j = 1; j <= n; j ++) { q[i][j] = '.'; } }
dfs(1);
return 0; }
|
第二种方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| #include<iostream>
using namespace std;
const int N = 11; int n; char q[N][N]; bool dg[N * 2], udg[N * 2], col[N], row[N];
void dfs(int x, int y, int s) { if(y == n) { y = 0; x ++; } if(x == n) { if(s == n) { for(int i = 0; i < n; i ++) cout << q[i] << endl; cout << endl; }
return; } q[x][y] = '.'; dfs(x, y + 1, s); if(!row[x] && !col[y] && !dg[x + y] && !udg[x - y + n]) { q[x][y] = 'Q'; row[x] = col[y] = dg[x + y] = udg[x - y + n] = true; dfs(x, y + 1, s + 1); row[x] = col[y] = dg[x + y] = udg[x - y + n] = false; q[x][y] = '.'; }
}
int main() {
cin >> n;
dfs(0, 0, 0);
return 0; }
|
树的重心
给定一颗树,树中包含 n 个结点(编号 1∼n)和 n−1 条无向边。
请你找到树的重心,并输出将重心删除后,剩余各个连通块中点数的最大值。
重心定义:重心是指树中的一个结点,如果将这个点删除后,剩余各个连通块中点数的最大值最小,那么这个节点被称为树的重心。
输入格式:
第一行包含整数 n,表示树的结点数。
接下来 n−1 行,每行包含两个整数 a 和 b,表示点 a 和点 b 之间存在一条边。
输出格式:
输出一个整数 m,表示将重心删除后,剩余各个连通块中点数的最大值。
数据范围:
1≤n≤105
输入样例:
1 2 3 4 5 6 7 8 9
| 9 1 2 1 7 1 4 2 8 2 5 4 3 3 9 4 6
|
输出样例:
代码示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| #include<iostream> #include<vector> #include<algorithm>
using namespace std;
const int N = 1e5 + 10; int n; vector<int> tree[N]; int size_[N]; int ans = N;
int dfs(int u, int parent) { size_[u] = 1; int max_part = 0;
for(int v : tree[u]) { if(v == parent) continue; dfs(v, u); size_[u] += size_[v]; max_part = max(max_part, size_[v]); }
max_part = max(max_part, n - size_[u]);
ans = min(ans, max_part);
return size_[u]; }
int main() {
cin >> n;
int a, b;
for(int i = 0; i < n - 1; i ++) { cin >> a >> b;
tree[a].push_back(b); tree[b].push_back(a); }
dfs(1, -1);
cout << ans;
return 0; }
|