#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define vi vector<int>
#define ve vector
#define ll long long
#define vl vector<ll>
#define vll vector<pair<ll,ll>>
#define onbit __builtin_popcount
#define ii pair<int,int>
#define vvi vector<vi>
#define vii vector<ii>
#define gii greater<ii>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define INF 1e18
#define eps 1e-7
#define eps1 1e-2
#define optimise ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define MAX_A 1e5+5
using namespace std;
using namespace __gnu_pbds;
template <class T>
using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const ll MOD = 1e9+7;
const int nax = 1e3+5;
const int MAX_VAL = 1e6+1;
double PI=3.14159265359;
int arx[8]={1,0,0,-1,-1,-1, 1, 1};
int ary[8]={0,1,-1, 0, 1,-1,-1, 1};
void setIO(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
int n,m;
char grid[nax][nax];
bool can(int x,int y){
if(x<0||y<0||x>=n||y>=m) return false;
return grid[x][y]!='#';
}
ii dfs(int x,int y,ii pas){
while(can(x+pas.fi,y+pas.se)){
x+=pas.fi;
y+=pas.se;
}
return {x,y};
}
int main(){
optimise;
cin>>n>>m;
ii start;
ii last;
int dis[n][m][2];
queue<ii> q;
int b[n][m];
memset(b,0,sizeof b);
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < m; ++j)
{
dis[i][j][0]=dis[i][j][1]=1e9;
cin>>grid[i][j];
b[i][j]=min(min(n-i,m-j),min(i+1,j+1));
if(grid[i][j]=='C') last={i,j};
if(grid[i][j]=='S') start={i,j};
else if(grid[i][j]=='#') {
q.push({i,j});
b[i][j]=0;
}
}
}
while(!q.empty()){
auto node=q.front();
int d=b[node.fi][node.se];
q.pop();
for (int i = 0; i < 4; ++i)
{
int x=node.fi+arx[i];
int y=node.se+ary[i];
if(!can(x,y)) continue;
if(b[x][y]>d+1){
b[x][y]=d+1;
q.push({x,y});
}
}
}
priority_queue<pair<ii,ii>,vector<pair<ii,ii>>,greater<pair<ii,ii>>> pq;
pq.push({{0,0},start});
dis[start.fi][start.se][0]=dis[start.fi][start.se][1]=0;
while(!pq.empty()){
ii node=pq.top().se;
int type=pq.top().fi.se;
int d=pq.top().fi.fi;
if(node==last){
cout << d <<endl;
return 0;
}
pq.pop();
if(type==1&&dis[node.fi][node.se][type]>=dis[node.fi][node.se][0]) continue;
if(dis[node.fi][node.se][type]<d) continue;
//cout <<node.fi<<" "<<node.se<<" "<<d<<" "<<type<<endl;
for (int i = 0; i < 4; ++i)
{
int x=node.fi+arx[i];
int y=node.se+ary[i];
if(!can(x,y)) continue;
int newd=d+1;
if(dis[x][y][type]<=newd) continue;
dis[x][y][type]=newd;
if(type==0) dis[x][y][1]=min(dis[x][y][1],newd);
pq.push({{newd,type},{x,y}});
}
if(type==0){
for (int i = 0; i < 4; ++i)
{
ii pas={arx[i],ary[i]};
ii to=dfs(node.fi,node.se,pas);
/*ii inverse={-arx[i],-ary[i]};
if(!can(inverse.fi+node.fi,inverse.se+node.se)){
int newd=d+1;
if(dis[to.fi][to.se][0]>newd){
dis[to.fi][to.se][0]=newd;
dis[to.fi][to.se][1]=min(dis[to.fi][to.se][1],newd);
pq.push({{newd,0},{to.fi,to.se}});
}
}*/
int newd=d+b[node.fi][node.se];
if(dis[to.fi][to.se][0]>newd){
dis[to.fi][to.se][0]=newd;
pq.push({{newd,0},{to.fi,to.se}});
}
}
}
}
}
Compilation message
portals.cpp: In function 'void setIO(std::string)':
portals.cpp:35:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
35 | freopen((s + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
portals.cpp:36:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
36 | freopen((s + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
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 |
1 ms |
464 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
472 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 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 |
468 KB |
Output is correct |
5 |
Correct |
0 ms |
468 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 |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
504 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
6 ms |
1292 KB |
Output is correct |
6 |
Correct |
7 ms |
1368 KB |
Output is correct |
7 |
Correct |
5 ms |
1372 KB |
Output is correct |
8 |
Correct |
3 ms |
1372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
544 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
2 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
6 ms |
1364 KB |
Output is correct |
15 |
Correct |
7 ms |
1368 KB |
Output is correct |
16 |
Correct |
5 ms |
1372 KB |
Output is correct |
17 |
Correct |
7 ms |
1112 KB |
Output is correct |
18 |
Correct |
7 ms |
1332 KB |
Output is correct |
19 |
Correct |
3 ms |
1116 KB |
Output is correct |
20 |
Correct |
17 ms |
1176 KB |
Output is correct |
21 |
Correct |
7 ms |
1372 KB |
Output is correct |
22 |
Correct |
6 ms |
1116 KB |
Output is correct |
23 |
Correct |
6 ms |
1204 KB |
Output is correct |
24 |
Correct |
28 ms |
1112 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
1 ms |
512 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
2 ms |
1372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
2 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
560 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
6 ms |
1116 KB |
Output is correct |
15 |
Correct |
7 ms |
1372 KB |
Output is correct |
16 |
Correct |
5 ms |
1372 KB |
Output is correct |
17 |
Correct |
6 ms |
1116 KB |
Output is correct |
18 |
Correct |
7 ms |
1116 KB |
Output is correct |
19 |
Correct |
3 ms |
1116 KB |
Output is correct |
20 |
Correct |
18 ms |
1116 KB |
Output is correct |
21 |
Correct |
5 ms |
1116 KB |
Output is correct |
22 |
Correct |
7 ms |
1116 KB |
Output is correct |
23 |
Correct |
8 ms |
1116 KB |
Output is correct |
24 |
Correct |
199 ms |
18204 KB |
Output is correct |
25 |
Execution timed out |
1039 ms |
14780 KB |
Time limit exceeded |
26 |
Halted |
0 ms |
0 KB |
- |