UVa 1419 - Ugly Windows (DFS + 暴力)

题意

找出有几个在顶端的窗口。需要满足以下条件

  1. 窗口里面只有'.'
  2. 窗口边缘是完整的。

思路

在浏览器标签上呆了一个星期的题目=。= 
今天标签实在是放不下了,就去A这题,空一点位置╮(╯▽╰)╭

  1. 找出完整的窗口。
  2. 判断这个窗口里面是不是全部是.

如何找出完整的窗口? 
碰到一个字母就DFS,如果从下往上走到起点,说明这个窗口是完整的。因此在起点的时候不能往下走。

如何判断2? 
枚举出当前窗口的边界,for一下。

代码

 
  
  1. #include
  2. #include
  3. #include
  4. #include
  5. #include
  6. #include
  7. #include
  8. #include
  9. #include
  10. #include
  11. #include
  12. #include
  13. #include
  14. #include
  15. #define LL long long
  16. #define ULL unsigned long long
  17. #define SZ(x) (int)x.size()
  18. #define Lowbit(x) ((x) & (-x))
  19. #define MP(a, b) make_pair(a, b)
  20. #define MS(arr, num) memset(arr, num, sizeof(arr))
  21. #define PB push_back
  22. #define F first
  23. #define S second
  24. #define ROP freopen("input.txt", "r", stdin);
  25. #define MID(a, b) (a + ((b - a) >> 1))
  26. #define LC rt << 1, l, mid
  27. #define RC rt << 1|1, mid + 1, r
  28. #define LRT rt << 1
  29. #define RRT rt << 1|1
  30. #define BitCount(x) __builtin_popcount(x)
  31. #define BitCountll(x) __builtin_popcountll(x)
  32. #define LeftPos(x) 32 - __builtin_clz(x) - 1
  33. #define LeftPosll(x) 64 - __builtin_clzll(x) - 1
  34. const double PI = acos(-1.0);
  35. const int INF = 0x3f3f3f3f;
  36. using namespace std;
  37. const double eps = 1e-8;
  38. const int MAXN = 100 + 10;
  39. const int MOD = 1000007;
  40. const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} }; //0123,上下左右
  41. typedef pair<int, int> pii;
  42. typedef vector::iterator viti;
  43. typedef vector::iterator vitii;
  44. char mp[MAXN][MAXN];
  45. int vis[MAXN][MAXN], n, m;
  46. pii tar;
  47. vector ans;
  48. bool Check(char curWindow)
  49. {
  50. int i, j;
  51. for (i = tar.F, j = tar.S; mp[i][j] == curWindow; j++);
  52. int len = j - 2;
  53. for (i = tar.F, j = tar.S; mp[i][j] == curWindow; i++);
  54. int wide = i - 2;
  55. for (i = tar.F + 1; i <= wide; i++)
  56. for (j = tar.S + 1; j <= len; j++)
  57. if (mp[i][j] != '.') return false;
  58. return true;
  59. }
  60. void DFS(int x, int y, char curWindow)
  61. {
  62. if (mp[x][y] == '.' || mp[x][y] != curWindow) return;
  63. vis[x][y] = 1;
  64. for (int i = 0; i < 4; i++)
  65. {
  66. if (i == 1 && x == tar.F && y == tar.S) continue; //一开始的时候不能往下走
  67. int xx = x + dir[i][0], yy = y + dir[i][1];
  68. if (i == 0 && xx == tar.F && yy == tar.S) //如果往上走能到达起点
  69. {
  70. if (Check(curWindow))
  71. ans.PB(curWindow);
  72. }
  73. if (xx >= 1 && xx <= n && yy >= 1 && yy <= m && !vis[xx][yy])
  74. DFS(xx, yy, curWindow);
  75. }
  76. }
  77. int main()
  78. {
  79. //ROP;
  80. int i, j;
  81. while (scanf("%d%d", &n, &m), n + m)
  82. {
  83. ans.clear();
  84. MS(vis, 0);
  85. for (i = 1; i <= n; i++) scanf("%s", mp[i] + 1);
  86. for (i = 1; i <= n; i++)
  87. for (j = 1; j <= m; j++)
  88. if (!vis[i][j] && mp[i][j] != '.')
  89. {
  90. tar.F = i, tar.S = j;
  91. DFS(i, j, mp[i][j]);
  92. }
  93. sort(ans.begin(), ans.end());
  94. for (i = 0; i < SZ(ans); i++) putchar(ans[i]);
  95. puts("");
  96. }
  97. return 0;
  98. }
 
  

你可能感兴趣的:(UVa)