广搜。

根据题意,可以知道状态总共有$4*n*m$种。每一个位置四种状态:两个都没有发现;发现$E$没发现$D$;发现$D$没发现$E$;两个都发现。

每次移动的花费都是$1$,队列里面状态的费用是单调不减的,所以第一次符合要求的位置就是答案。

  1. #pragma comment(linker, "/STACK:1024000000,1024000000")
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<cmath>
  5. #include<algorithm>
  6. #include<vector>
  7. #include<map>
  8. #include<set>
  9. #include<queue>
  10. #include<stack>
  11. #include<ctime>
  12. #include<iostream>
  13. using namespace std;
  14. typedef long long LL;
  15. const double pi=acos(-1.0),eps=1e-;
  16. void File()
  17. {
  18. freopen("D:\\in.txt","r",stdin);
  19. freopen("D:\\out.txt","w",stdout);
  20. }
  21. template <class T>
  22. inline void read(T &x)
  23. {
  24. char c = getchar();
  25. x = ;
  26. while(!isdigit(c)) c = getchar();
  27. while(isdigit(c))
  28. {
  29. x = x * + c - '';
  30. c = getchar();
  31. }
  32. }
  33.  
  34. int T,n,m,t;
  35. char s[][];
  36. int sx,sy,ex,ey,dx,dy;
  37. int dir[][]={ {-,},{,},{,-},{,} };
  38. int f[][][];
  39. int dis[][][];
  40. int flag;
  41.  
  42. struct X
  43. {
  44. int a,b,st;
  45. X(int A,int B,int ST)
  46. {
  47. a=A;
  48. b=B;
  49. st=ST;
  50. }
  51. };
  52.  
  53. bool check(int a,int b)
  54. {
  55. if(a<||a>=n) return ;
  56. if(b<||b>=m) return ;
  57. if(s[a][b]!='.') return ;
  58. return ;
  59. }
  60.  
  61. void bfs()
  62. {
  63. queue<X>Q; int st=f[][sx][sy]*(<<)+f[][sx][sy]*(<<);
  64. Q.push(X(sx,sy,st)); dis[sx][sy][st]=;
  65.  
  66. while(!Q.empty())
  67. {
  68. X h=Q.front(); Q.pop();
  69. if(h.st==)
  70. {
  71. if(dis[h.a][h.b][h.st]<=t)
  72. {
  73. printf("%d\n",dis[h.a][h.b][h.st]);
  74. flag=;
  75. }
  76. return ;
  77. }
  78.  
  79. for(int i=;i<;i++)
  80. {
  81. int tx=h.a+dir[i][],ty=h.b+dir[i][];
  82. if(check(tx,ty)==) continue;
  83. int tst = h.st;
  84. if(f[][tx][ty]==) tst=tst|(<<);
  85. if(f[][tx][ty]==) tst=tst|(<<);
  86. if(dis[tx][ty][tst]!=0x7FFFFFFF) continue;
  87.  
  88. dis[tx][ty][tst]=dis[h.a][h.b][h.st]+;
  89. Q.push(X(tx,ty,tst));
  90. }
  91.  
  92. }
  93. }
  94.  
  95. int main()
  96. {
  97. scanf("%d",&T); int cas=;
  98. while(T--)
  99. {
  100. scanf("%d%d%d",&n,&m,&t);
  101. for(int i=;i<n;i++) scanf("%s",s[i]);
  102.  
  103. for(int i=;i<n;i++)
  104. {
  105. for(int j=;j<m;j++)
  106. {
  107. if(s[i][j]=='S') sx=i,sy=j;
  108. if(s[i][j]=='E') ex=i,ey=j;
  109. if(s[i][j]=='D') dx=i,dy=j;
  110.  
  111. for(int k=;k<;k++) dis[i][j][k]=0x7FFFFFFF;
  112. f[][i][j]=f[][i][j]=;
  113. }
  114. }
  115.  
  116. s[sx][sy]='.';
  117.  
  118. for(int k=;k<=;k++)
  119. {
  120. int a,b;
  121. for(int i=;i<;i++)
  122. {
  123. if(k==) a=ex,b=ey;
  124. else a=dx,b=dy;
  125. for(int j=;;j++)
  126. {
  127. a=a+dir[i][], b=b+dir[i][];
  128. if(check(a,b)==) break;
  129. f[k][a][b]=;
  130. }
  131. }
  132. }
  133.  
  134. printf("Case %d:\n",cas++);
  135. flag=; bfs();
  136. if(flag==) printf("-1\n");
  137. }
  138. return ;
  139. }

HDU 4528 小明系列故事――捉迷藏的更多相关文章

  1. HDU 4828 小明系列故事——捉迷藏

    漂亮妹子点击就送:http://acm.hdu.edu.cn/showproblem.php?pid=4528 Time Limit: 500/200 MS (Java/Others)    Memo ...

  2. HDU 4528 BFS 小明系列故事——捉迷藏

    原题直通车:HDU 4528 小明系列故事——捉迷藏 分析: 标记时加两种状态就行. 代码: #include<iostream> #include<cstring> #inc ...

  3. C - 小明系列故事――捉迷藏 HDU - 4528 bfs +状压 旅游-- 最短路+状压

    C - 小明系列故事――捉迷藏 HDU - 4528 这个题目看了一下题解,感觉没有很难,应该是可以自己敲出来的,感觉自己好蠢... 这个是一个bfs 用bfs就很好写了,首先可以预处理出大明和二明能 ...

  4. hdu 4506 小明系列故事——师兄帮帮忙【幂取模乱搞】

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=4506 http://acm.hust.edu.cn/vjudge/contest/view.action ...

  5. hdu 4542 小明系列故事——未知剩余系

    小明系列故事——未知剩余系 题意:操作0表示某数有n个约数,操作1为某数有n个非约数:n <= 47777,若是存在小于2^62的数符合,则输出该数,否则若是不存在输出Illegal,若是大于2 ...

  6. HDU 4511 小明系列故事——女友的考验 (AC自动机+DP)

    小明系列故事——女友的考验 Time Limit: 500/200 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total ...

  7. HDU 4511 小明系列故事——女友的考验 (AC自动机 + DP)

    小明系列故事——女友的考验 Time Limit: 500/200 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total ...

  8. hdu 4542 小明系列故事——未知剩余系 反素数 + 打表

    小明系列故事——未知剩余系 Time Limit: 500/200 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Prob ...

  9. hdu4528 小明系列故事——捉迷藏

    Time Limit: 500/200 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Submission(s ...

随机推荐

  1. 51nod 1851俄罗斯方块(trick)

    题目大意:给出一个黑白图,你可以选定一个俄罗斯方块的区域,黑白翻转,问能否变成白图 比较trick的题目, 首先可以想到,奇数个1肯定是无解的,所以考虑偶数个1 可以先讨论n是2的情况 当n为2时,其 ...

  2. redux-saga基本用法

    redux-saga是管理redux异步操作的中间件,redux-saga通过创建sagas将所有异步操作逻辑收集在一个地方集中处理. sagas采用Generator函数来yield Effects ...

  3. bzoj2348

    实在不懂为啥网上的题解都是二分,本人没写二分,wa的很惨结果竟然是printf("%d")的锅,改了就A了 2348: [Baltic 2011]Plagiarism Time L ...

  4. 【BZOJ 4514】[Sdoi2016]数字配对 费用流

    利用spfa流的性质,我直接拆两半,正解分奇偶(妙),而且判断是否整除且质数我用的是暴力根号,整洁判断质数个数差一(其他非spfa流怎么做?) #include <cstdio> #inc ...

  5. mysql5.7.22以上版本忘记密码时这样修改

    1.关闭mysql服务 net stop mysql 2.找到mysql安装路径找到 my.ini 打开在 [mysqld] 下添加 skip-grant-tables 跳过密码校验 3.登陆mysq ...

  6. Fabric证书解析

    一.证书目录解析   通过cryptogen生成所有证书文件后,以peerOrgannizations的第一个组织树org1为例,每个目录和对应文件的功能如下:   ca: 存放组织的根证书和对应的私 ...

  7. ansible 批量修改root密码

    [root@sz_fy_virt_encrypt_33_239 fetch]# cat /opt/passwd.yml - hosts: web vars: path: /home/opsadmin ...

  8. BAT定期删除N天前的文件

    1.直接看脚本在win2008测试可用 ::clean logs @echo off title clean up logs ::delete logs FORFILES /P /C "cm ...

  9. 普通table表格样式及代码大全

     普通table表格样式及代码大全(全)(一) 单实线边框表格 <table style="border-collapse: collapse" borderColor=#0 ...

  10. 正式进入搭建OpenStack

    部署mariadb数据库 控制节点: yum install mariadb mariadb-server python2-PyMySQL -y 编辑: /etc/my.cnf.d/openstack ...