#include<bits/stdc++.h>
using namespace std;
#define int long long
#define mp make_pair
#define fi first
#define se second
const int maxn=1005;
int a[maxn][maxn],ans[maxn][maxn],up[maxn][maxn],dw[maxn][maxn],rt[maxn][maxn],lf[maxn][maxn];
int n,m,sx,sy,ex,ey;
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
struct e{
int x,y,dist;
};
class comp {
public:
bool operator()(e f,e s)
{
return f.dist>s.dist;
}
};
bool valid(int x,int y){
if(x>=0 && x<=n-1 && y>=0 && y<=m-1 && a[x][y]==1 && ans[x][y]==1e9) return true;
else return false;
}
void init(){
for (int i=0; i<n; i++)
for (int j=0; j<m; j++)
ans[i][j]=1e9;
for (int i=0; i<n; i++) {
int pre = 0;
for (int j=0; j<m; j++) {
if(a[i][j]==0) pre = j+1;
lf[i][j] = pre;
}
}
for (int i=0; i<n; i++) {
int pre = m-1;
for (int j=m-1; j>=0; j--) {
if(a[i][j] ==0) pre = j-1;
rt[i][j] = pre;
}
}
for (int j=0; j<m; j++) {
int pre = 0;
for (int i=0; i<n; i++) {
if(a[i][j] == 0) pre = i+1;
up[i][j] = pre;
}
}
for (int j=0; j<m; j++) {
int pre = n-1;
for (int i=n-1; i>=0; i--) {
if(a[i][j] == 0) pre = i-1;
dw[i][j] = pre;
}
}
}
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0);
cin>>n>>m;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
char x; cin>>x;
if(x=='#') a[i][j]=0;
else a[i][j]=1;
if(x=='S') {sx=i;sy=j;}
if(x=='C') {ex=i;ey=j;}
}
}
init();
priority_queue<e,vector<e>,comp> pq;
pq.push({sx,sy,0});
while(pq.size()){
e u=pq.top(); pq.pop();
if(ans[u.x][u.y]<=u.dist) continue;
if(u.x==ex && u.y==ey){
cout<<u.dist<<'\n';
return 0;
}
ans[u.x][u.y]=u.dist;
for(int i=0;i<4;i++){
if(valid(u.x+dx[i],u.y+dy[i]))
pq.push({u.x+dx[i],u.y+dy[i],u.dist+1});
}
//找到最靠近的传送门
int mn=min({abs(u.x-up[u.x][u.y]),
abs(u.x-dw[u.x][u.y]),
abs(u.y-rt[u.x][u.y]),
abs(u.y-lf[u.x][u.y])
});
//往上下左右发射,走mn个步就可以到那里然后tp就要+1
if(valid(u.x,rt[u.x][u.y])) pq.push({u.x,rt[u.x][u.y],u.dist+mn+1});
if(valid(u.x,lf[u.x][u.y])) pq.push({u.x,lf[u.x][u.y],u.dist+mn+1});
if(valid(up[u.x][u.y],u.y)) pq.push({up[u.x][u.y],u.y,u.dist+mn+1});
if(valid(dw[u.x][u.y],u.y)) pq.push({dw[u.x][u.y],u.y,u.dist+mn+1});
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
596 KB |
Output is correct |
9 |
Correct |
0 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
596 KB |
Output is correct |
9 |
Correct |
2 ms |
1620 KB |
Output is correct |
10 |
Correct |
2 ms |
1876 KB |
Output is correct |
11 |
Correct |
1 ms |
1620 KB |
Output is correct |
12 |
Correct |
1 ms |
1620 KB |
Output is correct |
13 |
Correct |
1 ms |
1620 KB |
Output is correct |
14 |
Correct |
0 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
1620 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
7 ms |
6996 KB |
Output is correct |
6 |
Correct |
8 ms |
7056 KB |
Output is correct |
7 |
Correct |
8 ms |
6996 KB |
Output is correct |
8 |
Correct |
7 ms |
6996 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
596 KB |
Output is correct |
9 |
Correct |
1 ms |
1620 KB |
Output is correct |
10 |
Correct |
2 ms |
1876 KB |
Output is correct |
11 |
Correct |
1 ms |
1620 KB |
Output is correct |
12 |
Correct |
1 ms |
1632 KB |
Output is correct |
13 |
Correct |
1 ms |
1620 KB |
Output is correct |
14 |
Correct |
8 ms |
7040 KB |
Output is correct |
15 |
Correct |
9 ms |
6996 KB |
Output is correct |
16 |
Correct |
8 ms |
6996 KB |
Output is correct |
17 |
Correct |
9 ms |
6996 KB |
Output is correct |
18 |
Correct |
14 ms |
7296 KB |
Output is correct |
19 |
Correct |
5 ms |
7252 KB |
Output is correct |
20 |
Correct |
15 ms |
10144 KB |
Output is correct |
21 |
Correct |
8 ms |
7048 KB |
Output is correct |
22 |
Correct |
8 ms |
6996 KB |
Output is correct |
23 |
Correct |
9 ms |
7064 KB |
Output is correct |
24 |
Correct |
12 ms |
6996 KB |
Output is correct |
25 |
Correct |
0 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
1620 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
7 ms |
7040 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
596 KB |
Output is correct |
9 |
Correct |
2 ms |
1620 KB |
Output is correct |
10 |
Correct |
2 ms |
1876 KB |
Output is correct |
11 |
Correct |
2 ms |
1620 KB |
Output is correct |
12 |
Correct |
1 ms |
1620 KB |
Output is correct |
13 |
Correct |
1 ms |
1620 KB |
Output is correct |
14 |
Correct |
7 ms |
6996 KB |
Output is correct |
15 |
Correct |
8 ms |
7064 KB |
Output is correct |
16 |
Correct |
9 ms |
7084 KB |
Output is correct |
17 |
Correct |
11 ms |
7088 KB |
Output is correct |
18 |
Correct |
10 ms |
7124 KB |
Output is correct |
19 |
Correct |
5 ms |
7252 KB |
Output is correct |
20 |
Correct |
21 ms |
10176 KB |
Output is correct |
21 |
Correct |
8 ms |
6996 KB |
Output is correct |
22 |
Correct |
8 ms |
6996 KB |
Output is correct |
23 |
Correct |
9 ms |
6996 KB |
Output is correct |
24 |
Correct |
267 ms |
48052 KB |
Output is correct |
25 |
Correct |
491 ms |
49132 KB |
Output is correct |
26 |
Correct |
105 ms |
60872 KB |
Output is correct |
27 |
Correct |
372 ms |
97884 KB |
Output is correct |
28 |
Correct |
165 ms |
48516 KB |
Output is correct |
29 |
Correct |
189 ms |
48532 KB |
Output is correct |
30 |
Correct |
186 ms |
48588 KB |
Output is correct |
31 |
Correct |
12 ms |
7124 KB |
Output is correct |
32 |
Correct |
320 ms |
48812 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
1 ms |
1620 KB |
Output is correct |
35 |
Correct |
177 ms |
48756 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
7 ms |
7000 KB |
Output is correct |
38 |
Correct |
126 ms |
48512 KB |
Output is correct |
39 |
Correct |
119 ms |
48536 KB |
Output is correct |