http://poj.org/problem?id=1321
    入门放棋子问题

    1. #include <cmath>
    2. #include <cstdio>
    3. #include <iostream>
    4. #include <algorithm>
    5. #include <queue>
    6. #include <map>
    7. #include <cstring>
    8. #include <vector>
    9. #include <stack>
    10. #include <string>
    11. using namespace std;
    12. #define ll long long
    13. #define ff first
    14. #define ss second
    15. #define mp make_pair
    16. #define ph push
    17. #define pb push_back
    18. #define all(x) (x).begin(), (x).end()
    19. typedef pair<int, int> pii;
    20. char m[10][10];
    21. int vis[10];
    22. int ans = 0;
    23. int n, k;
    24. void dfs(int x, int num) {
    25. if (num >= k) {
    26. ans++;
    27. return;
    28. }
    29. for (int i = x; i < n; ++i) {
    30. for (int j = 0; j < n; ++j) {
    31. //vis 用来判断当前列是否被放过棋子
    32. if (m[i][j] == '#' && !vis[j]) {
    33. vis[j] = 1;
    34. dfs(i + 1, num + 1);
    35. vis[j] = 0;
    36. }
    37. }
    38. }
    39. }
    40. int main() {
    41. ios::sync_with_stdio(false);
    42. while (cin >> n >> k) {
    43. if (n == -1 && k == -1) {
    44. break;
    45. }
    46. memset(vis, 0, sizeof(vis));
    47. memset(m, 0, sizeof(m));
    48. for (int i = 0; i < n; ++i) {
    49. cin >> m[i];
    50. }
    51. ans = 0;
    52. dfs(0, 0);
    53. cout << ans << endl;
    54. }
    55. return 0;
    56. }

    https://www.luogu.org/problemnew/show/P1433

    1. #include <cmath>
    2. #include <cstdio>
    3. #include <iostream>
    4. #include <algorithm>
    5. #include <queue>
    6. #include <map>
    7. #include <cstring>
    8. #include <vector>
    9. #include <stack>
    10. #include <string>
    11. using namespace std;
    12. #define ff first
    13. #define ss second
    14. #define mp make_pair
    15. #define ph push
    16. #define pb push_back
    17. typedef long long ll;
    18. typedef pair<int, int> pii;
    19. typedef pair<double, double> pdd;
    20. #define all(x) (x).begin(), (x).end()
    21. const int N = 20;
    22. const int INF = 1e9 + 5;
    23. int n;
    24. double ans = INF;
    25. pdd a[20];
    26. int f[N];
    27. void dfs(pdd p, int num, double sum) {
    28. if (sum > ans) {
    29. return;
    30. }
    31. if (num >= n) {
    32. ans = min(ans, sum);
    33. return;
    34. }
    35. for (int i = 0; i < n; ++i) {
    36. if (!f[i]) {
    37. f[i] = 1;
    38. dfs(a[i], num + 1,
    39. sum + (double) sqrt((p.ff - a[i].ff) * (p.ff - a[i].ff) + (p.ss - a[i].ss) * (p.ss - a[i].ss)));
    40. f[i] = 0;
    41. }
    42. }
    43. }
    44. int main() {
    45. // ios::sync_with_stdio(false);
    46. cin >> n;
    47. memset(f, 0, sizeof(f));
    48. for (int i = 0; i < n; ++i) {
    49. cin >> a[i].ff >> a[i].ss;
    50. }
    51. dfs(mp(0,0), 0, 0);
    52. printf("%.2f\n",ans);
    53. return 0;
    54. }