#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
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]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |