이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
/**
 *  In the name of GOD
 *  author : Behradm
**/
#include "bits/stdc++.h"
 
using namespace std;
 
#define sz(x) (int) ((x).size())
 
const int N = 4e3 + 5;
char s[N][N];
int tag[N][N];
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
 
int n, m;
 
bool Good(int x, int y) {
  return (0 <= x && x < n && 0 <= y && y < m && s[x][y] != '.');
}
 
void dfs(int i, int j, int c) {
  tag[i][j] = c;
  for (int k = 0; k < 4; k++) {
    int x = i + dx[k], y = j + dy[k];
    if (Good(x, y) && s[x][y] == s[i][j] && tag[x][y] == -1) {
      dfs(x, y, c);
    }
  }
}
 
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> s[i][j];
      tag[i][j] = -1;
    }
  }
  
  int c = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (tag[i][j] == -1 && s[i][j] != '.')
        dfs(i, j, c++);
    }
  }
 
  //[0, c - 1]
  vector<vector<int>> g(c);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (s[i][j] != 'R')
        continue;
      for (int k = 0; k < 4; k++) {
        int x = i + dx[k], y = j + dy[k];
        if (Good(x, y) && (s[i][j] == 'R' && s[x][y] == 'F')) {
          int p = tag[i][j], q = tag[x][y];
          g[p].push_back(q);
          g[q].push_back(p);
        }
      }
    }
  }
  
  for (int i = 0; i < c; i++) {
    sort(g[i].begin(), g[i].end());
    g[i].resize(unique(g[i].begin(), g[i].end()) - g[i].begin());
  }
 
  vector<int> h(c);
  queue<int> q;
  q.push(0), h[0] = 1;
  int ans = 1;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int v : g[u]) {
      if (h[v] == 0) {
        h[v] = h[u] + 1;
        ans = max(ans, h[v]);
        q.push(v);
      }
    }
  }
  cout << ans << '\n';
  return 0;
}
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |