4kyu Path Finder #1: can you reach the exit?

题目背景:

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 true if you can reach position [N-1, N-1] or false otherwise.

Empty positions are marked ., Walls are marked W,Start and exit positions are empty in all test cases.

题目分析:

迷宫问题一般就是深搜,广搜,最短路径等经典的图论问题的应用场景,这种题目在OJ中算是基本题目,刷了些题目的看到这种题目就很熟悉了。本道题我用广搜去处理,其实广搜的代码很有套路,写了几遍就知道如何写更舒服了,对于这种题型,其实应用场景还是蛮好分析的,难点在于正确无误地快速码出经典的搜索代码。

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;
};queue<Node> Q;bool 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 true;if ( nx < 0 || nx >= length || ny < 0 || ny >= length ) continue;if ( maze[nx * ( length + 1 ) + ny]  == 'W' ) continue;maze[nx * ( length + 1 ) + ny] = 'W'; // make the tag, have goneNode tmp;tmp.x = nx;tmp.y = ny;Q.push(tmp);}}return false;
}bool path_finder(string maze) {int length = std::floor( std::sqrt( (double) maze.size() ) );while(!Q.empty()) Q.pop();maze[0] = 'W';Node tmp;tmp.x = tmp.y = 0;Q.push(tmp);return BFS(length, maze);
}

4kyu Path Finder #1: can you reach the exit?相关推荐

  1. 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 onl ...

  2. 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 ...

  3. mac必备的文件管理器:Path Finder Mac中文版

    文件管理器当然要选Path Finder Mac中文版,让你管理文件更加轻松,Path Finder for Mac中文版提供了双窗格视图.查看和操作隐藏的文件.文件夹比较和同步.文件夹合并.批量重命 ...

  4. Path Finder for Mac(Mac文件管理工具)

    path finder for mac是Mac平台上一款能够让您完全控制文件的Mac文件管理工具.通过path finder for mac你可以按照自己的要求进行文件的排序,还可以直接显示隐藏文件, ...

  5. Mac文件管理工具:Path Finder

    Path Finder是一款Mac平台上的文件管理和操作工具,提供了比Finder更丰富的功能和更直观的用户界面.它可以帮助用户更高效地浏览.复制.移动.删除和管理文件,以及进行各种高级操作. Pat ...

  6. Path Finder for Mac(系统文件管理器)

    Path Finder 9 Mac中文特别版是一款功能非常强大,十分优秀的系统文件管理器,Path Finder mac能够轻而易举访问系统文件或者网络硬盘.使用起来比系统自带的文件管理器简单方便多了 ...

  7. 文件管理工具:Path Finder mac

    Path Finder mac是Mac上好用的文件管理工具,是一个用户友好的应用程序,可以比较和同步文件夹,查看隐藏文件,使用双窗格和全键盘导航来浏览文件系统,以节省您的时间,端的自定义允许您以许多不 ...

  8. Pocket Gem OA: Path Finder

    1. 有向图 找所有start node到end node之间的路径输入是一个txt 形式如下:A EA : B C D. B : CC : ED : B. 输出一个List<String> ...

  9. jpf java 是什么_[Java Path Finder][JPF学习笔记][6]jpf-symbc的初步使用

    这篇日志继续总结些简单的JPF使用经验: 这里摘录一些设置,进行简单的中文解释: # The following JPF options are usually used for SPF as wel ...

最新文章

  1. NR 5G 测量与寻呼
  2. linux java services_在 Linux 上创建第一个 Service Fabric Java 应用程序
  3. Bootstrap 栅格 样式 组件 插件
  4. Python说文解字_杂谈06
  5. C linux Debug
  6. iOS开发针对对Masonry下的FPS优化讨论
  7. 泛型类 0104 c#
  8. distributed--根索引
  9. java 数组 源码_Java数组转List的三种方式及对比
  10. layout_weight
  11. vue.js开发环境部署
  12. jQuery报错:Uncaught ReferenceError: $ is not defined
  13. 宽带网速如何测试软件,怎样测试网速 多种测试网速方法【推荐】
  14. cmd控制台通过sftp命令下载服务器文件
  15. 计算机word水印在哪,word中如何加入水印的两种方法
  16. JAVAweb开发技术-------(七)POI导入导出excel技术
  17. 大学计算机excel高级筛选,excel高级筛选的使用方法(入门 进阶 高级)
  18. iphonex 测试电池软件,iPhoneX电池容量多大 iPhoneX电池性能评测【详解】
  19. Xilinx SDK 中 的 'No rule to make target ' / '没有规则可以创建...'
  20. 星辰天合联合星环科技完成互认证 共同打造更有生命力的大数据存算解决方案

热门文章

  1. 20以内混合加减法100题_一年级数学20以内加减法混合运算练习题,寒假练习巩固!...
  2. 判定Java源文件名称_JAVA
  3. 【opencv】3.在一个opencv窗口中显示多个视频界面、画箭头、画掉头箭头
  4. 【c++】0.C++笔记
  5. Java 泛型中? super T和? extends T的区别
  6. FreeMarker的使用
  7. Android Studio 单刷《第一行代码》系列 06 —— Fragment 生命周期
  8. 分布式事务之最终一致的Mq实现
  9. 计算机视觉与模式识别国际期刊整理
  10. 干货︱机器学习中防止过拟合的处理方法