UVa 211 - The Domino Effect (DFS)

来源:互联网 发布:python服务器端开发 编辑:程序博客网 时间:2024/06/06 03:16

题意

多米诺骨牌有28种型号,每种型号都由两个pip组成。

比如bone 1 是由 0和0组成的,bone 18是由2和6组成的。

现在给出一个pip组成的图,要求输出所有能组成的bone型号排列。

我会说我看懂题目花了一个小时吗TAT

对于每种型号的骨牌,要么横着放,要么竖着放。一直往右DFS + 回溯即可。

代码

  1. #include <stack>
  2. #include <cstdio>
  3. #include <list>
  4. #include <set>
  5. #include <iostream>
  6. #include <string>
  7. #include <vector>
  8. #include <queue>
  9. #include <functional>
  10. #include <cstring>
  11. #include <algorithm>
  12. #include <cctype>
  13. #include <string>
  14. #include <map>
  15. #include <cmath>
  16. using namespace std;
  17. #define LL long long
  18. #define ULL unsigned long long
  19. #define SZ(x) (int)x.size()
  20. #define Lowbit(x) ((x) & (-x))
  21. #define MP(a, b) make_pair(a, b)
  22. #define MS(arr, num) memset(arr, num, sizeof(arr))
  23. #define PB push_back
  24. #define X first
  25. #define Y second
  26. #define ROP freopen("input.txt", "r", stdin);
  27. #define MID(a, b) (a + ((b - a) >> 1))
  28. #define LC rt << 1, l, mid
  29. #define RC rt << 1|1, mid + 1, r
  30. #define LRT rt << 1
  31. #define RRT rt << 1|1
  32. #define BitCount(x) __builtin_popcount(x)
  33. #define BitCountll(x) __builtin_popcountll(x)
  34. #define LeftPos(x) 32 - __builtin_clz(x) - 1
  35. #define LeftPosll(x) 64 - __builtin_clzll(x) - 1
  36. const double PI = acos(-1.0);
  37. const int INF = 0x3f3f3f3f;
  38. const double eps = 1e-8;
  39. const int MAXN = 4e5 + 10;
  40. const int MOD = 1000007;
  41. const int dir[][2] = { {1, 0}, {0, 1} };
  42. int cases = 0;
  43. typedef pair<int, int> pii;
  44. typedef vector<int>::iterator viti;
  45. typedef vector<pii>::iterator vitii;
  46. const int row = 7, col = 8;
  47. int mp[row][col], cvt[row][col], vis[30], pip[row][col];
  48. int cnt;
  49. bool Input()
  50. {
  51. for (int i = 0; i < row; i++)
  52. for (int j = 0; j < col; j++) if (scanf("%d", &pip[i][j]) == -1) return false;
  53. return true;
  54. }
  55. void Init()
  56. {
  57. int num = 1;
  58. cnt = 0;
  59. for (int i = 0; i < row; i++)
  60. for (int j = i; j < row; j++) cvt[i][j] = cvt[j][i] = num++;
  61. }
  62. void Output(int p[7][8])
  63. {
  64. for (int i = 0; i < row; i++)
  65. {
  66. for (int j = 0; j < col; j++) printf("%4d", p[i][j]);
  67. puts("");
  68. }
  69. puts("");
  70. }
  71. void DFS(int x, int y)
  72. {
  73. if (y == col)
  74. {
  75. x++, y = 0;
  76. if (x == row)
  77. {
  78. cnt++;
  79. Output(mp);
  80. return;
  81. }
  82. }
  83. if (mp[x][y]) DFS(x, y + 1);
  84. else
  85. {
  86. for (int i = 0; i < 2; i++)
  87. {
  88. int xx = x + dir[i][0], yy = y + dir[i][1];
  89. if (xx < row && yy < col && !mp[xx][yy])
  90. {
  91. int bone = cvt[pip[x][y]][pip[xx][yy]];
  92. if (vis[bone]) continue;
  93. mp[x][y] = mp[xx][yy] = bone;
  94. vis[bone] = 1;
  95. DFS(x, y + 1);
  96. vis[bone] = 0;
  97. mp[x][y] = mp[xx][yy] = 0;
  98. }
  99. }
  100. }
  101. }
  102. int main()
  103. {
  104. //ROP;
  105. Init();
  106. while (1)
  107. {
  108. cnt = 0;
  109. if (!Input()) break;
  110. if (cases) printf("\n\n\n");
  111. printf("Layout #%d:\n\n", ++cases);
  112. Output(pip);
  113. printf("Maps resulting from layout #%d are:\n\n", cases);
  114. DFS(0, 0);
  115. printf("There are %d solution(s) for layout #%d.\n", cnt, cases);
  116. }
  117. return 0;
  118. }
0 0
原创粉丝点击