이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,l,r) for(int i = (l); i < r; i++)
#define per(i,r,l) for(int i = (r); i >= l; i--)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define pb push_back
#define ff first
#define ss second
const ll mod = 1e9 + 7, maxn = 4e3 + 5, inf = 1e9 + 5;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
int a[maxn][maxn];
int main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n, m; cin >> n >> m;
rep(i,0,n){
string s; cin >> s;
rep(j,0,m){
if(s[j] == 'R') a[i][j] = 1;
else if(s[j] == 'F') a[i][j] = 2;
}
}
vector<vector<int>> dist(n, vector<int>(m, inf));
dist[0][0] = 1;
deque<pair<int, int>> q; q.pb({0, 0});
while(sz(q)){
int i = q.front().ff, j = q.front().ss; q.pop_front();
rep(d,0,4){
int r = dx[d] + i, c = dy[d] + j;
if(r < 0 || r >= n || c < 0 || c >= m || !a[r][c]) continue;
bool w = (a[i][j] != a[r][c]);
if(dist[r][c] > dist[i][j] + w){
dist[r][c] = dist[i][j] + w;
if(w){
q.push_back({r, c});
}else{
q.push_front({r, c});
}
}
}
}
int ans = 0;
rep(i,0,n){
rep(j,0,m){
if(a[i][j]) ans = max(ans, dist[i][j]);
}
}
cout << ans;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |