#include<iostream>
#include<vector>
#include<queue>
#include<algorithm>
#define ep emplace
#define eb emplace_back
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
using namespace std;
typedef long long ll;
typedef pair<int,int>pi;
typedef pair<ll,ll>pl;
const int inf=1e9+7;
const ll INF=1e18+7;
char map[1010][1010];
int l[1010][1010],r[1010][1010],u[1010][1010],d[1010][1010];
int dis[1000010];
vector<pi>adj[1000010];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n,m;
cin>>n>>m;
for(int i=0;i++<n;)
for(int j=0;j++<m;)
cin>>map[i][j];
for(int i=0;i++<n;)
for(int j=0;j++<m;)
l[i][j]=map[i][j-1]=='.'?l[i][j-1]:j,u[i][j]=map[i-1][j]=='.'?u[i-1][j]:i;
for(int i=n;i>0;i--)
for(int j=m;j>0;j--)
r[i][j]=map[i][j+1]=='.'?l[i][j+1]:j,d[i][j]=map[i+1][j]=='.'?d[i+1][j]:i;
for(int i=0;i++<n;)
{
for(int j=0;j++<m;)
{
if(map[i][j]!='.')
continue;
int cur=(i-1)*m+(j-1);
for(int k=-2;k<2;k++)
{
int nx=i+k%2;
int ny=j+(k+1)%2;
int nxt=(nx-1)*m+(ny-1);
if(map[nx][ny]=='.')
adj[cur].eb(nxt,2);
}
if(l[i][j]!=j)
adj[cur].eb((i-1)*m+(l[i][j]-1),1);
if(r[i][j]!=j)
adj[cur].eb((i-1)*m+(r[i][j]-1),1);
if(u[i][j]!=i)
adj[cur].eb((u[i][j]-1)*m+(j-1),1);
if(d[i][j]!=i)
adj[cur].eb((d[i][j]-1)*m+(j-1),1);
}
}
fill(dis,dis+n*m,inf);
priority_queue<pi,vector<pi>,greater<pi> >pq;
int sx,sy,ex,ey;
cin>>sx>>sy;
cin>>ex>>ey;
pq.ep(dis[(sx-1)*m+(sy-1)]=0,(sx-1)*m+(sy-1));
while(!pq.empty())
{
int i=pq.top().se;
int d=pq.top().fi;
pq.pop();
for(pi&t:adj[i])
if(dis[t.fi]>d+t.se)
pq.ep(dis[t.fi]=d+t.se,t.fi);
}
int r=dis[(ex-1)*m+(ey-1)];
if(r==inf)
r=-1;
cout<<r<<endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
22 ms |
24064 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
22 ms |
24064 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
22 ms |
24064 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |