#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
signed main(){
_3dSgv16;
int h,w;
cin>>h>>w;
vec(string) a(h);
rep(i,h){
cin>>a[i];
}
auto get_id=[&](int x,int y)->int{
return x*w+y;
};
auto get_pnt=[&](int id)->pii{
return {id/w,id%w};
};
const int inf=1e9;
priority_queue<pii,vec(pii),greater<pii>> pq;
vi dp(h*w,inf);
rep(i,h){
rep(j,w){
if(a[i][j]=='C'){
int id=get_id(i,j);
dp[id]=0;
pq.push({0,id});
}
}
}
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,j=v.se;
int w=top.fi;
int closest_wall=inf;
rep(dir,4){
int ni=i+di[dir],nj=j+dj[dir];
if(ok(ni,nj)){
if(a[ni][nj]!='#'){
int nid=get_id(ni,nj);
push(nid,w+1);
}
}
int now=0;
while(1){
if(ok(ni,nj) and a[ni][nj]=='#'){
closest_wall=min(closest_wall,now);
break;
}
now+=1;
ni+=di[dir],nj+=dj[dir];
}
}
rep(dir,4){
int ni=i,nj=j;
while(1){
ni+=di[dir],nj+=dj[dir];
if(!ok(ni,nj) or a[ni][nj]=='#'){
break;
}
int nni=ni+di[dir],nnj=nj+dj[dir];
if(ok(nni,nnj) and a[nni][nnj]=='#'){
int nid=get_id(ni,nj);
push(nid,w+1+closest_wall);
}
}
}
}
int ans=inf;
rep(i,h){
rep(j,w){
if(a[i][j]=='F'){
ans=dp[get_id(i,j)];
}
}
}
if(ans>=inf){
cout<<"nemoguce\n";
}else{
cout<<ans<<"\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
6 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
596 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
9 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
1124 KB |
Output is correct |
2 |
Correct |
18 ms |
852 KB |
Output is correct |
3 |
Correct |
22 ms |
724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
45 ms |
1364 KB |
Output is correct |
2 |
Correct |
1 ms |
1236 KB |
Output is correct |
3 |
Correct |
38 ms |
980 KB |
Output is correct |
4 |
Correct |
24 ms |
852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
60 ms |
1612 KB |
Output is correct |
2 |
Correct |
2 ms |
1492 KB |
Output is correct |
3 |
Correct |
59 ms |
1492 KB |
Output is correct |
4 |
Correct |
64 ms |
1492 KB |
Output is correct |