当前位置:网站首页>走迷宫(BFS)
走迷宫(BFS)
2022-04-21 12:17:00 【4nc414g0n】
走迷宫
题目链接
题目描述:
NowCoder最喜欢游乐场的迷宫游戏,他和小伙伴们比赛谁先走出迷宫。
现在把迷宫的地图给你,你能帮他算出最快走出迷宫需要多少步吗?
输入包含多组数据。
每组数据包含一个10*10,由“#”和“.”组成的迷宫。其中“#”代表墙;“.”代表通路。
入口在第一行第二列;出口在最后一行第九列。
从任意一个“.”点都能一步走到上下左右四个方向的“.”点。
输入:
#.########
#…#
#…#
#…#
#…#
#…#
#…#
#…#
#…#
########.#
输出:
16
思路:
- 简单BFS,入栈,弹栈,以每次size的值count++
代码如下:#include <bits/stdc++.h> using namespace std; vector<vector<int>> arr={ { 1,0},{ -1,0},{ 0,1},{ 0,-1}}; int Shortest(vector<vector<char>>& maze, vector<vector<bool>>& book) { int start_x=0; int start_y=1; int end_x=9; int end_y=8; queue<pair<int, int>> qu; qu.push(make_pair(start_x, start_y)); int count=0; while(!qu.empty()) { int size=qu.size(); while(size--) { int x=qu.front().first; int y=qu.front().second; for(int i=0;i<4;i++) { int tmpx=x+arr[i][0]; int tmpy=y+arr[i][1]; if(tmpx>=0 && tmpx<=9 && tmpy>=0 && tmpy<=9 && maze[tmpx][tmpy]=='.' && book[tmpx][tmpy]==true) { if(tmpx==end_x && tmpy==end_y) return count+1; qu.push(make_pair(tmpx, tmpy)); book[tmpx][tmpy]=false; } } qu.pop(); } count++; } return count; } int main() { while(1) { vector<vector<char>> maze(10,vector<char>(10)); vector<vector<bool>> book(10,vector<bool>(10,true)); char input; for(int i=0;i<10;i++) { for(int j=0;j<10;j++) { if(cin>>input) maze[i][j]=input; else goto flag; } } cout<< Shortest(maze, book) <<endl; } flag:; return 0; }
版权声明
本文为[4nc414g0n]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_41420788/article/details/124205904
边栏推荐
- Scala installation and development environment configuration tutorial
- 分享几款我在高频使用的 Chrome 浏览器插件,每一个都好用到飞起
- Vggnet neural network based on pytorch for flower recognition
- [software test series 8] software project test report
- Web--用户注册界面
- ASM插桩之美
- Base de données, ajouter les données d'un champ d'une autre table à une autre table et ajouter les données dont vous avez besoin lors de l'ajout
- World Reading Day | recommended books list of database classic books (free message at the end of the text)
- Scala安装和开发环境配置教程
- Notepad + + how to copy multiple lines and paste them to the corresponding position
猜你喜欢

electron net 如何发送 post 请求

JD cloud distributed database stardb successfully completed multi-party localization adaptation certification

《深度学习》学习笔记(五)

Intermediary encircles little red book

Vggnet neural network based on pytorch for flower recognition

The non display of markerarray caused by the ROS version problem has been solved

上海疫情中,新消费品牌们的坚守、放弃与救赎

AAAI2022|基于概率软逻辑的无偏时序常识知识推理

Usage Summary of hiredis and rapidjson Libraries

China Resources Yibao is rumored to have an IPO, and nongnongshanquan may usher in an early "old enemy"
随机推荐
S parameter introduction
《深度学习》学习笔记(六)
AcWing 1854. 晋升计数 题解 解方程推式子
idea中往数据库中插入多条数据
Teach you to easily solve CSRF Cross Site Request Forgery Attack
pycharm 跳转到指定行
Study notes of "deep learning" (V)
给定字符串提取姓名(字符串、list、re“零宽断言”)
AAAI2022|基于概率软逻辑的无偏时序常识知识推理
Vggnet neural network based on pytorch for flower recognition
數據庫,把另一張錶某字段數據添加到另一個錶中,同時加入的時候加入自己需要的數據
Three.js学习项目--3D抗美援朝数据可视化
The market share growth rate of Dameng database is leading in the industry, and its profitability has been greatly improved
虚拟货币已然没有市场,为何还能对一些人产生不小的诱惑?
Chris LATTNER, father of llvm: the golden age of compilers
[software test series v] software test application form
基于C#实现文本读取的7种方式
去中心化VC平台CULTDAO发起对BetaMars项目投票,目前投票正在进行中
[data visualization application] draw bivariate mapping map (with R language code)
Study notes of deep learning (6)