Submission #64575969
Source Code Expand
#include <bits/stdc++.h>
#define int long long
using namespace std;
constexpr int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
constexpr int dxUlt[4] = {2, 0, -2, 0}, dyUlt[4] = {0, 2, 0, -2};
constexpr int MAXN = 1010, INF = 0x7f7f7f7f, MAXM = 1e6 + 10;
int n, m, stx, sty, edx, edy, idx;
char Gew[MAXN][MAXN];
int Head[MAXM], dis[MAXM];
bool vis[MAXM];
struct Graph {
int v, w, nxt;
Graph (int v = 0, int w = 0, int nxt = 0):v(v), w(w), nxt(nxt){};
} Edge[12000005];
inline void AddEdge (int u, int v, int w) { Edge[idx] = Graph (v, w, Head[u]), Head[u] = idx ++; }
#define getID(x,y) ((x - 1) * m + y)
inline void MK_Edge (int x, int y) {
int u = getID (x, y);
for (int i = 0; i < 4; i ++) {
int nx = x + dx[i], ny = y + dy[i], v = getID (nx, ny);
if (nx < 1 || nx > n || ny < 1 || ny > m) continue;
if (Gew[nx][ny] == '#') {
AddEdge (u, v, 1);
int nxUlt = x + dxUlt[i], nyUlt = y + dyUlt[i], vUlt = getID (nxUlt, nyUlt);
if (nxUlt < 1 || nxUlt > n || nyUlt < 1 || nyUlt > m || Gew[nxUlt][nyUlt] == '.') continue;
AddEdge (u, vUlt, 1);
} else {
AddEdge (u, v, 0);
}
}
}
#define pii pair<int,int>
#define mkp make_pair
inline void dijkstra (int st, int ed) {
priority_queue <pii, vector<pii>, greater<pii>> q;
fill (dis, dis + n * m + 1, INF);
fill (vis, vis + n * m + 1, false);
dis[st] = 0, q.push (mkp (dis[st], st));
while (!q.empty()) {
int u = q.top().second; q.pop();
if (vis[u]) continue;
vis[u] = true;
for (int i = Head[u]; ~i; i = Edge[i].nxt) {
int v = Edge[i].v, w = Edge[i].w;
if (dis[v] > dis[u] + w) {
dis[v] = dis[u] + w;
q.push (mkp (dis[v], v));
}
}
}
cout << dis[ed] << "\n";
return;
}
signed main() {
// freopen ("data.txt", "r", stdin);
// freopen ("ABC400.out", "w", stdout);
cin.tie(0) -> sync_with_stdio(0);
cout.tie(0) -> sync_with_stdio(0);
cin >> n >> m, memset (Head, -1, sizeof (Head));
for (int i = 1; i <= n; i ++) {
for (int j = 1; j <= m; j ++)
cin >> Gew[i][j];
}
cin >> stx >> sty >> edx >> edy;
for (int i = 1; i <= n; i ++) {
for (int j = 1; j <= m; j ++)
MK_Edge (i, j);
}
dijkstra (getID (stx, sty), getID (edx, edy));
return 0;
}
Submission Info
Judge Result
| Set Name |
Sample |
All |
| Score / Max Score |
0 / 0 |
400 / 400 |
| Status |
|
|
| Set Name |
Test Cases |
| Sample |
example_00.txt, example_01.txt, example_02.txt, example_03.txt |
| All |
example_00.txt, example_01.txt, example_02.txt, example_03.txt, hand_00.txt, hand_01.txt, hand_02.txt, hand_03.txt, hand_04.txt, hand_05.txt, hand_06.txt, hand_07.txt, hand_08.txt, hand_09.txt, random_00.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt |
| Case Name |
Status |
Exec Time |
Memory |
| example_00.txt |
AC |
107 ms |
292536 KiB |
| example_01.txt |
AC |
106 ms |
292532 KiB |
| example_02.txt |
AC |
107 ms |
292520 KiB |
| example_03.txt |
AC |
106 ms |
292556 KiB |
| hand_00.txt |
AC |
366 ms |
302352 KiB |
| hand_01.txt |
AC |
364 ms |
302292 KiB |
| hand_02.txt |
AC |
355 ms |
302484 KiB |
| hand_03.txt |
AC |
366 ms |
302296 KiB |
| hand_04.txt |
AC |
326 ms |
302344 KiB |
| hand_05.txt |
AC |
251 ms |
302540 KiB |
| hand_06.txt |
AC |
251 ms |
302540 KiB |
| hand_07.txt |
AC |
237 ms |
310280 KiB |
| hand_08.txt |
AC |
175 ms |
302272 KiB |
| hand_09.txt |
AC |
107 ms |
292424 KiB |
| random_00.txt |
AC |
304 ms |
301872 KiB |
| random_01.txt |
AC |
301 ms |
301700 KiB |
| random_02.txt |
AC |
308 ms |
302096 KiB |
| random_03.txt |
AC |
327 ms |
301972 KiB |
| random_04.txt |
AC |
308 ms |
301456 KiB |
| random_05.txt |
AC |
322 ms |
301528 KiB |
| random_06.txt |
AC |
285 ms |
309908 KiB |
| random_07.txt |
AC |
278 ms |
309916 KiB |
| random_08.txt |
AC |
250 ms |
305916 KiB |
| random_09.txt |
AC |
292 ms |
309516 KiB |
| random_10.txt |
AC |
265 ms |
305280 KiB |
| random_11.txt |
AC |
313 ms |
309696 KiB |
| random_12.txt |
AC |
325 ms |
301920 KiB |
| random_13.txt |
AC |
317 ms |
301912 KiB |
| random_14.txt |
AC |
320 ms |
301768 KiB |
| random_15.txt |
AC |
322 ms |
302192 KiB |
| random_16.txt |
AC |
304 ms |
301956 KiB |
| random_17.txt |
AC |
339 ms |
301864 KiB |
| random_18.txt |
AC |
322 ms |
301988 KiB |
| random_19.txt |
AC |
331 ms |
302144 KiB |