This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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];
ll dist[MAXN+10][MAXN+10];
pii P, Q;
struct Queue
{
int y, x; ll w;
bool operator < (const Queue &p) const { return w>p.w; }
};
priority_queue<Queue> PQ;
void enqueue(Queue now)
{
if(dist[now.y][now.x]<=now.w) return;
dist[now.y][now.x]=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++) dist[i][j]=INF;
enqueue({P.first, P.second, 0});
enqueue({P.first, P.second, 0});
while(!PQ.empty())
{
Queue now=PQ.top(); PQ.pop();
if(dist[now.y][now.x]!=now.w) continue;
enqueue({now.y, L[now.y][now.x], now.w+1});
enqueue({now.y, R[now.y][now.x], now.w+1});
if(U[now.y][now.x]<=now.y-1) enqueue({now.y-1, now.x, now.w+2});
if(D[now.y][now.x]>=now.y+1) enqueue({now.y+1, now.x, now.w+2});
enqueue({U[now.y][now.x], now.x, now.w+1});
enqueue({D[now.y][now.x], now.x, now.w+1});
if(L[now.y][now.x]<=now.x-1) enqueue({now.y, now.x-1, now.w+2});
if(R[now.y][now.x]>=now.x+1) enqueue({now.y, now.x+1, now.w+2});
}
ll ans=dist[Q.first][Q.second];
if(ans==INF) ans=-1;
printf("%lld\n", ans);
}
Compilation message (stderr)
skating.cpp: In function 'int main()':
skating.cpp:33: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:34: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:35: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);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |