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;
using pint = array <int, 2>;
vector <vector <int>> ans, vst; // 0 based
int r, c, n, dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0};
vector <string> A;
pint s, g;
int main()
{
scanf("%d %d %d", &r, &c, &n);
A.resize(r);
ans.resize(r, vector <int> (c, -1));
vst.resize(r, vector <int> (c, 0));
scanf("%d %d %d %d", &s[0], &s[1], &g[0], &g[1]);
s[0]--, s[1]--, g[0]--, g[1]--;
for (int i = 0; i < r; i++) cin >> A[i];
deque <pint> dq;
ans[s[0]][s[1]] = 0;
dq.push_front(s);
while (!dq.empty()){
pint now = dq.front(); dq.pop_front();
if (vst[now[0]][now[1]]) continue;
vst[now[0]][now[1]] = 1;
for (int k : {0, 1, 2, 3}){
int nx = now[0] + dx[k], ny = now[1] + dy[k];
if (nx < 0 || ny < 0 || nx >= r || ny >= c) continue;
if (vst[nx][ny]) continue;
if (A[nx][ny] == A[now[0]][now[1]]){
if (ans[nx][ny] == -1 || ans[nx][ny] > ans[now[0]][now[1]]){
ans[nx][ny] = ans[now[0]][now[1]];
dq.push_front({nx, ny});
}
}
else {
if (ans[nx][ny] == -1 || ans[nx][ny] > ans[now[0]][now[1]] + 1){
ans[nx][ny] = ans[now[0]][now[1]] + 1;
dq.push_back({nx, ny});
}
}
}
}
printf("%d\n", ans[g[0]][g[1]]);
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:11:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
11 | scanf("%d %d %d", &r, &c, &n);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:15:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
15 | scanf("%d %d %d %d", &s[0], &s[1], &g[0], &g[1]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |