UESTCoj- 149- 解救小Q (BFS
来源:互联网 发布:淘宝助手上传图片失败 编辑:程序博客网 时间:2024/05/16 15:28
解救小Q
Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others)
Submit Status
小Q被邪恶的大魔王困在了迷宫里,love8909决定去解救她。 迷宫里面有一些陷阱,一旦走到陷阱里,就会被困身亡:(,迷宫 里还有一些古老的传送阵,一旦走到传送阵上,会强制被传送到 传送阵的另一头。
现在请你帮助love8909算一算,他至少需要走多少步才能解 救到小Q?
Input
第一行为一个整数TT,表示测试数据组数。
每组测试数据第一行为两个整数NN,MM,(1≤N,M≤501≤N,M≤50)表示 迷宫的长和宽。
接下来有NN行,每行MM个字符,是迷宫的具体描述。
.表示安全的位置
表示陷阱,
Q表示小Q的位置
L表示love8909所在位置,
数据保证love8909只有一个,数据也保证小Q只有一个。
小写字母a-z表示分别表示不同的传送阵,数据保证传送阵 两两配对。
Output
每组数据输出一行,解救小Q所需的最少步数,如果无论如何都 无法救小Q,输出-1。
Sample input and output
Sample Input Sample Output
25 5....L.###.b#b#a##.##...Qa35 5....L.###..#.#.##.##...Q.-1
主要是对传送门查找 还是挺不好写的了
#include <cstdio>#include <cstring> #include <queue>#include <algorithm>using namespace std;const int N = 60;char map[N][N];bool vis[N][N]; int dir[4][2] ={1,0,-1,0,0,1,0,-1};int n, m;int sx,ex,sy,ey,gx,gy;struct node { int x, y; int step; };bool judge(int x,int y){ if(x<0||y<0||x>=n||y>=m||vis[x][y]||map[x][y]=='#') return false; return true;}void find(char ch, int a, int b){ for(int i = 0;i < n; i++) { for(int j = 0;j < m; j++) { if(map[i][j]==ch&&(i!=a||j!=b)) { gx = i; gy = j; return ; } } }}int bfs(){ queue<node>q; node a; a.x = sx, a.y = sy, a.step = 0; q.push(a); vis[sx][sy] = true; while(!q.empty()) { node b = q.front(); q.pop(); if(b.x==ex && b.y==ey) return b.step; for(int i = 0;i < 4; i++) { int xx = b.x + dir[i][0]; int yy = b.y + dir[i][1]; if(judge(xx,yy)) { vis[xx][yy] = true; if(map[xx][yy]>='a' && map[xx][yy]<='z') { find(map[xx][yy],xx,yy); xx = gx; yy = gy; } node c; c.x = xx; c.y = yy; c.step = b.step + 1; q.push(c); } } } return -1;}int main(){ int T; scanf("%d",&T); while(T--) { memset(vis,false,sizeof(vis)); scanf("%d%d",&n,&m); for(int i = 0;i < n; i++) scanf("%s",map[i]); for(int i = 0;i < n; i++) { for(int j = 0;j < m; j++) { if(map[i][j]=='L') { sx = i; sy = j; } if(map[i][j]=='Q') { ex = i; ey = j; } } } int zz = bfs(); printf("%d\n",zz); }return 0;}
1 0
- UESTCoj- 149- 解救小Q (BFS
- 解救小Q(bfs)
- 解救小Q(BFS)
- CDOJ-#149 解救小Q(BFS)
- UESTC 149 -- 解救小Q (bfs)
- CDOJ 149 解救小Q 搜索 BFS
- 解救小Q(bfs)
- UESTC149 解救小Q(bfs)
- UESTC - 149 解救小Q
- CDOJ(UESTC) 149 解救小Q(bfs中涉及传送带的问题)
- UESTC 149——解救小Q
- CDOJ_149 解救小Q
- CDOJ 解救小Q
- UESTC 解救小Q
- 解救小Q
- 解救小Q
- 解救小Q UESTC
- bfs解救小哈
- wav pcm format初探
- 三维扫描仪[8]——如何设计一台云台式扫描仪(机械结构)
- Java学习----day10——(1)API-集合
- Http header详解
- mysql数据库常用操作命令集合
- UESTCoj- 149- 解救小Q (BFS
- JS 模拟鼠标点击
- 线索二叉树(Threaded BinaryTree)
- 使用 Gson 解析 Json
- win7下更改为achi硬盘模式
- maven 亲测可用国内镜像 阿里云
- LeetCode-标签为 stack 225. Implement Stack using Queues
- java基础各博主博客连接
- android Monkey测试源码分析之一