链接:https://www.nowcoder.com/acm/contest/105/F
来源:牛客网

有q个单向传送阵,每个传送阵各有一个入口和一个出口,入口和出口都在迷宫的格子里,当走到或被传送到一个有传送阵入口的格子时,小明可以选择是否开启传送阵。如果开启传送阵,小明就会被传送到出口对应的格子里,这个过程会花费3秒;如果不开启传送阵,将不会发生任何事情,小明可以继续向上下左右四个方向移动。

一个格子可能既有多个入口,又有多个出口,小明可以选择任意一个入口开启传送阵。使用传送阵是非常危险的,因为有的传送阵的出口在陷阱里,如果小明使用这样的传送阵,那他就会死亡。也有一些传送阵的入口在陷阱里,这样的传送阵是没有用的,因为小明不能活着进入。请告诉小明活着到达目的地的最短时间。

输入描述:

  1. 有多组数据。对于每组数据:
    第一行有三个整数n,m,q(2 n,m300,0 q 1000)。
    接下来是一个nm列的矩阵,表示迷宫。
    最后q行,每行四个整数

  1. 表示一个传送阵的入口在x

1

  1. y

1

  1. 列,出口在x

2

  1. y

2

  1. 列。
  1.  

输出描述:

  1. 如果小明能够活着到达目的地,则输出最短时间,否则输出-1
示例1

输入

  1. 5 5 1
  2. ..S..
  3. .....
  4. .###.
  5. .....
  6. ..T..
  7. 1 2 3 3
  8. 5 5 1
  9. ..S..
  10. .....
  11. .###.
  12. .....
  13. ..T..
  14. 3 3 1 2
  15. 5 5 1
  16. S.#..
  17. ..#..
  18. ###..
  19. .....
  20. ....T
  21. 0 1 0 2
  22. 4 4 2
  23. S#.T
  24. .#.#
  25. .#.#
  26. .#.#
  27. 0 0 0 3
  28. 2 0 2 2

输出

  1. 6
  2. 8
  3. -1
  4. 3
    bfs,注意,可能通过不止一个传送阵
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <cstring>
  4. #include <cstdio>
  5. #include <vector>
  6. #include <queue>
  7. #include <stack>
  8. #include <cstdlib>
  9. #include <iomanip>
  10. #include <cmath>
  11. #include <cassert>
  12. #include <ctime>
  13. #include <map>
  14. #include <set>
  15. using namespace std;
  16. #pragma comment(linker, "/stck:1024000000,1024000000")
  17. #define lowbit(x) (x&(-x))
  18. #define max(x,y) (x>=y?x:y)
  19. #define min(x,y) (x<=y?x:y)
  20. #define MAX 100000000000000000
  21. #define MOD 1000000007
  22. #define pi acos(-1.0)
  23. #define ei exp(1)
  24. #define PI 3.1415926535897932384626433832
  25. #define ios() ios::sync_with_stdio(true)
  26. #define INF 0x3f3f3f3f
  27. #define mem(a) ((a,0,sizeof(a)))
  28. typedef long long ll;
  29. struct node
  30. {
  31. int x,y,step;
  32. node(int x=,int y=,int t=):x(x),y(y),step(step){};
  33. bool operator<(const node a) const
  34. {
  35. return a.step<step;
  36. }
  37. }ans,pos;
  38. int dir[][]={{,},{,},{,-},{-,}};
  39. int n,m,k,xs,ys;
  40. int dis[][];
  41. char a[][];
  42. int xa,xb,ya,yb;
  43. vector<node>vis[][];
  44. int bfs(int u,int v)
  45. {
  46. priority_queue<node>q;
  47. memset(dis,INF,sizeof(dis));
  48. ans.x=u;ans.y=v;
  49. ans.step=;
  50. dis[u][v]=;
  51. q.push(ans);
  52. while(!q.empty())
  53. {
  54. pos=q.top();
  55. q.pop();
  56. if(a[pos.x][pos.y]=='T') return pos.step;
  57. for(int i=;i<vis[pos.x][pos.y].size();i++)
  58. {
  59. ans=vis[pos.x][pos.y][i];
  60. if(dis[ans.x][ans.y]>pos.step+)
  61. {
  62. ans.step=pos.step+;
  63. dis[ans.x][ans.y]=pos.step+;
  64. q.push(ans);
  65. }
  66. }
  67. for(int i=;i<;i++)
  68. {
  69. ans.x=pos.x+dir[i][];
  70. ans.y=pos.y+dir[i][];
  71. ans.step=pos.step+;
  72. if(ans.x>= && ans.x<n && ans.y>= && ans.y<m && a[ans.x][ans.y]!='#' && dis[ans.x][ans.y]>ans.step)
  73. {
  74. dis[ans.x][ans.y]=pos.step+;
  75. q.push(ans);
  76. }
  77. }
  78. }
  79. return -;
  80. }
  81. int main()
  82. {
  83. while(~scanf("%d%d%d",&n,&m,&k))
  84. {
  85. for(int i=;i<n;i++)
  86. {
  87. scanf("%s",&a[i]);
  88. for(int j=;j<m;j++)
  89. {
  90. vis[i][j].clear();
  91. if(a[i][j]=='S') xs=i,ys=j;
  92. }
  93. }
  94. for(int i=;i<k;i++)
  95. {
  96. scanf("%d%d%d%d",&xa,&ya,&xb,&yb);
  97. if(a[xa][ya]!='#' && a[xb][yb]!='#')
  98. vis[xa][ya].push_back(node(xb,yb));
  99. }
  100. int cnt=bfs(xs,ys);
  101. printf("%d\n",cnt);
  102. }
  103. return ;
  104. }#include <iostream>
  105. #include <algorithm>
  106. #include <cstring>
  107. #include <cstdio>
  108. #include <vector>
  109. #include <queue>
  110. #include <stack>
  111. #include <cstdlib>
  112. #include <iomanip>
  113. #include <cmath>
  114. #include <cassert>
  115. #include <ctime>
  116. #include <map>
  117. #include <set>
  118. using namespace std;
  119. #pragma comment(linker, "/stck:1024000000,1024000000")
  120. #define lowbit(x) (x&(-x))
  121. #define max(x,y) (x>=y?x:y)
  122. #define min(x,y) (x<=y?x:y)
  123. #define MAX 100000000000000000
  124. #define MOD 1000000007
  125. #define pi acos(-1.0)
  126. #define ei exp(1)
  127. #define PI 3.1415926535897932384626433832
  128. #define ios() ios::sync_with_stdio(true)
  129. #define INF 0x3f3f3f3f
  130. #define mem(a) ((a,0,sizeof(a)))
  131. typedef long long ll;
  132. struct node
  133. {
  134. int x,y,step;
  135. node(int x=,int y=,int t=):x(x),y(y),step(step){};
  136. bool operator<(const node a) const
  137. {
  138. return a.step<step;
  139. }
  140. }ans,pos;
  141. int dir[][]={{,},{,},{,-},{-,}};
  142. int n,m,k,xs,ys;
  143. int dis[][];
  144. char a[][];
  145. int xa,xb,ya,yb;
  146. vector<node>vis[][];
  147. int bfs(int u,int v)
  148. {
  149. priority_queue<node>q;
  150. memset(dis,INF,sizeof(dis));
  151. ans.x=u;ans.y=v;
  152. ans.step=;
  153. dis[u][v]=;
  154. q.push(ans);
  155. while(!q.empty())
  156. {
  157. pos=q.top();
  158. q.pop();
  159. if(a[pos.x][pos.y]=='T') return pos.step;
  160. for(int i=;i<vis[pos.x][pos.y].size();i++)
  161. {
  162. ans=vis[pos.x][pos.y][i];
  163. if(dis[ans.x][ans.y]>pos.step+)
  164. {
  165. ans.step=pos.step+;
  166. dis[ans.x][ans.y]=pos.step+;
  167. q.push(ans);
  168. }
  169. }
  170. for(int i=;i<;i++)
  171. {
  172. ans.x=pos.x+dir[i][];
  173. ans.y=pos.y+dir[i][];
  174. ans.step=pos.step+;
  175. if(ans.x>= && ans.x<n && ans.y>= && ans.y<m && a[ans.x][ans.y]!='#' && dis[ans.x][ans.y]>ans.step)
  176. {
  177. dis[ans.x][ans.y]=pos.step+;
  178. q.push(ans);
  179. }
  180. }
  181. }
  182. return -;
  183. }
  184. int main()
  185. {
  186. while(~scanf("%d%d%d",&n,&m,&k))
  187. {
  188. for(int i=;i<n;i++)
  189. {
  190. scanf("%s",&a[i]);
  191. for(int j=;j<m;j++)
  192. {
  193. vis[i][j].clear();
  194. if(a[i][j]=='S') xs=i,ys=j;
  195. }
  196. }
  197. for(int i=;i<k;i++)
  198. {
  199. scanf("%d%d%d%d",&xa,&ya,&xb,&yb);
  200. if(a[xa][ya]!='#' && a[xb][yb]!='#')
  201. vis[xa][ya].push_back(node(xb,yb));
  202. }
  203. int cnt=bfs(xs,ys);
  204. printf("%d\n",cnt);
  205. }
  206. return ;
  207. }

2018年湘潭大学程序设计竞赛 maze(bfs)的更多相关文章

  1. 2018年湘潭大学程序设计竞赛 F - maze

    把点抽出来 跑个最短路就好啦. #include<bits/stdc++.h> #define LL long long #define pii pair<int,int> # ...

  2. 2018年湘潭大学程序设计竞赛G又见斐波那契

    链接:https://www.nowcoder.com/acm/contest/105/G来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536 ...

  3. 牛客网-2018年湘潭大学程序设计竞赛-F

    题目链接:https://www.nowcoder.com/acm/contest/105/F 解题思路:这道题第一眼直接思路就是搜索,但想了半天没想到有什么好办法搜,然后就转成最短路写了, 因为多入 ...

  4. 2018年湘潭大学程序设计竞赛 H统计颜色

    链接:https://www.nowcoder.com/acm/contest/105/H来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536 ...

  5. 2018年湘潭大学程序设计竞赛 G- 又见斐波那契

    推一推矩阵直接快速幂. #include<bits/stdc++.h> #define LL long long #define pii pair<int,int> #defi ...

  6. 2018年湘潭大学程序设计竞赛 E 吃货

    题目描述 作为一个标准的吃货,mostshy又打算去联建商业街觅食了.混迹于商业街已久,mostshy已经知道了商业街的所有美食与其价格,而且他给每种美食都赋予了一个美味度,美味度越高表示他越喜爱这种 ...

  7. 2018年湘潭大学程序设计竞赛G又见斐波那契(矩阵快速幂)

    题意 题目链接 Sol 直接矩阵快速幂 推出来的矩阵应该长这样 \begin{equation*}\begin{bmatrix}1&1&1&1&1&1\\1 & ...

  8. 2018年湘潭大学程序设计竞赛 Fibonacci进制

    Fibonacci数是非常有名的一个数列,它的公式为 f(n)=f(n-1)+f(n-2),f(0)=1,f(1)=2.  我们可以把任意一个数x表示成若干不相同的Fibonacci数的和, 比如说1 ...

  9. 2018中国大学生程序设计竞赛 - 网络选拔赛 1001 - Buy and Resell 【优先队列维护最小堆+贪心】

    题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6438 Buy and Resell Time Limit: 2000/1000 MS (Java/O ...

随机推荐

  1. bzoj4519: [Cqoi2016]不同的最小割(分治最小割)

    4519: [Cqoi2016]不同的最小割 题目:传送门 题解: 同BZOJ 2229 基本一样的题目啊,就最后用set记录一下就ok 代码: #include<cstdio> #inc ...

  2. java中的输入输出<1>

    java中的输入输出基础(1) java中的IO支持通过java.io包下的类和接口来支持.在java.io包下主要包括输入.输出两种io流,每种输入.输出流又分为字节流和字符流. 字节流就是以字节为 ...

  3. 洛谷P1339 [USACO09OCT]热浪Heat Wave(最短路)

    题目描述 The good folks in Texas are having a heatwave this summer. Their Texas Longhorn cows make for g ...

  4. PostgreSQL 数据库性能调优的注意点

    PostgreSQL提供了一些性能调优的功能.主要有如下几个方面.1.使用EXPLAIN   EXPLAIN命令可以查看执行计划,这个方法是我们最主要的调试工具. 2.及时更新执行计划中使用的统计信息 ...

  5. IE6 css fixed

    .fixed-top{position:fixed;bottom:auto;top:0px;} .fixed-bottom{position:fixed;bottom:0px;top:auto;} . ...

  6. hdu 6082 - 完全背包,打表。

    2017-08-06 15:02:50 Accepted 1003 187 MS 2168 K G++ redips 对背包有了进一步的认识 ----------------------------- ...

  7. Matlab 从入门到精通 Chapter11 文件读取I/O

    11.1 工作空间数据读取 将工作空间的变量保存为文件,可以使用save命令.  save('filename') 将文件保存在当前目录下,文件名为filename.mat save('filenam ...

  8. LightOJ-1074 Extended Traffic 最短路问题 注意连通性

    题目链接:https://cn.vjudge.net/problem/LightOJ-1074 题意 给一图 求最短路 若最短路<3或没有最短路,则输出'?' 思路 首先注意到可能存在负环,所以 ...

  9. [UVa10188]Automated Judge Script

    题目大意:叫你写一个判断答案的系统. 解题思路:模拟即可.AC条件为,答案条数相等,所有字符相等.PE条件为,答案条数可能不等,所有数字字符相等.其他为WA. UVa现在的C++已经不支持gets了, ...

  10. 使用systemctl自定义系统服务

    1.创建系统服务文件,格式如下: [Unit] Description=httpd After=network.target [Service] Type=forking ExecStart=/usr ...