起点 S到终点T的步数
来源:互联网 发布:网络兼职怎么找 编辑:程序博客网 时间:2024/06/10 04:41
起点 S到终点T的步数
输入:
5 5......*.*..*S*..***....T*2 2 4 3
输出
11
#include <iostream>#include <cstdio>#include <cstdlib>#include <queue>#include <cstring>using namespace std;const int maxn=100;typedef struct{ int x,y; //坐标 int step; //step 为从起点S到达该位置的最小步数 }Node;Node S,T,node; //S为起点,T为终点,node为临时结点 int n,m;char maze[maxn][maxn];bool inq[maxn][maxn];int X[4]={0,0,1,-1};int Y[4]={1,-1,0,0};//检测坐标是否有效bool test(int x,int y){ //越界,false if(x>=n || x<0 || y>=m || y<0 ) { return false; } //碰墙,false;已经 入过队,false if( maze[x][y]=='*' || inq[x][y]==true ) { return false; } return true;} int BFS(){ //定义队列 queue<Node> queue; //将起点S 入队列 queue.push(S); while(!queue.empty()) { Node top=queue.front(); queue.pop(); if( top.x==T.x && top.y==T.y ) { return top.step; } for(int i=0;i<4;i++) { int newx=top.x+X[i]; int newy=top.y+Y[i]; if(test(newx,newy) ) { node.x=newx; node.y=newy; node.step=top.step+1; queue.push(node); inq[newx][newy]=true; } } } return -1;}int main(){ scanf("%d %d",&n,&m); for(int i=0;i<n;i++) { getchar(); //过滤掉每行之后的换行符 for(int j=0;j<m;j++) { maze[i][j]=getchar(); } maze[i][m+1]='\0'; } //初始化起点和终点的坐标 scanf("%d%d%d%d",&S.x,&S.y,&T.x,&T.y); S.step=0; //初始化起点的层数为0,即S到S的最少步数是0 printf("%d\n",BFS()); return 0;}
阅读全文
0 0
- 起点 S到终点T的步数
- 期盼中求出起点到终点的最小步数
- (hdu step 4.2.4)A strange lift(求从起点到终点的最小步数,限制条件是:在一维的情况下)
- (hdu step 4.2.3)Rescue(求从起点到终点的最少步数,遇到特殊节点需要耗时2秒)
- leetcode之网格从起点到终点的路径数之滚动数组
- (hdu step 4.2.3)Knight Moves(求从起点是否能够到达终点的最小步数)
- 校验同一时间只能存在一个起点到终点的数据,如何判断(时间,起点,终点)
- 【Codeforces Round 354 (Div 2)D】【迷宫搜索BFS】Theseus and labyrinth 门门互达 可做旋转操作 最少步数起点到终点
- 开源,从起点到终点
- [LeetCode] Unique Paths 起点与终点确定的路径数
- Unique Paths 寻找从起点到终点的路径个数
- 0001-人生就是一场从起点到终点的历程
- 迷宫的最短路径(BFS,起点到终点)
- Jump Game II 最小到达终点的步数
- Jump Game II 到达终点的最小步数 模拟
- 有向图起点s到目标点t的所有路径
- 终点-起点
- 终点 起点
- npm 权限问题
- Shell脚本.bat启动报错看不到错误信息
- LINTCODE93:平衡二叉树
- java并发-竞态条件与临界区(3)
- Eclipse插件汇总
- 起点 S到终点T的步数
- Full GC排查
- C语言基础与提高3——指针
- 机器学习笔记之R语言基础篇3(概率分布1)
- 日期转化
- 收款吧-shouk8 个人微信支付宝网站收款、即时入账、即充宝--收支宝 免签约收款即时到帐辅助系统
- Elasticsearch 安装ik分词器
- 【云计算】mapreduce的理解
- C++学习之友元函数(二)