HDOJ 2612 Find a way【最短路 双重bfs】
来源:互联网 发布:淘宝客佣金 购物车 编辑:程序博客网 时间:2024/04/29 04:22
HDOJ 2612 Find a way【最短路 双重bfs】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2612
1、Y与M在KFC约定见面,在此之前不能见面,所以对于Y和M个人来说,M和Y所在的位置是不能走的
2、有一些KFC不能到达,步数为0,这个时候步数非常小但是不是正确答案,最后排除一下
3、步数计数方式:visY[xx][yy] = visY[x][y]+1;(父结点步数+1)
#include<iostream>#include<algorithm>#include<cstdio>#include<cstring>#include<queue>using namespace std;const int MAXL = 205; // 矩阵最大宽度char maze[MAXL][MAXL]; // 迷宫int visY[MAXL][MAXL]; // 访问标记(Y到达坐标点所需步数)int visM[MAXL][MAXL]; // 访问标记(M到达坐标点所需步数)int n, m; // 迷宫的长和宽int dirx[4] = {1, 0, -1, 0};int diry[4] = {0, 1, 0, -1}; // 移动方向(下 右 上 左)typedef struct point{ int x, y;}P; // 坐标queue<P> path; // 路径P Y, M;const int INF = 1000;void Output(){ for(int i = 0; i < n; i++){ for(int j = 0; j < m; j++){ printf("%c", maze[i][j]); } printf("\n"); }}void Input(){ memset(maze, 0, sizeof(maze)); memset(visY, 0, sizeof(visY)); memset(visM, 0, sizeof(visM)); for(int i = 0; i < n; i++){ scanf("%s", &maze[i]); } //Output();}bool go(int xx, int yy){ if(xx >= 0 && xx < n && yy >= 0 && yy < m && (maze[xx][yy] == '.' || maze[xx][yy] == '@')){ return true; } else return false;}void bfs(P start, char flag){ P current, next; int x, y, xx, yy; // x,y是当前坐标 xx,yy是下一步的坐标 path.push(start); while(!path.empty()){ // 如果队列不为空 current = path.front(); x = current.x; y = current.y; for(int i = 0; i < 4; i++){ xx = x + dirx[i]; yy = y + diry[i]; if(flag == 'Y' && visY[xx][yy] == 0 && go(xx, yy)){ visY[xx][yy] = visY[x][y]+1; next.x = xx; next.y = yy; path.push(next); } else if(flag == 'M' && visM[xx][yy] == 0 && go(xx, yy)){ visM[xx][yy] = visM[x][y]+1; next.x = xx; next.y = yy; path.push(next); } } path.pop(); }}//void OutputY(){// for(int i = 0; i < n; i++){// for(int j = 0; j < m; j++){// printf("%d\t", visY[i][j]);// }// printf("\n");// }//}////void OutputM(){// for(int i = 0; i < n; i++){// for(int j = 0; j < m; j++){// printf("%d\t", visM[i][j]);// }// printf("\n");// }//}void Resolve(){ for(int i = 0; i < n; i++){ // 找到Y & M 所在坐标 for(int j = 0; j < m; j++){ if(maze[i][j] == 'Y'){ Y.x = i; Y.y = j; } if(maze[i][j] == 'M'){ M.x = i; M.y = j; } } } bfs(Y, 'Y'); bfs(M, 'M');//// printf("\n");// OutputY();// printf("\n");// OutputM();// printf("\n"); int minStep = INF; for(int i = 0; i < n; i++){ for(int j = 0; j < m; j++){ if(maze[i][j] == '@'){ if((visY[i][j] + visM[i][j] < minStep) && (visY[i][j] + visM[i][j] != 0)) minStep = visY[i][j] + visM[i][j]; } } } printf("%d\n", minStep * 11);}int main(){ while(~scanf("%d%d", &n, &m)){ Input(); Resolve(); } return 0;}
0 0
- HDOJ 2612 Find a way【最短路 双重bfs】
- HDU 2612 Find a way【双重BFS】
- hdu 2612 Find a way(BFS + 最短路)
- hdoj 2612 Find a way (BFS)
- hdoj-2612-find a way-双向BFS
- HDOJ 2612 Find a way【BFS】
- HDOJ 2612 Find a way (双向BFS)
- Find a way (双重BFS)
- HDU 2162 Find a way(双BFS+最短路)
- HDU/HDOJ 2612 Find a way 双向BFS
- HDOJ 题目2612 Find a way(BFS)
- hdoj 2612 Find a way (bfs,双向搜索)
- HDOJ Find a way(两次bfs)
- hdoj 2612 Find a way
- hdoj 2612 Find a way
- HDOJ 2612 Find a way
- HDOJ-2612 Find a way
- Find a way(BFS)
- ThreadPoolExecutor工作原理
- uva1660 最大流
- Activity生命周期(三)——横竖屏切换
- 写论文时,画黑白电路图的技巧
- fzu 1752 A^B mod C(快速乘+快速幂取模)
- HDOJ 2612 Find a way【最短路 双重bfs】
- 滚动条控制视频播放
- OSAL函数分析-osalTimerUpdate
- HDOJ 1213 How Many Tables(并查集)
- IOS LocationManager定位国内偏移,火星坐标(GCJ-02)解决方法
- egit push到gihub里提醒reject non - fast -forward
- 索引
- ConcurrentHashMap缓存
- ListView,图片,两行内容