bfs hrbust 2188
来源:互联网 发布:淘宝快递投诉怎么撤销 编辑:程序博客网 时间:2024/06/05 14:39
星际旅行
Time Limit: 1000 MS
Memory Limit: 32768 K
Total Submit: 112(44 users)
Total Accepted: 55(41 users)
Rating:
Special Judge: No
Description
小z在星际旅行中,他想从当前起始点到达目的地,他可以上下左右的移动,并且每移动一格需要花费一单位时间,
星际中有许多危险的地方不可以走,还有许多的黑洞,你可以不花费任何时间从一个黑洞瞬间到达其它任何一个黑洞,
请你帮助小z计算从当前起始点到目的地最少需要多少单位时间。
Input
第一行是一个整数T,代表T组测试数据。
对于每组测试数据,首先是两个整数n,m代表星际地图(1<=n<=100, 1<=m<=100)
接下来是一个n*m的地图。
'O' 代表黑洞。
'#' 代表不可以走的地方。
'.' 代表普通的空间。
'S' 代表起始点,有且只有一个。
'E' 代表目的地,有且只有一个。
Output
对于每组测试数据,如果小z可以到达目的地,则输出需要的最少时间,否则输出"impossible"。
Sample Input
2
3 4
SO..
....
..OE
3 3
#S#
###
E##
Sample Output
2
impossible
#include<stdio.h>#include<queue>#include<string.h>using namespace std;char map[105][105];int n,m;int turn[105][105];int dx[4] = {0,0,1,-1};int dy[4] = {1,-1,0,0};struct node{int x,y,step;};node u,v;int judge(int x, int y){if(x>=n||x<0||y>=m||y<0||map[x][y] == '#')return 0;return 1;}int bfs(int x,int y, int x0,int y0){queue<node>s;u.x = x;u.y = y;u.step = 0;s.push(u);while (!s.empty()){u = s.front();s.pop();if (u.x == x0 && u.y == y0) return u.step;for (int i = 0; i < 4; i++){v.x = u.x + dx[i];v.y = u.y + dy[i];v.step = u.step + 1;if(judge(v.x,v.y)){if(map[v.x][v.y] == 'O'){for(int j = 0; j < n;j ++)for(int k = 0; k < m; k++)if(map[j][k] == 'O') {v.x = j;v.y = k;if(judge(v.x,v.y))s.push(v);if (u.x == x0 && u.y == y0) return u.step;}map[v.x][v.y] = '#';}else{map[v.x][v.y] = '#';s.push(v);}}}}return -1;}int main(){int T;scanf("%d",&T);while(T--){memset(turn,0,sizeof turn);scanf("%d%d",&n,&m);for(int i = 0; i < n;i ++)scanf("%s",map[i]);int startx,starty;int endx,endy;int count = 0;for(int i = 0; i < n; i++){for(int j = 0; j < m; j++){if(map[i][j] == 'S') startx = i,starty = j;if(map[i][j] == 'E') endx = i, endy = j;if(map[i][j] == '#') turn[i][j] = 1;}}int ans = bfs(startx,starty,endx,endy);if(ans == -1)printf("impossible\n");elseprintf("%d\n",ans);}}
阅读全文
0 0
- bfs hrbust 2188
- hrbust 2159【BFS水题】
- Hrbust 1143 (bfs)
- bfs\dfs hrbust 1143
- bfs hrbust 2074
- bfs hrbust 1948
- hrbust 哈理工oj 2188 星际旅行【BFS+传送阵】
- hrbust 院赛 bfs 骑士相遇
- HRBUST 1012 Catch That Cow (BFS)
- bfs 思路 hrbust院赛初赛D
- hrbust/哈理工oj 1617 回家【BFS+BFS】
- HRBUST
- HRBUST
- HRBUST
- HRBUST
- HRBUST
- HRBUST
- HRBUST
- caffe之python接口实战 :pascal-multilabel-with-datalayer 官方教程源码解析
- C#学习之旅之属性和方法
- 洛谷 P2484 [SDOI2011]打地鼠
- vue.js 2.x整理
- 静态导入和自动装箱和自动拆箱
- bfs hrbust 2188
- Linux操作系统启动流程
- BZOJ 3110 [Zjoi2013]K大数查询(整体二分+树状数组)
- 使用dd测试硬盘
- 300万知乎多标签文本分类任务经验分享(附源码)
- centos php编译安装
- Spark SQL & Spark Hive编程开发, 并和Hive执行效率对比
- UI_布局之线性布局-动态生成与LayoutInflater
- jdbc报错 :com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure