fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int n, m;
  4. char c[2005][2005];
  5. int vis[2005][2005];
  6. bool ok(int i, int j) {
  7. return i >= 1 && i <= n && j >= 1 && j <= m;
  8. }
  9. struct tn {
  10. int x, y;
  11. char before;
  12. };
  13. int main() {
  14. ios::sync_with_stdio(false);
  15. cin.tie(0);
  16. cin >> n >> m;
  17. for (int i = 1; i <= n; i++) {
  18. for (int j = 1; j <= m; j++) {
  19. cin >> c[i][j];
  20. }
  21. }
  22. map<char, pair<int, int>> mp;
  23. mp['D'] = {1, 0};
  24. mp['R'] = {0, 1};
  25. mp['U'] = {-1, 0};
  26. mp['L'] = {0, -1};
  27. queue<tn> q;
  28. for (int i = 1; i <= n; i++) {
  29. for (int j = 1; j <= m; j++) {
  30. if (c[i][j] != '#' && c[i][j] != '.') {
  31. q.push({i, j, 'F'});
  32. vis[i][j]++;
  33. }
  34. }
  35. }
  36. while (q.size()) {
  37. int x = q.front().x;
  38. int y = q.front().y;
  39. char before = q.front().before;
  40. q.pop();
  41. if (c[x][y] == '.') {
  42. int nx = x + mp[before].first;
  43. int ny = y + mp[before].second;
  44. if (ok(nx, ny)) {
  45. if (c[nx][ny] == '#') {
  46. vis[nx][ny]++;
  47. }
  48. else {
  49. if (c[nx][ny] != '.') {
  50. if (vis[nx][ny] == 0) {
  51. vis[nx][ny]++;
  52. q.push({nx, ny, before});
  53. }
  54. }
  55. else {
  56. if (vis[nx][ny] < 4) {
  57. vis[nx][ny]++;
  58. q.push({nx, ny, before});
  59. }
  60. }
  61. }
  62. }
  63. }
  64. else {
  65. int nx = mp[c[x][y]].first + x;
  66. int ny = mp[c[x][y]].second + y;
  67. if (ok(nx, ny)) {
  68. if (c[nx][ny] == '#') {
  69. vis[nx][ny]++;
  70. }
  71. else {
  72. if (c[nx][ny] != '.') {
  73. if (vis[nx][ny] == 0) {
  74. vis[nx][ny]++;
  75. q.push({nx, ny, c[x][y]});
  76. }
  77. }
  78. else {
  79. if (vis[nx][ny] < 4) {
  80. vis[nx][ny]++;
  81. q.push({nx, ny, c[x][y]});
  82. }
  83. }
  84. }
  85. }
  86. }
  87. }
  88. int ans = 0;
  89. for (int i = 1; i <= n; i++) {
  90. for (int j = 1; j <= m; j++) {
  91. if (c[i][j] == '#') {
  92. if (vis[i][j] > 0) {
  93. ans++;
  94. }
  95. }
  96. }
  97. }
  98. cout << ans << '\n';
  99. return 0;
  100. }
  101.  
  102. /*
  103. break
  104. could
  105. misty
  106. phone
  107. deads
  108. */
  109.  
Success #stdin #stdout 0.01s 5264KB
stdin
Standard input is empty
stdout
0