#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define rng(i,c,n) for(int i=c;i<n;i++)
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define vec(...) vector<__VA_ARGS__>
#define _3dSgv16 ios::sync_with_stdio(0),cin.tie(0)
typedef long long ll;
using pii=pair<int,int>;
using vi=vector<int>;
void print(){cout<<'\n';}
template<class h,class...t>
void print(const h&v,const t&...u){cout<<v<<' ',print(u...);}
// e
using T=pair<pii,int>;
signed main(){
_3dSgv16;
int h,w;
cin>>h>>w;
const int n=h*w;
vec(string) a(h);
rep(i,h){
cin>>a[i];
}
auto get_id=[&](int x,int y,int t)->int{
return x*w+y+t*n;
};
auto get_pnt=[&](int id)->T{
int t=0;
if(id>=n){
id-=n;
t=1;
}
return {{id/w,id%w},t};
};
const int inf=1e9;
priority_queue<pii,vec(pii),greater<pii>> pq;
vi dp(2*n,inf);
rep(i,h){
rep(j,w){
if(a[i][j]=='C'){
int id=get_id(i,j,0);
dp[id]=0;
pq.push({0,id});
break;
}
}
}
auto ok=[&](int x,int y)->bool{
return min(x,y)>=0 and x<h and y<w;
};
auto push=[&](int id,int w){
if(dp[id]>w){
dp[id]=w;
pq.push({w,id});
}
};
const int di[]={1,-1,0,0};
const int dj[]={0,0,1,-1};
while(sz(pq)){
auto top=pq.top();
pq.pop();
if(dp[top.se]!=top.fi) continue;
auto v=get_pnt(top.se);
int i=v.fi.fi,j=v.fi.se,t=v.se;
int w=top.fi;
// print(i,j,t,w);
rep(dir,4){
int ni=i,nj=j;
rep(_,max(h,w)+1){
ni+=di[dir],nj+=dj[dir];
if(!ok(ni,nj) or a[ni][nj]=='#'){
break;
}
if(!_){
int nid=get_id(ni,nj,0);
push(nid,w+1);
}
int nni=ni+di[dir],nnj=nj+dj[dir];
if(ok(nni,nnj) and a[nni][nnj]=='#'){
int nid=get_id(ni,nj,1);
push(nid,w+1);
}
}
}
}
// print(dp[get_id(1,5,1)]);
int ans=inf;
rep(i,h){
rep(j,w){
if(a[i][j]=='F'){
ans=min(dp[get_id(i,j,0)],dp[get_id(i,j,1)]);
}
}
}
print(ans==inf?"nemoguce":to_string(ans));
}
Compilation message
portal.cpp: In function 'int main()':
portal.cpp:78:27: warning: unused variable 't' [-Wunused-variable]
78 | int i=v.fi.fi,j=v.fi.se,t=v.se;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
316 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
540 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
14 ms |
848 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
47 ms |
1876 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
66 ms |
2500 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
73 ms |
2896 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |