이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
int a[4005][4005];
vector<pair<pair<int,int>, int>> adj[4005][4005];
int d[4005][4005];
int main() {
ios::sync_with_stdio(0); cin.tie(0);
int h, w; cin >> h >> w;
memset(d, -1, sizeof(d));
for (int i = 1; i <= h; i++) {
for (int j = 1; j <= w; j++) {
char c; cin >> c;
if (c=='.') a[i][j]=1;
if (c=='R') a[i][j]=2;
if (c=='F') a[i][j] = 3;
}
}
for (int i = 1; i <= h; i++) {
for (int j = 1; j <= w; j++) {
int x = a[i][j]; if (x==1) continue;
if (i==h && j==w) continue;
if (i+1 <= h && a[i+1][j]!=1) adj[i][j].push_back({{i+1, j}, (x==5-a[i+1][j])});
if (i-1 >= 1 && a[i-1][j]!=1) adj[i][j].push_back({{i-1, j}, (x==5-a[i-1][j])});
if (j+1 <= w && a[i][j+1]!=1) adj[i][j].push_back({{i, j+1}, (x==5-a[i][j+1])});
if (j-1 >= 1 && a[i][j-1] != 1) adj[i][j].push_back({{i, j-1}, (x==5-a[i][j-1])});
}
}
d[1][1] = 1;
deque<pair<int,int>> q;
q.push_back({1, 1});
while (!q.empty()) {
int i = q.front().first, j=q.front().second;
q.pop_front();
for (auto p : adj[i][j]) {
int x= p.first.first, y=p.first.second, c = p.second;
if (d[x][y]==-1 || d[x][y] > d[i][j]+c) {
d[x][y] = d[i][j]+c;
if (c==0) {
q.push_front({x, y});
} else {
q.push_back({x, y});
}
}
}
}
int ans = 1;
for (int i = 1; i <= h; i++) {
for (int j = 1; j <= w; j++) {
ans = max(ans, d[i][j]);
}
}
cout << ans << endl;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |