Where is the canteen

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1070    Accepted Submission(s): 298

Problem Description
After a long drastic struggle with himself, LL decide to go for some snack at last. But when steping out of the dormitory, he found a serious problem : he can't remember where is the canteen... Even worse is the campus is very dark at night. So, each time he move, he check front, back, left and right to see which of those four adjacent squares are free, and randomly walk to one of the free squares until landing on a canteen.
 
Input
Each case begin with two integers n and m ( n<=15,m<=15 ), which indicate the size of the campus. Then n line follow, each contain m characters to describe the map. There are 4 different type of area in the map:

'@' is the start location. There is exactly one in each case.
'#' is an impassible square.
'$' is a canteen. There may be more than one in the campus.
'.' is a free square.

 
Output
Output the expected number of moves required to reach a canteen, which accurate to 6 fractional digits. If it is impossible , output -1.
 
Sample Input
1 2
@$
2 2
@. .$
1 3
@#$
 
Sample Output
1.000000
4.000000
-1
 
题目大意:一个图,一个起点多个终点,求到达终点的步数期望。
 #include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
using namespace std; char map[][];
int vis[][],d[][];//vis记录访问的标号,d记录改点的出度
double A[][];
int dir[][]={,,-,,,,,-};
int n,m,cnt,stx,sty;
const double eps=1e-;
int dcmp(double x)
{
if(fabs(x)<eps) return ;
if(x->eps) return ;
return -;
}
void swap(int &a,int &b){int t=a;a=b;b=t;}
struct point
{
int x,y;
}p,t; void init()
{
int i,j;
memset(vis,-,sizeof(vis));
memset(A,,sizeof(A));
memset(d,,sizeof(d));
for(i=;i<n;i++)
{
getchar();
for(j=;j<m;j++)
{
scanf("%c",&map[i][j]);
if(map[i][j]=='@')
stx=i,sty=j;
}
}
} bool judge(point p)
{
if(<=p.x&&p.x<n&&<=p.y&&p.y<m&&map[p.x][p.y]!='#')
return true;
return false;
} bool bfs()
{
queue<point> Q;
p.x=stx;p.y=sty;cnt=;
Q.push(p);
vis[p.x][p.y]=cnt++;
bool flag=;
while(!Q.empty())
{
p=Q.front();Q.pop();
for(int i=;i<;i++)
{
t.x=p.x+dir[i][];t.y=p.y+dir[i][];
if(judge(t))
{
if(map[t.x][t.y]=='$') flag=;
d[p.x][p.y]++;
if(vis[t.x][t.y]!=-) continue;
vis[t.x][t.y]=cnt++;
Q.push(t);
}
}
}
return flag;
} void build_matrix()
{
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
if(vis[i][j]==-) continue;
int u=vis[i][j];
A[u][u]=;
if(map[i][j]=='$'){A[u][cnt]=;continue;}
double p=1.0/d[i][j];
for(int k=;k<;k++)
{
point temp;
temp.x=i+dir[k][];temp.y=j+dir[k][];
if(judge(temp) && vis[temp.x][temp.y]!=-)
{
int v=vis[temp.x][temp.y];
A[u][v]-=p;
A[u][cnt]+=p;
}
}
}
}
A[][cnt]=;
} void gauss(int n)
{
int i,j,k,r;
for(i=;i<n;i++)
{
r=i;
for(j=i+;j<n;j++)
if(fabs(A[j][i])>fabs(A[r][i])) r=j;
if(dcmp(A[r][i])==) continue;
if(r!=i) for(j=;j<=n;j++) swap(A[r][j],A[i][j]);
for(k=;k<n;k++) if(k!=i)
for(j=n;j>=i;j--) A[k][j]-=A[k][i]/A[i][i]*A[i][j];
}
for(i=n-;i>=;i--)
{
for(j=i+;j<cnt;j++)
A[i][cnt]-=A[j][cnt]*A[i][j];
A[i][cnt]/=A[i][i];
}
} int main()
{
while(~scanf("%d%d",&n,&m))
{
init();
if(!bfs()){puts("-1");continue;}
build_matrix();
gauss(cnt);
printf("%.6lf\n",A[][cnt]);
}
return ;
}
 
 

hdu 2262 高斯消元求期望的更多相关文章

  1. hdu 4418 高斯消元求期望

    Time travel Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  2. hdu 4870 rating(高斯消元求期望)

    Rating Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  3. HDU4870_Rating_双号从零单排_高斯消元求期望

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4870 原题: Rating Time Limit: 10000/5000 MS (Java/Other ...

  4. [ACM] hdu 4418 Time travel (高斯消元求期望)

    Time travel Problem Description Agent K is one of the greatest agents in a secret organization calle ...

  5. hdu 3992 AC自动机上的高斯消元求期望

    Crazy Typewriter Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  6. HDU 5833 (2016大学生网络预选赛) Zhu and 772002(高斯消元求齐次方程的秩)

    网络预选赛的题目……比赛的时候没有做上,确实是没啥思路,只知道肯定是整数分解,然后乘起来素数的幂肯定是偶数,然后就不知道该怎么办了… 最后题目要求输出方案数,首先根据题目应该能写出如下齐次方程(从别人 ...

  7. 高斯消元与期望DP

    高斯消元可以解决一系列DP序混乱的无向图上(期望)DP DP序 DP序是一道DP的所有状态的一个排列,使状态x所需的所有前置状态都位于状态x前: (通俗的说,在一个状态转移方程中‘=’左侧的状态应该在 ...

  8. 【BZOJ2137】submultiple 高斯消元求伯努利数

    [BZOJ2137]submultiple Description 设函数g(N)表示N的约数个数.现在给出一个数M,求出所有M的约数x的g(x)的K次方和. Input 第一行输入N,K.N表示M由 ...

  9. HDU 2827 高斯消元

    模板的高斯消元.... /** @Date : 2017-09-26 18:05:03 * @FileName: HDU 2827 高斯消元.cpp * @Platform: Windows * @A ...

随机推荐

  1. pbr 5.2.1需使用中科大的源,豆瓣的不行

    -bash-4.2$ .tox/tempest/bin/pip install pbr==5.2.1DEPRECATION: Python 2.7 will reach the end of its ...

  2. SQLSTATE=42000 #42000

    在使用PowerDesigner生成数据库表的时候遇到了这个问题. 原来是在填写属性的类型的时候, 少了两个括号, 造成了mysql数据类型错误 本来应该是varchar(50)的,写成了varcha ...

  3. 1_HDFS理论及安装部署

    一.hadoop简介 1.hadoop的初衷是为了解决Nutch的海量数据爬取和存储的需要,HDFS来源于google的GFS,MapReduce来源于Google的MapReduce,HBase来源 ...

  4. 约束Constraints

    1.setNeedsUpdateConstraints:当想要调整子视图布局时,在主线程调用该方法标记constraint需要在未来的某个点更新(该方法不会立刻强制刷新constraint,而是等待下 ...

  5. 设置通过Maven创建的工程的JDK的版本,更改conf/settings.xml

    eclipse提示警告如下: Build path specifies execution environment J2SE-1.5. There are no JREs installed in t ...

  6. 二分查找、upper_bound、lower_bound

    整理及总结二分查找的判断和边界细节 修改版 package com.leej.binarysearch; import java.util.Arrays; /** * @author jerry * ...

  7. [图文]RHEL 7/CentOS 7/Fedora28 联网初始化

    实验说明: 入门Linux,一般会遇到以下几个问题: 从哪里获取LInux镜像? 如何通过镜像文件安装Linux系统? 安装实体机还是虚拟机? 安装完系统如何配置网络? 虚拟机的网络配置与实体机有何不 ...

  8. Eclipse使用Mybatis-Generator插件

    Mybatis-Generator插件极大地方便了我们的开发效率,不用每张表每个字段人工去敲,所以本文介绍使用Mybatis-Generator自动生成Dao.Model.Mapping相关文件 版权 ...

  9. Python基础——模块与包

    在Python中,可以用import导入需要的模块.包.库.文件等. 把工作路径导入系统路径 import os#os是工作台 import sys#sys是系统 sys.path.append(os ...

  10. Python头脑风暴1

    发个致富脑洞:我就在想本人虽然单身,但本人恋爱经历很多,追女生技术十足,女朋友漂亮又贤惠.如果本人开个平台帮人诚心介绍女朋友,男女成男女朋友经男方同意我收2.5万(IT界平均月收入的1.5倍不到),双 ...