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. c++作业:使用函数调用的方法,实现求两个整数中大的数的程序。

    #include <iostream> using namespace std; int main(){ //从键盘接收两个整数,保存在变量num1和num2中 cout<<& ...

  2. (转发)InputAccessoryView的使用方法

    转自:http://blog.sina.com.cn/s/blog_45e2b66c01015we9.html UITextFields and UITextViews have an inputAc ...

  3. Postman 没有走hosts文件

    问题: 在Windows10系统中,从官方下载Postman安装并登录后,创建一个请求并执行.但这个请求并没有走hosts文件中定义的192.168.33.10主机,而是走到了线上的主机. 分析: 通 ...

  4. kubernetes安装rabbitmq集群

    1.准备K8S环境 2.下载基础镜像,需要安装两种插件:autocluster.rabbitmq_management 方法一: 下载已有插件镜像 [root@localhost ~]#docker ...

  5. VNC远程登录端使用经验之一

    1.vnc/xmanager都是经常用的远程登录软件.vnc有个缺点就是他的进程不会自动退出比如如果开了PID1再去开PID2...PIDn.那么前面的PIDn-1个进程就会一直运行如果不手动kill ...

  6. [译]The Python Tutorial#5. Data Structures

    [译]The Python Tutorial#Data Structures 5.1 Data Structures 本章节详细介绍之前介绍过的一些内容,并且也会介绍一些新的内容. 5.1 More ...

  7. GoF23种设计模式之结构型模式之享元模式

    一.概述  运用共享技术有效地支持大量细粒度的对象. 二.适用性 1.当一个应用程序使用了大量的对象的时候. 2.由于使用大量的独享而造成很大的存储开销的时候. 3.对象的大多数状态都可变为外部状态的 ...

  8. 原生Ajax+springBoot实现用户登录

    思路:用户输入登录信息——信息传到后台——数据库查询——比较查询结果——返回登录信息(成功/失败) html页面代码: <!DOCTYPE html> <html lang=&quo ...

  9. npm 安装express npm ERR! code UNABLE_TO_VERIFY_LEAF_SIGNATURE

    npm安装总是报错 报错信息: 解决方案: 在命令行输入: npm config set strict-ssl false 设置之后即可安装软件:

  10. day03_11 if语句实现猜年龄01

    老男孩猜年龄游戏 age_of_princal = 56 guess_age = int( input(">>:") ) #以下为伪代码 ''' if guess_ag ...