#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
typedef pair<int, pii> pipii;
#define mp make_pair
#define fi first
#define se second
const int INF=1e9;
int main(){
// cin.tie(0); ios_base::sync_with_stdio(0);
int r, c;
cin >> r >> c;
char m[r][c];
vector<int> xw[r], yw[c];
pii sl, cl;
for(auto && i: xw){
i.push_back(-1);
}
for(auto && i: yw){
i.push_back(-1);
}
for(int i=0; i<r; i++){
for(int j=0; j<c; j++){
cin >> m[i][j];
if(m[i][j] == '#'){
xw[i].push_back(j);
yw[j].push_back(i);
}
else if(m[i][j] == 'S'){
sl = mp(i, j);
}
else if(m[i][j] == 'C'){
cl = mp(i, j);
}
}
}
for(auto && i: xw){
i.push_back(c);
}
for(auto && i: yw){
i.push_back(r);
}
queue<pii> wq;
int dw[r][c];
memset(dw, -1, sizeof(dw));
vector<pipii> al[r][c];
const int dx[] = {-1, 1, 0, 0};
const int dy[] = {0, 0, -1, 1};
for(int i=0; i<r; i++){
for(int j=0; j<c; j++){
for(int k=0; k<4; k++){
int nx = i+dx[k], ny = j+dy[k];
if(nx == -1 || ny == -1 || nx == r || ny== c || m[nx][ny] == '#'){
if(dw[i][j] == 1) continue;
wq.emplace(i, j);
dw[i][j] = 1;
}
else{
al[i][j].emplace_back(1, mp(nx, ny));
}
}
}
}
while(wq.size()){
auto[cx, cy] = wq.front();
wq.pop();
for(int k=0; k<4; k++){
int nx = cx+dx[k], ny = cy+dy[k];
if(nx == -1 || ny == -1 || nx == r || ny == c) continue;
if(m[nx][ny] == '#') continue;
if(dw[nx][ny] != -1) continue;
dw[nx][ny] = dw[cx][cy] + 1;
wq.emplace(nx, ny);
}
}
/*for(int i=0; i<r; i++){
for(int j=0; j<c; j++){
cout << dw[i][j] << ' ';
}
cout << '\n';
}*/
for(int i=0; i<r; i++){
for(int j=0; j<c; j++){
if(m[i][j] == '#') continue;
vector<int>::iterator xw1, xw2, yw1, yw2;
xw2 = upper_bound(xw[i].begin(), xw[i].end(), j);
xw1 = xw2-1;
yw2 = upper_bound(yw[j].begin(), yw[j].end(), i);
yw1 = yw2-1;
// dw[i][j] = 69420;
if(*xw2 - j > 2) al[i][j].emplace_back(dw[i][j], mp(i, (*xw2)-1));
if(j - *xw1 > 2) al[i][j].emplace_back(dw[i][j], mp(i, (*xw1)+1));
if(*yw2 - i > 2) al[i][j].emplace_back(dw[i][j], mp((*yw2)-1, j));
if(i - *yw1 > 2) al[i][j].emplace_back(dw[i][j], mp((*yw1)+1, j));
}
}
/*for(int i=0; i<r; i++){
for(int j=0; j<c; j++){
cout << i << ' ' << j << ':' << ' ';
for(auto k: al[i][j]){
cout << k.fi << '{' << k.se.fi << ',' << k.se.se << '}' << ' ';
}
cout << '\n';
}
}*/
priority_queue<pipii, vector<pipii>, greater<pipii>> pq;
int dst[r][c];
bool visited[r][c];
for(int i=0; i<r; i++){
for(int j=0; j<c; j++){
dst[i][j] = INF;
}
}
memset(visited, 0, sizeof(visited));
dst[sl.fi][sl.se] = 0;
pq.emplace(0, sl);
while(pq.size()){
auto[cd, cxy] = pq.top();
pq.pop();
auto[cx, cy] = cxy;
if(visited[cx][cy]) continue;
for(auto [nw, nxy] : al[cx][cy]){
auto[nx, ny] = nxy;
if(dst[nx][ny] > cd+nw){
dst[nx][ny] = cd+nw;
pq.emplace(dst[nx][ny], nxy);
}
}
visited[cx][cy] = true;
}
cout << dst[cl.fi][cl.se];
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
600 KB |
Output is correct |
12 |
Correct |
1 ms |
600 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
13 ms |
4184 KB |
Output is correct |
6 |
Correct |
16 ms |
4444 KB |
Output is correct |
7 |
Correct |
15 ms |
4700 KB |
Output is correct |
8 |
Correct |
9 ms |
3676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
600 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
16 ms |
4188 KB |
Output is correct |
15 |
Correct |
16 ms |
4316 KB |
Output is correct |
16 |
Correct |
15 ms |
4700 KB |
Output is correct |
17 |
Correct |
15 ms |
4956 KB |
Output is correct |
18 |
Correct |
17 ms |
5676 KB |
Output is correct |
19 |
Correct |
16 ms |
6232 KB |
Output is correct |
20 |
Correct |
15 ms |
6236 KB |
Output is correct |
21 |
Correct |
13 ms |
4184 KB |
Output is correct |
22 |
Correct |
14 ms |
4184 KB |
Output is correct |
23 |
Correct |
14 ms |
4444 KB |
Output is correct |
24 |
Correct |
15 ms |
6224 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
9 ms |
3676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
600 KB |
Output is correct |
12 |
Correct |
1 ms |
600 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
13 ms |
4188 KB |
Output is correct |
15 |
Correct |
14 ms |
4444 KB |
Output is correct |
16 |
Correct |
15 ms |
4688 KB |
Output is correct |
17 |
Correct |
14 ms |
4956 KB |
Output is correct |
18 |
Correct |
17 ms |
5628 KB |
Output is correct |
19 |
Correct |
17 ms |
6236 KB |
Output is correct |
20 |
Correct |
17 ms |
6236 KB |
Output is correct |
21 |
Correct |
13 ms |
4188 KB |
Output is correct |
22 |
Correct |
13 ms |
4192 KB |
Output is correct |
23 |
Correct |
14 ms |
4444 KB |
Output is correct |
24 |
Correct |
508 ms |
120288 KB |
Output is correct |
25 |
Correct |
545 ms |
144560 KB |
Output is correct |
26 |
Correct |
538 ms |
144628 KB |
Output is correct |
27 |
Correct |
496 ms |
144808 KB |
Output is correct |
28 |
Correct |
451 ms |
96232 KB |
Output is correct |
29 |
Correct |
462 ms |
97764 KB |
Output is correct |
30 |
Correct |
494 ms |
99552 KB |
Output is correct |
31 |
Correct |
15 ms |
5980 KB |
Output is correct |
32 |
Correct |
525 ms |
144156 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
604 KB |
Output is correct |
35 |
Correct |
435 ms |
115748 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
9 ms |
3676 KB |
Output is correct |
38 |
Correct |
292 ms |
85888 KB |
Output is correct |
39 |
Correct |
326 ms |
86268 KB |
Output is correct |