#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];
bool ch[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;
ch[x][y]=true;
dist[x][y]=d;
for(auto i:link[x][y]){
if(ch[i.F.F][i.F.S]||dist[i.F.F][i.F.S]<=d+i.S)continue;
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:21: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:22: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:66: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 |
15 ms |
24320 KB |
Output is correct |
2 |
Correct |
18 ms |
24320 KB |
Output is correct |
3 |
Correct |
17 ms |
24448 KB |
Output is correct |
4 |
Correct |
15 ms |
24320 KB |
Output is correct |
5 |
Correct |
15 ms |
24320 KB |
Output is correct |
6 |
Correct |
15 ms |
24320 KB |
Output is correct |
7 |
Correct |
16 ms |
24320 KB |
Output is correct |
8 |
Correct |
15 ms |
24320 KB |
Output is correct |
9 |
Correct |
18 ms |
24320 KB |
Output is correct |
10 |
Correct |
16 ms |
24320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
24320 KB |
Output is correct |
2 |
Correct |
18 ms |
24320 KB |
Output is correct |
3 |
Correct |
17 ms |
24448 KB |
Output is correct |
4 |
Correct |
15 ms |
24320 KB |
Output is correct |
5 |
Correct |
15 ms |
24320 KB |
Output is correct |
6 |
Correct |
15 ms |
24320 KB |
Output is correct |
7 |
Correct |
16 ms |
24320 KB |
Output is correct |
8 |
Correct |
15 ms |
24320 KB |
Output is correct |
9 |
Correct |
18 ms |
24320 KB |
Output is correct |
10 |
Correct |
16 ms |
24320 KB |
Output is correct |
11 |
Correct |
53 ms |
31852 KB |
Output is correct |
12 |
Correct |
57 ms |
31864 KB |
Output is correct |
13 |
Correct |
50 ms |
31864 KB |
Output is correct |
14 |
Correct |
46 ms |
30328 KB |
Output is correct |
15 |
Correct |
26 ms |
28536 KB |
Output is correct |
16 |
Correct |
58 ms |
31736 KB |
Output is correct |
17 |
Correct |
41 ms |
30456 KB |
Output is correct |
18 |
Correct |
37 ms |
30072 KB |
Output is correct |
19 |
Correct |
42 ms |
30712 KB |
Output is correct |
20 |
Correct |
43 ms |
31232 KB |
Output is correct |
21 |
Correct |
29 ms |
29696 KB |
Output is correct |
22 |
Correct |
21 ms |
27768 KB |
Output is correct |
23 |
Correct |
40 ms |
29944 KB |
Output is correct |
24 |
Correct |
50 ms |
31356 KB |
Output is correct |
25 |
Correct |
36 ms |
29688 KB |
Output is correct |
26 |
Correct |
36 ms |
29816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
24320 KB |
Output is correct |
2 |
Correct |
18 ms |
24320 KB |
Output is correct |
3 |
Correct |
17 ms |
24448 KB |
Output is correct |
4 |
Correct |
15 ms |
24320 KB |
Output is correct |
5 |
Correct |
15 ms |
24320 KB |
Output is correct |
6 |
Correct |
15 ms |
24320 KB |
Output is correct |
7 |
Correct |
16 ms |
24320 KB |
Output is correct |
8 |
Correct |
15 ms |
24320 KB |
Output is correct |
9 |
Correct |
18 ms |
24320 KB |
Output is correct |
10 |
Correct |
16 ms |
24320 KB |
Output is correct |
11 |
Correct |
53 ms |
31852 KB |
Output is correct |
12 |
Correct |
57 ms |
31864 KB |
Output is correct |
13 |
Correct |
50 ms |
31864 KB |
Output is correct |
14 |
Correct |
46 ms |
30328 KB |
Output is correct |
15 |
Correct |
26 ms |
28536 KB |
Output is correct |
16 |
Correct |
58 ms |
31736 KB |
Output is correct |
17 |
Correct |
41 ms |
30456 KB |
Output is correct |
18 |
Correct |
37 ms |
30072 KB |
Output is correct |
19 |
Correct |
42 ms |
30712 KB |
Output is correct |
20 |
Correct |
43 ms |
31232 KB |
Output is correct |
21 |
Correct |
29 ms |
29696 KB |
Output is correct |
22 |
Correct |
21 ms |
27768 KB |
Output is correct |
23 |
Correct |
40 ms |
29944 KB |
Output is correct |
24 |
Correct |
50 ms |
31356 KB |
Output is correct |
25 |
Correct |
36 ms |
29688 KB |
Output is correct |
26 |
Correct |
36 ms |
29816 KB |
Output is correct |
27 |
Correct |
1440 ms |
187864 KB |
Output is correct |
28 |
Correct |
1691 ms |
189368 KB |
Output is correct |
29 |
Correct |
1534 ms |
189668 KB |
Output is correct |
30 |
Correct |
1311 ms |
174552 KB |
Output is correct |
31 |
Correct |
423 ms |
108504 KB |
Output is correct |
32 |
Correct |
1291 ms |
187280 KB |
Output is correct |
33 |
Correct |
234 ms |
60920 KB |
Output is correct |
34 |
Correct |
922 ms |
165676 KB |
Output is correct |
35 |
Correct |
1425 ms |
183928 KB |
Output is correct |
36 |
Correct |
1172 ms |
167408 KB |
Output is correct |
37 |
Correct |
697 ms |
140920 KB |
Output is correct |
38 |
Correct |
1695 ms |
187968 KB |
Output is correct |
39 |
Correct |
701 ms |
134396 KB |
Output is correct |
40 |
Correct |
244 ms |
85752 KB |
Output is correct |