hdu 2262 高斯消元求期望
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
'@' 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.
#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 高斯消元求期望的更多相关文章
- hdu 4418 高斯消元求期望
Time travel Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- hdu 4870 rating(高斯消元求期望)
Rating Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- HDU4870_Rating_双号从零单排_高斯消元求期望
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4870 原题: Rating Time Limit: 10000/5000 MS (Java/Other ...
- [ACM] hdu 4418 Time travel (高斯消元求期望)
Time travel Problem Description Agent K is one of the greatest agents in a secret organization calle ...
- hdu 3992 AC自动机上的高斯消元求期望
Crazy Typewriter Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 5833 (2016大学生网络预选赛) Zhu and 772002(高斯消元求齐次方程的秩)
网络预选赛的题目……比赛的时候没有做上,确实是没啥思路,只知道肯定是整数分解,然后乘起来素数的幂肯定是偶数,然后就不知道该怎么办了… 最后题目要求输出方案数,首先根据题目应该能写出如下齐次方程(从别人 ...
- 高斯消元与期望DP
高斯消元可以解决一系列DP序混乱的无向图上(期望)DP DP序 DP序是一道DP的所有状态的一个排列,使状态x所需的所有前置状态都位于状态x前: (通俗的说,在一个状态转移方程中‘=’左侧的状态应该在 ...
- 【BZOJ2137】submultiple 高斯消元求伯努利数
[BZOJ2137]submultiple Description 设函数g(N)表示N的约数个数.现在给出一个数M,求出所有M的约数x的g(x)的K次方和. Input 第一行输入N,K.N表示M由 ...
- HDU 2827 高斯消元
模板的高斯消元.... /** @Date : 2017-09-26 18:05:03 * @FileName: HDU 2827 高斯消元.cpp * @Platform: Windows * @A ...
随机推荐
- 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 ...
- SQLSTATE=42000 #42000
在使用PowerDesigner生成数据库表的时候遇到了这个问题. 原来是在填写属性的类型的时候, 少了两个括号, 造成了mysql数据类型错误 本来应该是varchar(50)的,写成了varcha ...
- 1_HDFS理论及安装部署
一.hadoop简介 1.hadoop的初衷是为了解决Nutch的海量数据爬取和存储的需要,HDFS来源于google的GFS,MapReduce来源于Google的MapReduce,HBase来源 ...
- 约束Constraints
1.setNeedsUpdateConstraints:当想要调整子视图布局时,在主线程调用该方法标记constraint需要在未来的某个点更新(该方法不会立刻强制刷新constraint,而是等待下 ...
- 设置通过Maven创建的工程的JDK的版本,更改conf/settings.xml
eclipse提示警告如下: Build path specifies execution environment J2SE-1.5. There are no JREs installed in t ...
- 二分查找、upper_bound、lower_bound
整理及总结二分查找的判断和边界细节 修改版 package com.leej.binarysearch; import java.util.Arrays; /** * @author jerry * ...
- [图文]RHEL 7/CentOS 7/Fedora28 联网初始化
实验说明: 入门Linux,一般会遇到以下几个问题: 从哪里获取LInux镜像? 如何通过镜像文件安装Linux系统? 安装实体机还是虚拟机? 安装完系统如何配置网络? 虚拟机的网络配置与实体机有何不 ...
- Eclipse使用Mybatis-Generator插件
Mybatis-Generator插件极大地方便了我们的开发效率,不用每张表每个字段人工去敲,所以本文介绍使用Mybatis-Generator自动生成Dao.Model.Mapping相关文件 版权 ...
- Python基础——模块与包
在Python中,可以用import导入需要的模块.包.库.文件等. 把工作路径导入系统路径 import os#os是工作台 import sys#sys是系统 sys.path.append(os ...
- Python头脑风暴1
发个致富脑洞:我就在想本人虽然单身,但本人恋爱经历很多,追女生技术十足,女朋友漂亮又贤惠.如果本人开个平台帮人诚心介绍女朋友,男女成男女朋友经男方同意我收2.5万(IT界平均月收入的1.5倍不到),双 ...