This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define sp << " " <<
#define nl << "\n"
vector<int> e;
int find(int u){
return e[u] < 0 ? u : e[u] = find(e[u]);
}
void unite(int u, int v){
u = find(u), v = find(v);
if(u==v) return;
if(e[u] > e[v]) swap(u, v);
e[u] += e[v], e[v] = u;
}
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
signed main(){
cin.tie(0)->sync_with_stdio(0);
int h, w; cin >> h >> w;
int g[h][w];
e.assign(h*w, -1);
for(int i=0; i<h; ++i){
for(int j=0; j<w; ++j){
char c; cin >> c;
if(c=='.') g[i][j] = 0;
if(c=='F') g[i][j] = 1;
if(c=='R') g[i][j] = 2;
}
}
int dist[h][w];
for(int i=0; i<h; ++i)
fill(dist[i], dist[i]+w, 1e18);
dist[0][0] = 1;
priority_queue<array<int, 3>> q;
q.push({-1, 0, 0});
int ans = 0;
while(!q.empty()){
int x = q.top()[1], y = q.top()[2], d = -q.top()[0];
q.pop();
if(d!=dist[x][y]) continue;
for(int k=0; k<4; ++k){
int i = x+dx[k], j = y+dy[k];
if(i<0 or j<0 or i==h or j==w or !g[i][j]) continue;
int w = g[i][j]!=g[x][y];
if(dist[i][j] > dist[x][y] + w){
dist[i][j] = dist[x][y] + w;
q.push({-dist[i][j], i, j});
}
}
ans = d;
}
cout << ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |