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 INF LLONG_MAX
#define endl '\n'
const int mod = 1000 * 1000 * 1000 + 7;
const int N = 100005;
#define f first
#define s second
#define rep(i, a, b) for(int i = (a); i < (b); i++)
#define rrep(i, a, b) for(int i = (a); i > (b); i--)
#define vi vector<int>
#define pii pair<int, int>
mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
int dx[4] = {1,0,-1,0};
int dy[4] = {0,1,0,-1};
struct edge {
int xx , yy , val;
bool operator < (const edge& other) const {
return val > other.val; // Compare based on distance
}
};
void Solve() {
int r , c , n; cin >> r >> c >> n;
int sr , sc; cin >> sr >> sc;
int gr, gc; cin >> gr >> gc;
vector<vector<int>> vis(r + 3, vector<int>(c + 3));
vector<vector<int>> mat(r + 3, vector<int>(c + 3, -1));
vector<vector<int>> dis(r + 3, vector<int>(c + 3, INF));
for (int i = 1; i <= r; ++i) {
string s; cin >> s;
for (int j = 1; j <= c; ++j) {
if (s[j-1]=='.') mat[i][j] = 0;
else mat[i][j] = 1;
}
}
priority_queue<edge, vector<edge>> pq;
pq.push({sr, sc, mat[sr][sc]}); // Start node with distance 0
dis[sr][sc] = mat[sr][sc]; // Distance to start node is 0
while (!pq.empty()) {
auto node = pq.top();
pq.pop();
if (vis[node.xx][node.yy]) continue;
vis[node.xx][node.yy] = true;
for (int i = 0; i < 4; ++i) {
int nx = node.xx + dx[i];
int ny = node.yy + dy[i];
if (nx >= 1 && nx <= r && ny >= 1 && ny <= c && mat[nx][ny] != -1) {
int new_dist = dis[node.xx][node.yy] + mat[nx][ny];
if (new_dist < dis[nx][ny]) {
dis[nx][ny] = new_dist;
pq.push({nx, ny, new_dist});
}
}
}
}
cout << dis[gr][gc] << endl;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int t = 1;
// cin >> t;
for (int i = 1; i <= t; i++) {
Solve();
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |