fork download
  1. // ~~ icebear love attttttt ~~
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. typedef long long ll;
  6. typedef pair<int, int> ii;
  7. typedef pair<int, ii> iii;
  8.  
  9. template<class T>
  10. bool minimize(T &a, const T &b) {
  11. if (a > b) return a = b, true;
  12. return false;
  13. }
  14.  
  15. template<class T>
  16. bool maximize(T &a, const T &b) {
  17. if (a < b) return a = b, true;
  18. return false;
  19. }
  20.  
  21. #define FOR(i,a,b) for(int i=(a); i<=(b); ++i)
  22. #define FORR(i,a,b) for(int i=(a); i>=(b); --i)
  23. #define REP(i, n) for(int i=0; i<(n); ++i)
  24. #define RED(i, n) for(int i=(n)-1; i>=0; --i)
  25. #define MASK(i) (1LL << (i))
  26. #define mp make_pair
  27. #define pb push_back
  28. #define fi first
  29. #define se second
  30. #define all(x) x.begin(), x.end()
  31. #define task "robot"
  32.  
  33. const int MOD = 1e9 + 7;
  34. const int inf = 1e9 + 27092008;
  35. const ll INF = 1e18 + 27092008;
  36. const int N = 2000 + 5;
  37. int n, m, c[N][N], f[N][N];
  38. char a[N][N];
  39. ii nxt[N][N];
  40.  
  41. void init(void) {
  42. cin >> n >> m;
  43. FOR(i, 1, n) FOR(j, 1, m) {
  44. cin >> a[i][j];
  45. int x = i, y = j;
  46. if (a[i][j] == 'L') y--;
  47. if (a[i][j] == 'R') y++;
  48. if (a[i][j] == 'U') x--;
  49. if (a[i][j] == 'D') x++;
  50. nxt[i][j] = mp(x, y);
  51. }
  52. }
  53.  
  54. void process(void) {
  55. int ans = 0;
  56. stack<ii> st;
  57. FOR(i, 1, n) FOR(j, 1, m) if (c[i][j] == 0) {
  58. f[i][j] = 1;
  59. int x = i, y = j;
  60. int ep = 0;
  61. bool isCycle = false;
  62. while(true) {
  63. c[x][y] = 1;
  64. st.push(mp(x, y));
  65. int u, v; tie(u, v) = nxt[x][y];
  66. if (u <= 0 || u > n || v <= 0 || v > m) break;
  67. if (c[u][v] == 0) {
  68. f[u][v] = f[x][y] + 1;
  69. x = u; y = v;
  70. }
  71. if (c[u][v] == 1) {
  72. isCycle = true;
  73. break;
  74. }
  75. if (c[u][v] == 2) {
  76. f[x][y] += (ep = f[u][v]);
  77. break;
  78. }
  79. }
  80.  
  81. tie(x, y) = st.top();
  82. ans = max(ans, f[x][y]);
  83. if (isCycle) ep = f[x][y];
  84.  
  85. while(!st.empty()) {
  86. tie(x, y) = st.top(); st.pop();
  87. c[x][y] = 2;
  88. f[x][y] = (isCycle ? ep : ++ep);
  89. }
  90. }
  91. cout << ans << endl;
  92. }
  93.  
  94. int main() {
  95. ios_base::sync_with_stdio(0);
  96. cin.tie(0); cout.tie(0);
  97. if (fopen(task".inp", "r")) {
  98. freopen(task".inp", "r", stdin);
  99. freopen(task".out", "w", stdout);
  100. }
  101. int tc = 1;
  102. // cin >> tc;
  103. while(tc--) {
  104. init();
  105. process();
  106. }
  107. return 0;
  108. }
  109.  
Success #stdin #stdout 0s 5292KB
stdin
Standard input is empty
stdout
0