This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |