Where is the canteen

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

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
 
  1. /*
  2. 将点所有点令为1至n*m-1那么建立n*m个方程
  3. 对于一个任一个点En=(E1+E2+E3)/cnt+1; cnt为可以走的方向数 [0,4]
  4. 最后用高斯消元模版求解(用kuangbin的模版不知为什么这么慢)
  5. 而且餐厅有很多个
  6. */
  7. #include<iostream>
  8. #include<cmath>
  9. #include<queue>
  10. #include<cstdio>
  11. #include<algorithm>
  12. #include<cstring>
  13. using namespace std;
  14.  
  15. #define eps 1e-12
  16. const int MAXN = 250;
  17. char mp[20][20];
  18. bool vis[20][20];
  19. int n,m;
  20. int sx,sy;
  21. int dx[4]={-1,1,0,0};
  22. int dy[4]={0,0,1,-1};
  23. struct Node{
  24. int x,y;
  25. }aa,bb;
  26. queue<Node>q;
  27. int temp[MAXN];
  28. double a[MAXN][MAXN];
  29. double x[MAXN];
  30. int equ,var;
  31.  
  32. inline int C(int x,int y){
  33. return x*m+y;
  34. }
  35. bool Ok(int x,int y,int d){
  36. if(d==0){
  37. if(x>=0 && x<n && y>=0 && y<m && mp[x][y]!='#' && !vis[x][y]) return true;
  38. }else {
  39. if(x>=0 && x<n && y>=0 && y<m && mp[x][y]!='#' && vis[x][y]) return true;
  40. }
  41. return false;
  42. }
  43. void Bfs(){
  44. int i,j,k;
  45. while(!q.empty()){
  46. bb=q.front(); q.pop();
  47. for(i=0;i<4;i++){
  48. aa.x=bb.x+dx[i];
  49. aa.y=bb.y+dy[i];
  50. if(Ok(aa.x,aa.y,0)){
  51. vis[aa.x][aa.y]=1;
  52. q.push(aa);
  53. }
  54. }
  55. }
  56. }
  57.  
  58. void Makefunction(){
  59. int i,j,k;
  60. for(i=0;i<n;i++)
  61. for(j=0;j<m;j++){
  62. int cnt=0;
  63. if(mp[i][j]=='#') continue;
  64. if(mp[i][j]=='$'){
  65. a[C(i,j)][C(i,j)]=1; continue;
  66. }
  67. for(k=0;k<4;k++){
  68. int xx=i+dx[k];
  69. int yy=j+dy[k];
  70. if(Ok(xx,yy,1)){
  71. cnt++; a[C(i,j)][C(xx,yy)]=1;
  72. }
  73. }
  74. a[C(i,j)][C(i,j)]=-1*cnt;
  75. x[C(i,j)]=-1*cnt;
  76. }
  77. }
  78. int Gauss(){
  79. int i,j,k,col,max_r; //max_r 指现在对哪一行操作 equ 方程数 var 未知数个数
  80. for(k=0,col=0;k<equ && col<var;k++,col++){
  81. max_r=k;
  82. for(i=k+1;i<equ;i++)
  83. if(fabs(a[i][col])>fabs(a[max_r][col])) max_r=k; //寻找这个未知数最大的一个
  84. if(fabs(a[max_r][col])<eps) {
  85. if(col==C(sx,sy)) return 0;
  86. else continue;
  87. }
  88. if(k!=max_r){
  89. for(j=col;j<var;j++) swap(a[k][j],a[max_r][j]);
  90. swap(x[k],x[max_r]);
  91. }
  92. x[k]/=a[k][col];
  93. for(j=col+1;j<var;j++) a[k][j]/=a[k][col];
  94. a[k][col]=1;
  95. for(i=0;i<equ;i++)
  96. if(i!=k){
  97. x[i]-=x[k]*a[i][col];
  98. for(j=col+1;j<var;j++) a[i][j]-=a[k][j]*a[i][col];
  99. a[i][col]=0;
  100. }
  101. }
  102. return 1;
  103. }
  104.  
  105. int main(){
  106. //freopen("in.txt","r",stdin);
  107. int i,j,k;
  108. while(~scanf("%d %d",&n,&m)){
  109. q.empty();
  110. memset(vis,0,sizeof(vis));
  111. for(i=0;i<n;i++) scanf("%s",mp[i]);
  112. for(i=0;i<n;i++)
  113. for(j=0;j<m;j++){
  114. if(mp[i][j]=='@'){ sx=i; sy=j; }
  115. if(mp[i][j]=='$'){ aa.x=i; aa.y=j; q.push(aa); vis[i][j]=1; }
  116. }
  117. Bfs();
  118. memset(a,0,sizeof(a));
  119. memset(x,0,sizeof(x));
  120. Makefunction();
  121. var=n*m; equ=n*m;
  122. ;
  123. if(vis[sx][sy] && Gauss() ) printf("%lf\n",x[C(sx,sy)]);
  124. else printf("-1\n");
  125. }
  126. return 0;
  127. }

hdu2262 Where is the canteen的更多相关文章

  1. HDU-2262 Where is the canteen 概率DP,高斯消元

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2262 题意:LL在一个迷宫里面转,每次走向周围能走的点的概率都是一样的,现在LL要随机的走到cante ...

  2. HDU2262;Where is the canteen(高斯消元+期望)

    传送门 题意 给出一张图,LL从一个点等概率走到上下左右位置,询问LL从宿舍走到餐厅的步数期望 分析 该题是一道高斯消元+期望的题目 难点在于构造矩阵,我们发现以下结论 设某点走到餐厅的期望为Ek 1 ...

  3. HDU 2262 Where is the canteen 期望dp+高斯消元

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2262 Where is the canteen Time Limit: 10000/5000 MS ...

  4. unknown directive "" in E:\canteen\nginx-1.16.0/conf/nginx.conf:3-------文本编辑器修改nginx配置文件的坑

    nignx小白一个,今天在配置nginx的时候,理所当然的用了文本编辑器编辑并保存了一下nginx的nginx.conf配置文件,一不小心就折腾了几个钟. 保存之后就nginx -s reload一下 ...

  5. 1350. Canteen(map)

    1350 这题没什么  就考一下map的用法吧 #include <iostream> #include<cstdio> #include<cstring> #in ...

  6. hdu2262 高斯消元

    题目:有一个地图,一个人从某个点出发,问走到花园的期望步数为多少 设某点的期望步数为Ei. 那么目标的Ei=0. Ei=(Enext1+Enext2……Enextk)/k+1. 为什么是这个公式 因为 ...

  7. SQLServer------Join的使用方法

    参考菜鸟教程网: http://www.runoob.com/sql/sql-join.html select a.Canteen,b.OrderNum,b.CreateTime,c.Name fro ...

  8. IELTS - Word List 28

    1, The lawsuit is very much o the lawyer's mind. 2, The canteen was absolutely packed. 3, Doctors di ...

  9. Do things for others

    早上,按照平常的时间去吃早饭,食堂格外的空旷,打饭的员工说今天人很少,我说昨天是有元旦晚会,她说今天是放假,我后来想,还是她说的更有道理.她看的比我清楚更清楚! 幸亏昨晚上记录下了早上要帮别人搜论文的 ...

随机推荐

  1. git的一些常见命令

    一.新建代码库 # 在当前目录新建一个Git代码库 $ git init # 新建一个目录,将其初始化为Git代码库 $ git init [project-name] # 下载一个项目和它的整个代码 ...

  2. 除了使用URLSearchParams处理axios发送的数据,但是兼容性不好,其他的兼容方法

    在使用axios这个ajax插件的时候,我们有些时候会遇到一些问题,比如:数据格式不正确 以最简单的例子为基础(这里使用post方法): 在上面的例子中我们直接调用axios的post方法,传给后台的 ...

  3. 【IT人】如何提高阅读源代码的效率

    1.最近刚到公司,公司就发一架构代码自己看,看了几天看的想吐,也在网上找了下相关的技巧吧,不是有句话叫做:成功必有方法,失败总是借口! 2.借鉴别人的方法来看看如下: 记得在开源流行之前,我看过的代码 ...

  4. JAVAEE——BOS物流项目04:学习计划、datagrid、分页查询、批量删除、修改功能

    1 学习计划 1.datagrid使用方法(重要) n 将静态HTML渲染为datagrid样式 n 发送ajax请求获取json数据创建datagrid n 使用easyUI提供的API创建data ...

  5. SpringMVC之Http标准的头部信息

  6. javamail+ical4j发送会议提醒

    本篇讲述小编在使用ical4j时对其的理解与使用,留作笔记的同时希望能帮助到大家! 初学者可以先了解下ical4j的基本信息: iCalender编程基础,了解与使用ical4j:https://ww ...

  7. 恢复linux系统文件夹颜色

    /etc/DIR_COLORS 默认值 # Background color codes:# 40=black 41=red 42=green 43=yellow 44=blue 45=magenta ...

  8. 阿里云 virtual memory exhausted: 无法分配内存

    在阿里云买了个云服务器,内存1G.编译php时出现下面的错误: virtual memory exhausted: Cannot allocate memory 问题原因:由于物理内存本身很小,且阿里 ...

  9. PHP的 first day of 和 last day of

    话不多说,先上代码(当前是2017年6月2日) echo date("Y-m-d", strtotime("2017-02 first day of")).'& ...

  10. ansible安装

    本文来自我的github pages博客http://galengao.github.io/ 即www.gaohuirong.cn 1.配置epel源 wget -O /etc/yum.repos.d ...