#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 1000;
const ll INF = 1e18;
int N, M;
char S[MAXN+10][MAXN+10];
int L[MAXN+10][MAXN+10], R[MAXN+10][MAXN+10], U[MAXN+10][MAXN+10], D[MAXN+10][MAXN+10];
pii P, Q;
struct Queue
{
int y, x, d; ll w;
bool operator < (const Queue &p) const { return w>p.w; }
};
priority_queue<Queue> PQ;
ll dist[MAXN+10][MAXN+10][2];
void enqueue(Queue now)
{
if(dist[now.y][now.x][now.d]<=now.w) return;
dist[now.y][now.x][now.d]=now.w;
PQ.push(now);
}
int main()
{
scanf("%d%d", &N, &M);
for(int i=1; i<=N; i++) scanf("%s", S[i]+1);
scanf("%d%d%d%d", &P.first, &P.second, &Q.first, &Q.second);
for(int i=1; i<=N; i++) for(int j=1; j<=M; j++)
{
if(S[i][j]=='#') U[i][j]=i;
else U[i][j]=U[i-1][j];
if(S[i][j]=='#') L[i][j]=j;
else L[i][j]=L[i][j-1];
}
for(int i=N; i>=1; i--) for(int j=M; j>=1; j--)
{
if(S[i][j]=='#') D[i][j]=i;
else D[i][j]=D[i+1][j];
if(S[i][j]=='#') R[i][j]=j;
else R[i][j]=R[i][j+1];
}
for(int i=1; i<=N; i++) for(int j=1; j<=M; j++)
{
L[i][j]++; R[i][j]--;
U[i][j]++; D[i][j]--;
}
for(int i=1; i<=N; i++) for(int j=1; j<=M; j++) for(int k=0; k<2; k++) dist[i][j][k]=INF;
enqueue({P.first, P.second, 0, 0});
enqueue({P.first, P.second, 1, 0});
while(!PQ.empty())
{
Queue now=PQ.top(); PQ.pop();
if(dist[now.y][now.x][now.d]!=now.w) continue;
if(now.d==0)
{
for(int i=L[now.y][now.x]; i<now.x; i++) enqueue({now.y, i, 1, now.w+min(1+(i-L[now.y][now.x])*2, (now.x-i)*2)});
for(int i=now.x+1; i<=R[now.y][now.x]; i++) enqueue({now.y, i, 1, now.w+min(1+(R[now.y][now.x]-i)*2, (i-now.x)*2)});
}
else
{
for(int i=U[now.y][now.x]; i<now.y; i++) enqueue({i, now.x, 0, now.w+min(1+(i-U[now.y][now.x])*2, (now.y-i)*2)});
for(int i=now.y+1; i<=D[now.y][now.x]; i++) enqueue({i, now.x, 0, now.w+min(1+(D[now.y][now.x]-i)*2, (i-now.y)*2)});
}
}
ll ans=min(dist[Q.first][Q.second][0], dist[Q.first][Q.second][1]);
if(ans==INF) ans=-1;
printf("%lld\n", ans);
}
Compilation message
skating.cpp: In function 'int main()':
skating.cpp:34:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &N, &M);
~~~~~^~~~~~~~~~~~~~~~
skating.cpp:35:31: 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", S[i]+1);
~~~~~^~~~~~~~~~~~~~
skating.cpp:36:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d%d", &P.first, &P.second, &Q.first, &Q.second);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
512 KB |
Output is correct |
2 |
Correct |
0 ms |
512 KB |
Output is correct |
3 |
Correct |
0 ms |
512 KB |
Output is correct |
4 |
Correct |
0 ms |
512 KB |
Output is correct |
5 |
Correct |
1 ms |
512 KB |
Output is correct |
6 |
Correct |
1 ms |
512 KB |
Output is correct |
7 |
Correct |
1 ms |
512 KB |
Output is correct |
8 |
Correct |
1 ms |
512 KB |
Output is correct |
9 |
Correct |
0 ms |
512 KB |
Output is correct |
10 |
Correct |
0 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
512 KB |
Output is correct |
2 |
Correct |
0 ms |
512 KB |
Output is correct |
3 |
Correct |
0 ms |
512 KB |
Output is correct |
4 |
Correct |
0 ms |
512 KB |
Output is correct |
5 |
Correct |
1 ms |
512 KB |
Output is correct |
6 |
Correct |
1 ms |
512 KB |
Output is correct |
7 |
Correct |
1 ms |
512 KB |
Output is correct |
8 |
Correct |
1 ms |
512 KB |
Output is correct |
9 |
Correct |
0 ms |
512 KB |
Output is correct |
10 |
Correct |
0 ms |
512 KB |
Output is correct |
11 |
Correct |
83 ms |
8436 KB |
Output is correct |
12 |
Correct |
90 ms |
11504 KB |
Output is correct |
13 |
Correct |
79 ms |
8436 KB |
Output is correct |
14 |
Correct |
19 ms |
5504 KB |
Output is correct |
15 |
Correct |
4 ms |
5120 KB |
Output is correct |
16 |
Correct |
47 ms |
8436 KB |
Output is correct |
17 |
Correct |
15 ms |
5248 KB |
Output is correct |
18 |
Correct |
10 ms |
5248 KB |
Output is correct |
19 |
Correct |
20 ms |
5696 KB |
Output is correct |
20 |
Correct |
72 ms |
5884 KB |
Output is correct |
21 |
Correct |
4 ms |
5248 KB |
Output is correct |
22 |
Correct |
4 ms |
5120 KB |
Output is correct |
23 |
Correct |
13 ms |
5248 KB |
Output is correct |
24 |
Correct |
34 ms |
6140 KB |
Output is correct |
25 |
Correct |
9 ms |
5248 KB |
Output is correct |
26 |
Correct |
9 ms |
5248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
512 KB |
Output is correct |
2 |
Correct |
0 ms |
512 KB |
Output is correct |
3 |
Correct |
0 ms |
512 KB |
Output is correct |
4 |
Correct |
0 ms |
512 KB |
Output is correct |
5 |
Correct |
1 ms |
512 KB |
Output is correct |
6 |
Correct |
1 ms |
512 KB |
Output is correct |
7 |
Correct |
1 ms |
512 KB |
Output is correct |
8 |
Correct |
1 ms |
512 KB |
Output is correct |
9 |
Correct |
0 ms |
512 KB |
Output is correct |
10 |
Correct |
0 ms |
512 KB |
Output is correct |
11 |
Correct |
83 ms |
8436 KB |
Output is correct |
12 |
Correct |
90 ms |
11504 KB |
Output is correct |
13 |
Correct |
79 ms |
8436 KB |
Output is correct |
14 |
Correct |
19 ms |
5504 KB |
Output is correct |
15 |
Correct |
4 ms |
5120 KB |
Output is correct |
16 |
Correct |
47 ms |
8436 KB |
Output is correct |
17 |
Correct |
15 ms |
5248 KB |
Output is correct |
18 |
Correct |
10 ms |
5248 KB |
Output is correct |
19 |
Correct |
20 ms |
5696 KB |
Output is correct |
20 |
Correct |
72 ms |
5884 KB |
Output is correct |
21 |
Correct |
4 ms |
5248 KB |
Output is correct |
22 |
Correct |
4 ms |
5120 KB |
Output is correct |
23 |
Correct |
13 ms |
5248 KB |
Output is correct |
24 |
Correct |
34 ms |
6140 KB |
Output is correct |
25 |
Correct |
9 ms |
5248 KB |
Output is correct |
26 |
Correct |
9 ms |
5248 KB |
Output is correct |
27 |
Execution timed out |
3059 ms |
132816 KB |
Time limit exceeded |
28 |
Halted |
0 ms |
0 KB |
- |