#include <bits/stdc++.h>
#define eb emplace_back
#define mp make_pair
#define F first
#define S second
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const LL llinf=2000000000000000000;
int n, m;
char str[1010][1010];
LL dist[1010][1010];
priority_queue<pair<LL, pii>, vector<pair<LL, pii> >, greater<pair<LL, pii> > > pq;
vector<pair<pii, LL> > link[1010][1010];
int mx[]={1, 0, -1, 0}, my[]={0, 1, 0, -1};
int main(){
scanf("%d %d", &n, &m);
for(int i=1; i<=n; i++)scanf("%s", str[i]+1);
vector<pii> vc;
for(int i=1; i<=n; i++){
for(int j=1; j<=m; j++){
if(str[i][j]=='#'){
for(auto k:vc)link[k.F][k.S].eb(mp(i, j-1), 1ll);
vc.clear();
}
else vc.eb(i, j);
}
for(int j=m; j>=1; j--){
if(str[i][j]=='#'){
for(auto k:vc)link[k.F][k.S].eb(mp(i, j+1), 1ll);
vc.clear();
}
else vc.eb(i, j);
}
}
for(int j=1; j<=m; j++){
for(int i=1; i<=n; i++){
if(str[i][j]=='#'){
for(auto k:vc)link[k.F][k.S].eb(mp(i-1, j), 1ll);
vc.clear();
}
else vc.eb(i, j);
}
for(int i=n; i>=1; i--){
if(str[i][j]=='#'){
for(auto k:vc)link[k.F][k.S].eb(mp(i+1, j), 1ll);
vc.clear();
}
else vc.eb(i, j);
}
}
for(int i=1; i<=n; i++){
for(int j=1; j<=m; j++){
dist[i][j]=llinf;
if(str[i][j]=='#')continue;
for(int k=0; k<4; k++){
if(str[i+mx[k]][j+my[k]]=='.')link[i][j].eb(mp(i+mx[k], j+my[k]), 2ll);
}
}
}
int sx, sy, ex, ey;
scanf("%d %d %d %d", &sx, &sy, &ex, &ey);
pq.push(mp(0, mp(sx, sy)));
while(!pq.empty()){
LL d=pq.top().F;
int x=pq.top().S.F, y=pq.top().S.S;
pq.pop();
if(dist[x][y]<=d)continue;
dist[x][y]=d;
for(auto i:link[x][y])pq.push(mp(d+i.S, i.F));
}
if(dist[ex][ey]==llinf)puts("-1");
else printf("%lld", dist[ex][ey]);
}
/*
5 5
#####
#...#
#...#
#...#
#####
2 2
3 3
*/
Compilation message
skating.cpp: In function 'int main()':
skating.cpp:20:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &n, &m);
~~~~~^~~~~~~~~~~~~~~~~
skating.cpp:21:33: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(int i=1; i<=n; i++)scanf("%s", str[i]+1);
~~~~~^~~~~~~~~~~~~~~~
skating.cpp:65:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d %d", &sx, &sy, &ex, &ey);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
24320 KB |
Output is correct |
2 |
Correct |
14 ms |
24320 KB |
Output is correct |
3 |
Correct |
14 ms |
24320 KB |
Output is correct |
4 |
Correct |
14 ms |
24320 KB |
Output is correct |
5 |
Correct |
14 ms |
24320 KB |
Output is correct |
6 |
Correct |
14 ms |
24320 KB |
Output is correct |
7 |
Correct |
14 ms |
24320 KB |
Output is correct |
8 |
Correct |
17 ms |
24320 KB |
Output is correct |
9 |
Correct |
14 ms |
24320 KB |
Output is correct |
10 |
Correct |
16 ms |
24320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
24320 KB |
Output is correct |
2 |
Correct |
14 ms |
24320 KB |
Output is correct |
3 |
Correct |
14 ms |
24320 KB |
Output is correct |
4 |
Correct |
14 ms |
24320 KB |
Output is correct |
5 |
Correct |
14 ms |
24320 KB |
Output is correct |
6 |
Correct |
14 ms |
24320 KB |
Output is correct |
7 |
Correct |
14 ms |
24320 KB |
Output is correct |
8 |
Correct |
17 ms |
24320 KB |
Output is correct |
9 |
Correct |
14 ms |
24320 KB |
Output is correct |
10 |
Correct |
16 ms |
24320 KB |
Output is correct |
11 |
Correct |
84 ms |
31736 KB |
Output is correct |
12 |
Correct |
95 ms |
32088 KB |
Output is correct |
13 |
Correct |
103 ms |
32500 KB |
Output is correct |
14 |
Correct |
68 ms |
30328 KB |
Output is correct |
15 |
Correct |
27 ms |
28544 KB |
Output is correct |
16 |
Correct |
90 ms |
32372 KB |
Output is correct |
17 |
Correct |
66 ms |
30328 KB |
Output is correct |
18 |
Correct |
48 ms |
29824 KB |
Output is correct |
19 |
Correct |
73 ms |
30712 KB |
Output is correct |
20 |
Correct |
76 ms |
31352 KB |
Output is correct |
21 |
Correct |
36 ms |
29560 KB |
Output is correct |
22 |
Correct |
23 ms |
27520 KB |
Output is correct |
23 |
Correct |
57 ms |
29824 KB |
Output is correct |
24 |
Correct |
84 ms |
31352 KB |
Output is correct |
25 |
Correct |
52 ms |
29568 KB |
Output is correct |
26 |
Correct |
49 ms |
29560 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
24320 KB |
Output is correct |
2 |
Correct |
14 ms |
24320 KB |
Output is correct |
3 |
Correct |
14 ms |
24320 KB |
Output is correct |
4 |
Correct |
14 ms |
24320 KB |
Output is correct |
5 |
Correct |
14 ms |
24320 KB |
Output is correct |
6 |
Correct |
14 ms |
24320 KB |
Output is correct |
7 |
Correct |
14 ms |
24320 KB |
Output is correct |
8 |
Correct |
17 ms |
24320 KB |
Output is correct |
9 |
Correct |
14 ms |
24320 KB |
Output is correct |
10 |
Correct |
16 ms |
24320 KB |
Output is correct |
11 |
Correct |
84 ms |
31736 KB |
Output is correct |
12 |
Correct |
95 ms |
32088 KB |
Output is correct |
13 |
Correct |
103 ms |
32500 KB |
Output is correct |
14 |
Correct |
68 ms |
30328 KB |
Output is correct |
15 |
Correct |
27 ms |
28544 KB |
Output is correct |
16 |
Correct |
90 ms |
32372 KB |
Output is correct |
17 |
Correct |
66 ms |
30328 KB |
Output is correct |
18 |
Correct |
48 ms |
29824 KB |
Output is correct |
19 |
Correct |
73 ms |
30712 KB |
Output is correct |
20 |
Correct |
76 ms |
31352 KB |
Output is correct |
21 |
Correct |
36 ms |
29560 KB |
Output is correct |
22 |
Correct |
23 ms |
27520 KB |
Output is correct |
23 |
Correct |
57 ms |
29824 KB |
Output is correct |
24 |
Correct |
84 ms |
31352 KB |
Output is correct |
25 |
Correct |
52 ms |
29568 KB |
Output is correct |
26 |
Correct |
49 ms |
29560 KB |
Output is correct |
27 |
Correct |
2667 ms |
188264 KB |
Output is correct |
28 |
Correct |
2975 ms |
190492 KB |
Output is correct |
29 |
Execution timed out |
3109 ms |
201040 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |