2709: [Violet 1]迷宫花园
Time Limit: 5 Sec Memory Limit: 128 MBSubmit: 976 Solved: 340
[Submit][Status][Discuss]
Description
Input
Output
Sample Input
5
10.28 9 9
#########
# #
# # # # #
#S# #
##### # #
## # #
# ### ###
##E #
#########
4.67 9 9
#########
# ## ##
### #S# #
# # E ##
# # #####
# ## ###
# ##### #
# # #
#########
39.06 9 9
#########
# #
# # # # #
# E# # #
# # # # #
## ### #
# # #S# #
##### #
#########
24.00 9 9
#########
# ##
# # ## ##
# # ##
###S## E#
### # ##
# # # #
##### # #
#########
25.28 9 9
#########
# S##E# #
# ### # #
# ## #
# ## ###
# # ####
# # # ###
# #
#########
10.28 9 9
#########
# #
# # # # #
#S# #
##### # #
## # #
# ### ###
##E #
#########
4.67 9 9
#########
# ## ##
### #S# #
# # E ##
# # #####
# ## ###
# ##### #
# # #
#########
39.06 9 9
#########
# #
# # # # #
# E# # #
# # # # #
## ### #
# # #S# #
##### #
#########
24.00 9 9
#########
# ##
# # ## ##
# # ##
###S## E#
### # ##
# # # #
##### # #
#########
25.28 9 9
#########
# S##E# #
# ### # #
# ## #
# ## ###
# # ####
# # # ###
# #
#########
Sample Output
0.41000
4.67000
3.34000
5.00000
1.69000
4.67000
3.34000
5.00000
1.69000
HINT
Source
实数二分+最短路判定
很水的一道题,注意读数据的时候,空格scanf和cin会自动忽略,用gets去读
#include <bits/stdc++.h>
#define ll long long
#define eps 1e-7
using namespace std;
inline int read(){
int x=0;int f=1;char ch=getchar();
while(!isdigit(ch)) {if(ch=='-') f=-1;ch=getchar();}
while(isdigit(ch)) {x=x*10+ch-'0';ch=getchar();}
return x*f;
}
const int MAXN=1e4+10;
struct node{
int x,y;
}e[MAXN];
const int dx[4]={0,0,-1,1};
const int dy[4]={-1,1,0,0};
int n,m,T,vis[MAXN],S,E;
char ch[110][110];
double dis[MAXN],L;
typedef pair < double,int > pii;
priority_queue < pii,vector<pii>,greater<pii> > q;
namespace zhangenming{
inline int di(int xx,int yy){
return (xx-1)*m+yy;
}
inline void dijsktra(double vv){
for(int i=1;i<=n*m;i++){
dis[i]=1e30;
}
memset(vis,0,sizeof(vis));
while(!q.empty()) q.pop();
q.push(make_pair(0,S));dis[S]=0;
while(!q.empty()){
int tn=q.top().second;q.pop();
if(vis[tn]) continue;vis[tn]=1;
for(int i=0;i<4;i++){
if(i>=2){
int xx=e[tn].x+dx[i];int yy=e[tn].y+dy[i];
if(yy==0||yy==m+1||ch[xx][yy]=='#') continue;
if(dis[di(xx,yy)]>dis[tn]+vv){
dis[di(xx,yy)]=dis[tn]+vv;
q.push(make_pair(dis[di(xx,yy)],di(xx,yy)));
}
}
else{
int xx=e[tn].x+dx[i];int yy=e[tn].y+dy[i];
if(xx==0||xx==n+1||ch[xx][yy]=='#') continue;
if(dis[di(xx,yy)]>dis[tn]+1){
dis[di(xx,yy)]=dis[tn]+1;
q.push(make_pair(dis[di(xx,yy)],di(xx,yy)));
}
}
}
}
}
void work(){
double l=0;double r=10;
while((r-l)>eps){
double mid=(l+r)*0.5;
dijsktra(mid);
if((L-dis[E])>-eps) l=mid;
else r=mid;
}
dijsktra(r);
if(dis[E]-L<=eps) printf("%.5lf\n",r);
else printf("%.5lf\n",l);
}
void solve(){
T=read();
while(T--){
scanf("%lf",&L);n=read();m=read();char c=getchar();
for(int i=1;i<=n;i++){
gets(ch[i]+1);
for(int j=1;j<=m;j++){
if(ch[i][j]=='S') S=di(i,j);
if(ch[i][j]=='E') E=di(i,j);
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
e[(i-1)*m+j].x=i;e[(i-1)*m+j].y=j;
}
}
work();
}
}
}
int main(){
using namespace zhangenming;
solve();
return 0;
}