#include<bits/stdc++.h>
using namespace std;
#define fast ios::sync_with_stdio(false); cin.tie(NULL)
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define em emplace
#define all(v) (v).begin(), (v).end()
#define pre(a) cout<<fixed; cout.precision(a)
#define mp make_pair
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<int,pii> pipii;
const int inf = 1e9;
const ll INF = 1e18;
int n, m;
string s[1010];
pii S;
pii E;
int l[1010][1010];
int r[1010][1010];
int L[1010][1010];
int R[1010][1010];
bool chk[1010][1010];
priority_queue<pipii, vector<pipii>, greater<pipii>> pQ;
int main() {
fast;
fast;
cin >> n >> m;
for(int i=0; i<n; i++) {
cin >> s[i];
}
cin >> S.fi >> S.se;
cin >> E.fi >> E.se;
S.fi--;
S.se--;
E.fi--;
E.se--;
for(int i=0; i<n; i++) {
for(int j=1; j<m; j++) {
l[i][j] = j;
if(s[i][j] == '.' && s[i][j-1] == '.') l[i][j] = l[i][j-1];
}
for(int j=m-1; j>=0; j--) {
r[i][j] = j;
if(j < m-1 && s[i][j] == '.' && s[i][j+1] == '.') r[i][j] = r[i][j+1];
}
}
for(int i=0; i<m; i++) {
for(int j=1; j<n; j++) {
L[j][i] = j;
if(s[j][i] == '.' && s[j-1][i] == '.') L[j][i] = L[j-1][i];
}
for(int j=n-1; j>=0; j--) {
R[j][i] = j;
if(j < n-1 && s[j][i] == '.' && s[j+1][i] == '.') R[j][i] = R[j+1][i];
}
}
pQ.em(0, S);
while(pQ.size()) {
int cost = pQ.top().fi;
pii cur = pQ.top().se;
pQ.pop();
if(cur == E) {
cout << cost;
return 0;
}
if(chk[cur.fi][cur.se]) continue;
//cout << cur.fi << " " << cur.se << " : " << cost << endl;
chk[cur.fi][cur.se] = true;
pQ.em(cost + 1, mp(cur.fi, l[cur.fi][cur.se]));
pQ.em(cost + 1, mp(cur.fi, r[cur.fi][cur.se]));
pQ.em(cost + 1, mp(L[cur.fi][cur.se], cur.se));
pQ.em(cost + 1, mp(R[cur.fi][cur.se], cur.se));
if(cur.se < m-1 && s[cur.fi][cur.se+1] == '.') pQ.em(cost + 2, mp(cur.fi, cur.se+1));
if(cur.se > 0 && s[cur.fi][cur.se-1] == '.') pQ.em(cost + 2, mp(cur.fi, cur.se-1));
if(cur.fi < n-1 && s[cur.fi+1][cur.se] == '.') pQ.em(cost + 2, mp(cur.fi+1, cur.se));
if(cur.fi > 0 && s[cur.fi-1][cur.se] == '.') pQ.em(cost + 2, mp(cur.fi-1, cur.se));
}
cout << "-1";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
512 KB |
Output is correct |
2 |
Correct |
0 ms |
512 KB |
Output is correct |
3 |
Correct |
1 ms |
512 KB |
Output is correct |
4 |
Correct |
1 ms |
512 KB |
Output is correct |
5 |
Correct |
0 ms |
512 KB |
Output is correct |
6 |
Correct |
1 ms |
512 KB |
Output is correct |
7 |
Correct |
0 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 |
1 ms |
512 KB |
Output is correct |
2 |
Correct |
0 ms |
512 KB |
Output is correct |
3 |
Correct |
1 ms |
512 KB |
Output is correct |
4 |
Correct |
1 ms |
512 KB |
Output is correct |
5 |
Correct |
0 ms |
512 KB |
Output is correct |
6 |
Correct |
1 ms |
512 KB |
Output is correct |
7 |
Correct |
0 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 |
32 ms |
3968 KB |
Output is correct |
12 |
Correct |
41 ms |
4288 KB |
Output is correct |
13 |
Correct |
17 ms |
4732 KB |
Output is correct |
14 |
Correct |
24 ms |
4028 KB |
Output is correct |
15 |
Correct |
3 ms |
3584 KB |
Output is correct |
16 |
Correct |
61 ms |
4732 KB |
Output is correct |
17 |
Correct |
35 ms |
3840 KB |
Output is correct |
18 |
Correct |
24 ms |
3840 KB |
Output is correct |
19 |
Correct |
40 ms |
4096 KB |
Output is correct |
20 |
Correct |
15 ms |
3584 KB |
Output is correct |
21 |
Correct |
8 ms |
3712 KB |
Output is correct |
22 |
Correct |
5 ms |
3712 KB |
Output is correct |
23 |
Correct |
32 ms |
3960 KB |
Output is correct |
24 |
Correct |
49 ms |
3960 KB |
Output is correct |
25 |
Correct |
17 ms |
3840 KB |
Output is correct |
26 |
Correct |
25 ms |
3840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
512 KB |
Output is correct |
2 |
Correct |
0 ms |
512 KB |
Output is correct |
3 |
Correct |
1 ms |
512 KB |
Output is correct |
4 |
Correct |
1 ms |
512 KB |
Output is correct |
5 |
Correct |
0 ms |
512 KB |
Output is correct |
6 |
Correct |
1 ms |
512 KB |
Output is correct |
7 |
Correct |
0 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 |
32 ms |
3968 KB |
Output is correct |
12 |
Correct |
41 ms |
4288 KB |
Output is correct |
13 |
Correct |
17 ms |
4732 KB |
Output is correct |
14 |
Correct |
24 ms |
4028 KB |
Output is correct |
15 |
Correct |
3 ms |
3584 KB |
Output is correct |
16 |
Correct |
61 ms |
4732 KB |
Output is correct |
17 |
Correct |
35 ms |
3840 KB |
Output is correct |
18 |
Correct |
24 ms |
3840 KB |
Output is correct |
19 |
Correct |
40 ms |
4096 KB |
Output is correct |
20 |
Correct |
15 ms |
3584 KB |
Output is correct |
21 |
Correct |
8 ms |
3712 KB |
Output is correct |
22 |
Correct |
5 ms |
3712 KB |
Output is correct |
23 |
Correct |
32 ms |
3960 KB |
Output is correct |
24 |
Correct |
49 ms |
3960 KB |
Output is correct |
25 |
Correct |
17 ms |
3840 KB |
Output is correct |
26 |
Correct |
25 ms |
3840 KB |
Output is correct |
27 |
Correct |
986 ms |
19952 KB |
Output is correct |
28 |
Correct |
1906 ms |
21480 KB |
Output is correct |
29 |
Correct |
45 ms |
18548 KB |
Output is correct |
30 |
Correct |
597 ms |
21000 KB |
Output is correct |
31 |
Correct |
49 ms |
17280 KB |
Output is correct |
32 |
Correct |
1422 ms |
18624 KB |
Output is correct |
33 |
Correct |
155 ms |
9208 KB |
Output is correct |
34 |
Correct |
433 ms |
18232 KB |
Output is correct |
35 |
Correct |
1204 ms |
19144 KB |
Output is correct |
36 |
Correct |
976 ms |
18932 KB |
Output is correct |
37 |
Correct |
238 ms |
18168 KB |
Output is correct |
38 |
Correct |
1959 ms |
21484 KB |
Output is correct |
39 |
Correct |
190 ms |
18296 KB |
Output is correct |
40 |
Correct |
120 ms |
18168 KB |
Output is correct |