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. 已知一棵完全二叉树,求其节点的个数 要求:时间复杂度低于O(N),N为这棵树的节点个数

    package my_basic.class_4; public class Code_08_CBTNode { // 完全二叉树的节点个数 复杂度低于O(N) public static class ...

  2. C# 替换去除HTML标记方法(正则表达式)

    [from] http://blog.csdn.net/sgear/article/details/6263848/// <summary> /// 将所有HTML标签替换成"& ...

  3. MySQL 5.7.20绿色版安装详细图文教程

    MySQL 5.7.20绿色版安装详细图文教程 MySQL是一个关系型数据库管理系统,由瑞典MySQL AB公司开发,目前属于Oracle旗下产品.这篇文章主要介绍了MySQL 5.7.20绿色版安装 ...

  4. 【贪心】10.24assassin

    题目分析 没有题目分析…… 寄存一下神奇反悔贪心 #include<bits/stdc++.h> ; struct node { int a,b; node(, ):a(x),b(y) { ...

  5. 10GNU C语言函数调用

    6. C 函数调用机制概述 ​ 在 Linux 内核程序 boot/head.s 执行完基本初始化操作之后,就会跳转区执行 init/main.c 程序.那么 head.s 程序时如何把执行控制转交给 ...

  6. MDK editions for Nuvoton devices

    10 Sep 2018 MDK editions for Nuvoton devices For users of Nuvoton devices, Keil® MDK increases its a ...

  7. HDU 5025 Saving Tang Monk(状态转移, 广搜)

    #include<bits/stdc++.h> using namespace std; ; ; char G[maxN][maxN], snake[maxN][maxN]; ]; int ...

  8. Flask-用户角色及权限

    app/models.py class Role(db.Model): __tablename__ = 'roles' id = db.Column(db.Integer, primary_key=T ...

  9. .NET开发时让人头痛的SESSION超时

    前言 不知道大家在使用用.NET的SESSION的时候有没有遇到过很奇怪的问题,不时候不知道怎么回事,这个SESSION就无缘无故的丢失了 怎么也想不通,不是说SESSION很可靠的吗?这个问题要好好 ...

  10. 用上GIT你一定会爱上他

    前言 Git是一个开源的分布式版本控制系统,用以有效.高速的处理从很小到非常大的项目版本管理. Git 是 Linus Torvalds 为了帮助管理 Linux 内核开发而开发的一个开放源码的版本控 ...