This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// __builtin_popcount(x) broj bitova
// __builtin_popcountll(x)
// __builtin_clz(x) vodece nule
// __builtin_clzll(x)
// __builtin_ctz(x) nule na pocetku
// __builtin_ctzll(x)
// 2000000011
// 2000000033
#include "bits/stdc++.h"
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#define ld double
#define ll long long
#define ull unsigned long long
#define llinf 100000000000000000LL // 10^17
#define iinf 2000000000 // 2*10^9
#define pb push_back
#define popb pop_back
#define fi first
#define sc second
#define endl '\n'
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pld pair<ld,ld>
#define sz(a) int(a.size())
#define all(a) a.begin(),a.end()
#define rall(a) a.begin(),a.end(),greater<int>()
#define getunique(v) {sort(all(v)); v.erase(unique(all(v)), v.end());}
#define pi 3.14159265358979323846
#define here cerr<<"---------------------------\n"
#define ceri(a,l,r) {for(ll i = l;i<=r;i++) cerr<<a[i]<< " ";cerr<<endl;}
#define ceri2(a,l,r,n,m) {for(ll i = l;i<=r;i++){for(ll j = n;j<=m;j++) cerr<<a[i][j]<< " ";cerr<<endl;}}
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
using namespace std;
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll rnd(ll l,ll r){
return uniform_int_distribution<ll>(l,r)(rng);
}
void setIO(string inoutname)
{
freopen((inoutname+".in").c_str(),"r",stdin);
freopen((inoutname+".out").c_str(),"w",stdout);
}
#define mod 1
ll gcd(ll a, ll b)
{
if(b==0) return a;
if(a==0) return b;
if(a>=b) return gcd(a%b,b);
return gcd(a,b%a);
}
ll lcm(ll a,ll b){
return (a/gcd(a,b))*b;
}
ll add(ll a,ll b){
a+=b;
if(a<0) a+=mod;
if(a>=mod) a%=mod;
return a;
}
ll mul(ll a,ll b){return(a*b)%mod;}
#define maxn 1005
ll n,m;
ll a[maxn][maxn];
vector<ll> g[maxn];
ll dis[maxn*maxn];
ll lasti[maxn*maxn];
bool vis[maxn*maxn];
ll kod(ll x,ll y){
if(n>m) return (x-1)*n+y;
return (y-1)*m+x;
}
vector<ll> dx = {1,-1,0,0};
vector<ll> dy = {0,0,1,-1};
pll f(ll x){
for(ll i = 1;i<=m;i++){
if((x-i)%n==0) return {(x-i)/n,i};
}
}
bool valid(ll x,ll y){
return a[x][y]==1&&x>=1&&x<=n&&y<=m&&y>=1;
}
map<ll,pll> mp;
void tc(){
ios_base::sync_with_stdio(false);cerr.tie(0);cout.tie(0);cin.tie(0);
cin >> n >> m;
ll px,py,kx,ky;
for(ll i = 1;i<=n;i++){
string s; cin >> s;
for(ll j = 1;j<=m;j++){
char c = s[j-1];
if(c=='S') px = i,py = j,a[i][j] = 1;
if(c=='C') kx = i,ky = j,a[i][j] = 1;
if(c=='.') a[i][j] = 1;
}
}
for(ll i = 1;i<=n;i++){
for(ll j = 1;j<=m;j++){
if(!valid(i,j)) continue;
for(ll k = 0;k<4;k++){
ll ni = i+dx[k];
ll nj = j+dy[k];
if(valid(ni,nj)){
g[kod(i,j)].pb(kod(ni,nj));
g[kod(ni,nj)].pb(kod(i,j));
}
}
}
}
for(ll i = 1;i<=n;i++){
ll last = -1;
for(ll j = 1;j<=m;j++){
if(last==-1){
if(a[i][j]==1) last = j;
continue;
}
if(a[i][j]==0){
last = -1;
continue;
}
g[kod(i,j)].pb(kod(i,last));
//g[kod(i,last)].pb(kod(i,j));
}
last = -1;
for(ll j = m;j>=1;j--){
if(last==-1){
if(a[i][j]==1) last = j;
continue;
}
if(a[i][j]==0){
last = -1;
continue;
}
g[kod(i,j)].pb(kod(i,last));
//g[kod(i,last)].pb(kod(i,j));
}
}
for(ll i = 1;i<=n;i++){
for(ll j = 1;j<=m;j++){
if(mp.find(kod(i,j))!=mp.end()) here;
mp[kod(i,j)] = {i,j};
}
}
for(ll i = 1;i<=m;i++){
ll last = -1;
for(ll j = 1;j<=n;j++){
if(last==-1){
if(a[j][i]==1) last = j;
continue;
}
if(a[j][i]==0){
last = -1;
continue;
}
g[kod(j,i)].pb(kod(last,i));
//g[kod(last,i)].pb(kod(j,i));
}
last = -1;
for(ll j = n;j>=1;j--){
if(last==-1){
if(a[j][i]==1) last = j;
continue;
}
if(a[j][i]==0){
last = -1;
continue;
}
g[kod(j,i)].pb(kod(last,i));
//g[kod(last,i)].pb(kod(j,i));
}
}
queue<ll> q;
q.push(kod(px,py));
fill(dis,dis+n*m+1,llinf);
dis[kod(px,py)] = 0;
lasti[q.front()] = q.front();
while(sz(q)){
ll u = q.front();
q.pop();
for(ll s : g[u]){
if(vis[s]) continue;
vis[s] = 1;
q.push(s);
dis[s] = dis[u]+1;
lasti[s] = u;
}
}
ll s = kod(kx,ky);
/*while(lasti[s]!=s){
pll p = f(s);
cerr<<mp[s].fi<< " "<<mp[s].sc<<endl;
s = lasti[s];
}*/
cout<<dis[kod(kx,ky)]<<endl;
}
int main(){
ios_base::sync_with_stdio(false);cerr.tie(0);cout.tie(0);cin.tie(0);
//setIO("lol");
int t; t = 1;
while(t--){
tc();
}
return 0;
}
Compilation message (stderr)
portals.cpp: In function 'void tc()':
portals.cpp:194:8: warning: unused variable 's' [-Wunused-variable]
194 | ll s = kod(kx,ky);
| ^
portals.cpp: In function 'void setIO(std::string)':
portals.cpp:47:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
47 | freopen((inoutname+".in").c_str(),"r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
portals.cpp:48:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
48 | freopen((inoutname+".out").c_str(),"w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
portals.cpp: In function 'std::pair<long long int, long long int> f(long long int)':
portals.cpp:85:1: warning: control reaches end of non-void function [-Wreturn-type]
85 | }
| ^
portals.cpp: In function 'void tc()':
portals.cpp:76:22: warning: 'kx' may be used uninitialized in this function [-Wmaybe-uninitialized]
76 | if(n>m) return (x-1)*n+y;
| ~~^~~
portals.cpp:93:14: note: 'kx' was declared here
93 | ll px,py,kx,ky;
| ^~
portals.cpp:76:28: warning: 'ky' may be used uninitialized in this function [-Wmaybe-uninitialized]
76 | if(n>m) return (x-1)*n+y;
| ^
portals.cpp:93:17: note: 'ky' was declared here
93 | ll px,py,kx,ky;
| ^~
portals.cpp:77:20: warning: 'px' may be used uninitialized in this function [-Wmaybe-uninitialized]
77 | return (y-1)*m+x;
| ^
portals.cpp:93:8: note: 'px' was declared here
93 | ll px,py,kx,ky;
| ^~
portals.cpp:76:28: warning: 'py' may be used uninitialized in this function [-Wmaybe-uninitialized]
76 | if(n>m) return (x-1)*n+y;
| ^
portals.cpp:93:11: note: 'py' was declared here
93 | ll px,py,kx,ky;
| ^~
# | 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... |