#include<bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define mp make_pair
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef long double ld;
template <typename T> bool chkmin(T &x,T y){return x>y?x=y,1:0;}
template <typename T> bool chkmax(T &x,T y){return x<y?x=y,1:0;}
ll readint(){
ll x=0,f=1; char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
const int dx[]={1,0,-1,0};
const int dy[]={0,1,0,-1};
int n,m,sx,sy,fx,fy;
int dist[1005][1005];
char s[1005][1005];
pii l[1005][1005],r[1005][1005],u[1005][1005],d[1005][1005];
int main(){
n=readint(); m=readint();
for(int i=0;i<n;i++) scanf("%s",s+i);
sx=readint(); sy=readint();
fx=readint(); fy=readint();
--sx; --sy; --fx; --fy;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
dist[i][j]=(int)1e9;
}
}
for(int j=0;j<m;j++){
for(int i=0;i<n;i++){
if(j==0||s[i][j]=='#') l[i][j]=mp(i,j+1);
else l[i][j]=l[i][j-1];
}
}
for(int j=m-1;j>=0;j--){
for(int i=0;i<n;i++){
if(j==m-1||s[i][j]=='#') r[i][j]=mp(i,j-1);
else r[i][j]=r[i][j+1];
}
}
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(i==0||s[i][j]=='#') u[i][j]=mp(i+1,j);
else u[i][j]=u[i-1][j];
}
}
for(int i=n-1;i>=0;i--){
for(int j=0;j<m;j++){
if(i==n-1||s[i][j]=='#') d[i][j]=mp(i-1,j);
else d[i][j]=d[i+1][j];
}
}
dist[sx][sy]=0;
set<array<int,3>> st;
st.insert({0,sx,sy});
while(!st.empty()){
auto it=st.begin();
int x=(*it)[1],y=(*it)[2];
st.erase(it);
for(auto&p:{l[x][y],r[x][y],u[x][y],d[x][y]}){
if(p==mp(x,y)) continue;
int nx=p.fi,ny=p.se;
if(dist[nx][ny]>dist[x][y]+1){
dist[nx][ny]=dist[x][y]+1;
st.insert({dist[nx][ny],nx,ny});
}
}
for(int dir=0;dir<4;dir++){
int nx=x+dx[dir],ny=y+dy[dir];
if(s[nx][ny]=='#') continue;
if(dist[nx][ny]>dist[x][y]+2){
dist[nx][ny]=dist[x][y]+2;
st.insert({dist[nx][ny],nx,ny});
}
}
}
if(dist[fx][fy]==(int)1e9) dist[fx][fy]=-1;
printf("%d\n",dist[fx][fy]);
return 0;
}
Compilation message
skating.cpp: In function 'int main()':
skating.cpp:35:31: warning: format '%s' expects argument of type 'char*', but argument 2 has type 'char (*)[1005]' [-Wformat=]
35 | for(int i=0;i<n;i++) scanf("%s",s+i);
| ~^ ~~~
| | |
| | char (*)[1005]
| char*
skating.cpp:35:28: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
35 | for(int i=0;i<n;i++) scanf("%s",s+i);
| ~~~~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8540 KB |
Output is correct |
2 |
Correct |
1 ms |
8636 KB |
Output is correct |
3 |
Correct |
1 ms |
8540 KB |
Output is correct |
4 |
Correct |
1 ms |
8540 KB |
Output is correct |
5 |
Correct |
1 ms |
8540 KB |
Output is correct |
6 |
Correct |
1 ms |
8536 KB |
Output is correct |
7 |
Correct |
2 ms |
8540 KB |
Output is correct |
8 |
Correct |
1 ms |
8540 KB |
Output is correct |
9 |
Correct |
1 ms |
8540 KB |
Output is correct |
10 |
Correct |
1 ms |
8540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8540 KB |
Output is correct |
2 |
Correct |
1 ms |
8636 KB |
Output is correct |
3 |
Correct |
1 ms |
8540 KB |
Output is correct |
4 |
Correct |
1 ms |
8540 KB |
Output is correct |
5 |
Correct |
1 ms |
8540 KB |
Output is correct |
6 |
Correct |
1 ms |
8536 KB |
Output is correct |
7 |
Correct |
2 ms |
8540 KB |
Output is correct |
8 |
Correct |
1 ms |
8540 KB |
Output is correct |
9 |
Correct |
1 ms |
8540 KB |
Output is correct |
10 |
Correct |
1 ms |
8540 KB |
Output is correct |
11 |
Correct |
12 ms |
19036 KB |
Output is correct |
12 |
Correct |
10 ms |
19292 KB |
Output is correct |
13 |
Correct |
12 ms |
19536 KB |
Output is correct |
14 |
Correct |
10 ms |
19036 KB |
Output is correct |
15 |
Correct |
3 ms |
19036 KB |
Output is correct |
16 |
Correct |
11 ms |
19292 KB |
Output is correct |
17 |
Correct |
8 ms |
19036 KB |
Output is correct |
18 |
Correct |
7 ms |
18924 KB |
Output is correct |
19 |
Correct |
9 ms |
19124 KB |
Output is correct |
20 |
Correct |
8 ms |
19036 KB |
Output is correct |
21 |
Correct |
5 ms |
19036 KB |
Output is correct |
22 |
Correct |
3 ms |
19036 KB |
Output is correct |
23 |
Correct |
9 ms |
19036 KB |
Output is correct |
24 |
Correct |
9 ms |
19036 KB |
Output is correct |
25 |
Correct |
8 ms |
19036 KB |
Output is correct |
26 |
Correct |
7 ms |
19036 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8540 KB |
Output is correct |
2 |
Correct |
1 ms |
8636 KB |
Output is correct |
3 |
Correct |
1 ms |
8540 KB |
Output is correct |
4 |
Correct |
1 ms |
8540 KB |
Output is correct |
5 |
Correct |
1 ms |
8540 KB |
Output is correct |
6 |
Correct |
1 ms |
8536 KB |
Output is correct |
7 |
Correct |
2 ms |
8540 KB |
Output is correct |
8 |
Correct |
1 ms |
8540 KB |
Output is correct |
9 |
Correct |
1 ms |
8540 KB |
Output is correct |
10 |
Correct |
1 ms |
8540 KB |
Output is correct |
11 |
Correct |
12 ms |
19036 KB |
Output is correct |
12 |
Correct |
10 ms |
19292 KB |
Output is correct |
13 |
Correct |
12 ms |
19536 KB |
Output is correct |
14 |
Correct |
10 ms |
19036 KB |
Output is correct |
15 |
Correct |
3 ms |
19036 KB |
Output is correct |
16 |
Correct |
11 ms |
19292 KB |
Output is correct |
17 |
Correct |
8 ms |
19036 KB |
Output is correct |
18 |
Correct |
7 ms |
18924 KB |
Output is correct |
19 |
Correct |
9 ms |
19124 KB |
Output is correct |
20 |
Correct |
8 ms |
19036 KB |
Output is correct |
21 |
Correct |
5 ms |
19036 KB |
Output is correct |
22 |
Correct |
3 ms |
19036 KB |
Output is correct |
23 |
Correct |
9 ms |
19036 KB |
Output is correct |
24 |
Correct |
9 ms |
19036 KB |
Output is correct |
25 |
Correct |
8 ms |
19036 KB |
Output is correct |
26 |
Correct |
7 ms |
19036 KB |
Output is correct |
27 |
Correct |
230 ms |
38756 KB |
Output is correct |
28 |
Correct |
328 ms |
40600 KB |
Output is correct |
29 |
Correct |
341 ms |
45344 KB |
Output is correct |
30 |
Correct |
240 ms |
39764 KB |
Output is correct |
31 |
Correct |
27 ms |
37716 KB |
Output is correct |
32 |
Correct |
170 ms |
38004 KB |
Output is correct |
33 |
Correct |
44 ms |
27484 KB |
Output is correct |
34 |
Correct |
74 ms |
37972 KB |
Output is correct |
35 |
Correct |
176 ms |
38740 KB |
Output is correct |
36 |
Correct |
156 ms |
38236 KB |
Output is correct |
37 |
Correct |
63 ms |
37980 KB |
Output is correct |
38 |
Correct |
323 ms |
40500 KB |
Output is correct |
39 |
Correct |
48 ms |
37716 KB |
Output is correct |
40 |
Correct |
30 ms |
37716 KB |
Output is correct |