4kyu Path Finder #2: shortest path
4kyu Path Finder #2: shortest path
题目背景:
Task
You are at position [0, 0]
in maze NxN and you can only move in one of the four cardinal directions (i.e. North, East, South, West). Return the minimal number of steps to exit position [N-1, N-1]
if it is possible to reach the exit from the starting position. Otherwise, return false
in JavaScript/Python and -1
in C++/C#/Java.
Empty positions are marked .
. Walls are marked W
. Start and exit positions are guaranteed to be empty in all test cases.
题目分析:
迷宫问题一般就是深搜,广搜,最短路径等经典的图论问题的应用场景,这种题目在OJ中算是基本题目,刷了些相关题目后看到这种题目就很熟悉了。本道题我用广搜去处理,从起点一层一层地往外剥,对于走过的格点都打上tag,每一个格点再附上到达这个格点的步数属性,这样广搜到终点的话就可以获取到步数。
AC代码:
#include <iostream>
#include <string>
#include <cmath>
#include <queue>using namespace std;int go[4][2] = {0, 1,0, -1,1, 0,-1, 0
};struct Node {int x, y;int steps;
};queue<Node> Q;int BFS(int length, string maze) {while( !Q.empty() ) {Node now = Q.front();Q.pop();for ( int i = 0; i < 4; i++ ) {int nx = now.x + go[i][0];int ny = now.y + go[i][1];if ( nx == length - 1 && ny == length - 1 ) return ++now.steps;if ( nx < 0 || nx >= length || ny < 0 || ny >= length ) continue;if ( maze[nx * ( length + 1 ) + ny] == 'W' ) continue;maze[nx * ( length + 1 ) + ny] = 'W';Node tmp;tmp.x = nx;tmp.y = ny;tmp.steps = now.steps + 1;Q.push(tmp);}}return -1;
}int path_finder(string maze) {int length = std::floor( std::sqrt( (double) maze.size() ) );if ( length == 1 ) return 0;while(!Q.empty()) Q.pop();maze[0] = 'W';Node tmp;tmp.x = tmp.y = tmp.steps = 0;Q.push(tmp);return BFS(length, maze);
}
4kyu Path Finder #2: shortest path相关推荐
- AOJ GRL_1_B: Shortest Path - Single Source Shortest Path (Negative Edges) (Bellman-Frod算法求负圈和单源最短路径)
题目链接: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_1_B Single Source Shortest Path ( ...
- 程序员的算法课(19)-常用的图算法:最短路径(Shortest Path)
一.最短路径问题 [google笔试题]一个环形公路,给出相邻两点的距离(一个数组),求任意两点的最短距离,要求空间复杂度不超过O(N). 如果从有向图中某一顶点(称为源点)到达另一顶点(称为终点)的 ...
- 4kyu Path Finder #1: can you reach the exit?
4kyu Path Finder #1: can you reach the exit? 题目背景: Task You are at position [0, 0] in maze NxN and y ...
- 3kyu Path Finder #3: the Alpinist
3kyu Path Finder #3: the Alpinist 题目背景: Task You are at start location [0, 0] in mountain area of Nx ...
- P - The Shortest Path in Nya Graph HDU - 4725
P - The Shortest Path in Nya Graph HDU - 4725 最短路 不是 每两个点之间按层数设置边权 + 额外边权 TLE 是 相邻两层之间设置边权 + 额外边权 需注 ...
- [CF843D]Dynamic Shortest Path
[CF843D]Dynamic Shortest Path 题目大意: 给定一个带权有向图,包含\(n(n\le10^5)\)个点和\(m(m\le10^5)\)条边.共\(q(q\le2000)\) ...
- zoj 2760 How Many Shortest Path 最大流
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1760 Given a weighted directed graph ...
- OSPF(Open Shortest Path First开放式最短路径优先)
**协议** OSPF(Open Shortest Path First开放式最短路径优先)是一个内部网关协议(Interior Gateway Protocol,简称IGP),用于在单一自治系统(a ...
- AOJ GRL_1_C: All Pairs Shortest Path (Floyd-Warshall算法求任意两点间的最短路径)(Bellman-Ford算法判断负圈)
题目链接:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_1_C All Pairs Shortest Path Input ...
最新文章
- c语言遍历字符串数组的方法
- 廖雪峰python教程百度云-廖雪峰Python教程的配套视频教程,全套完整版!
- 一张倾斜图片进行矫正 c++_专业性文章:10分钟矫正骨盆前倾
- 基础算法 —— 调度问题 —— 多机并行调度问题
- 将HaneWin DHCP 注册为服务
- python 趋势跟踪算法_DualThrust区间突破策略Python版
- 灰鸽子病毒——网络神偷之后应用最广的反弹端口***
- Python——简化表达
- 蓝牙(BLE)自动配对
- js中的Symbol数据类型
- DDR Layout使用技巧
- Oracle表中序列号的添加
- Java篇 - 最全BigInteger和BigDecimal实战
- vs2019添加文件夹到到现有项目
- Android使用MediaPlayer播放流媒体,支持远程以及本地流媒体,一行代码实现
- 静态HTML明星主页 HTML+CSS 周杰伦明星页面(学生课程设计网页设计制作大作业)
- 时间序列模型相关说明和模型介绍
- left join最多几张表_Spark中的join策略
- 十分钟全面了解es6及其发展历史
- MT7628学习笔记(12)——GPIO操作,注册LED驱动