当前位置: 首页 > news >正文

广度优先搜索BFS(广搜)复习(c++)

走出迷宫的最少步数 

题目

 代码

#include <bits/stdc++.h>
using namespace std;
struct xiao_che_shi
{int x,y,v;xiao_che_shi(){};xiao_che_shi(int a,int b,int c){x = a;y = b;v = c;}
};
xiao_che_shi che_shi[1700];
int head,tail;
int n,m;
char a[50][50];
int b[50][50];
int che_shi_x[] = {1,0,-1,0};
int che_shi_y[] = {0,1,0,-1};
int main()
{cin>>n>>m;for(int i = 0;i<n;i++){for(int j = 0;j<m;j++){cin>>a[i][j];}}che_shi[++tail] = {0,0,1};b[0][0] = 1;while(head<tail){head++;for(int i = 0;i<4;i++){int ma_che_shi_x = che_shi[head].x+che_shi_x[i];int ma_che_shi_y = che_shi[head].y+che_shi_y[i];if(ma_che_shi_x>=0&&ma_che_shi_x<n&&ma_che_shi_y>=0&&ma_che_shi_y<m&&a[ma_che_shi_x][ma_che_shi_y]=='.'&&b[ma_che_shi_x][ma_che_shi_y]==0){che_shi[++tail] = {ma_che_shi_x,ma_che_shi_y,che_shi[head].v+1};b[ma_che_shi_x][ma_che_shi_y] = 1;if(che_shi[tail].x==n-1&&che_shi[tail].y==m-1){cout<<che_shi[tail].v;return 0;}}}}return 0;
}


 

走出迷宫的最少步数2

题目

代码

#include <bits/stdc++.h>
using namespace std;
struct xiao_che_shi
{int x,y,v;xiao_che_shi(){};xiao_che_shi(int a,int b,int c){x = a;y = b;v = c;}
};
xiao_che_shi che_shi[1700];
int head,tail;
int n,m;
char a[50][50];
int b[50][50];
int xx1,yy1,xx,yy;
int che_shi_x[] = {1,0,-1,0};
int che_shi_y[] = {0,1,0,-1};
int main()
{cin>>n>>m;for(int i = 0;i<n;i++){for(int j = 0;j<m;j++){cin>>a[i][j];if(a[i][j]=='S') xx1 = i,yy1 = j;if(a[i][j]=='T') xx = i,yy = j;}}che_shi[++tail] = {xx1,yy1,0};b[xx1][yy1] = 1;while(head<tail){head++;for(int i = 0;i<4;i++){int ma_che_shi_x = che_shi[head].x+che_shi_x[i];int ma_che_shi_y = che_shi[head].y+che_shi_y[i];if(ma_che_shi_x>=0&&ma_che_shi_x<n&&ma_che_shi_y>=0&&ma_che_shi_y<m&&a[ma_che_shi_x][ma_che_shi_y]!='#'&&b[ma_che_shi_x][ma_che_shi_y]==0){che_shi[++tail] = {ma_che_shi_x,ma_che_shi_y,che_shi[head].v+1};b[ma_che_shi_x][ma_che_shi_y] = 1;if(che_shi[tail].x==xx&&che_shi[tail].y==yy){cout<<che_shi[tail].v;return 0;}}}}return 0;
}

 

骑士巡游

题目

代码

#include <bits/stdc++.h>
using namespace std;
struct xiao_che_shi
{int x,y,v;xiao_che_shi(){};xiao_che_shi(int a,int b,int c){x = a;y = b;v = c;}
};
xiao_che_shi che_shi[1700];
int head,tail;
int n,m;
int b[50][50];
int xx1,yy1,xx,yy;
int che_shi_x[] = {2,2,1,1,-1,-1,-2,-2};
int che_shi_y[] = {1,-1,2,-2,-2,2,-1,1};
int main()
{cin>>n>>m>>xx1>>yy1>>xx>>yy;xx1--;yy1--;xx--;yy--;che_shi[++tail] = {xx1,yy1,0};b[xx1][yy1] = 1;while(head<tail){head++;for(int i = 0;i<8;i++){int ma_che_shi_x = che_shi[head].x+che_shi_x[i];int ma_che_shi_y = che_shi[head].y+che_shi_y[i];if(ma_che_shi_x>=0&&ma_che_shi_x<n&&ma_che_shi_y>=0&&ma_che_shi_y<m&&b[ma_che_shi_x][ma_che_shi_y]==0){che_shi[++tail] = {ma_che_shi_x,ma_che_shi_y,che_shi[head].v+1};b[ma_che_shi_x][ma_che_shi_y] = 1;if(che_shi[tail].x==xx&&che_shi[tail].y==yy){cout<<che_shi[tail].v;return 0;}}}}return 0;
}

 

http://www.xdnf.cn/news/1073557.html

相关文章:

  • 深入理解Mysql索引底层数据结构和算法
  • NeRF-Lidar实景重建:大疆Mavic 4 Pro低成本建模方案(2025实战指南)
  • H3C-路由器DHCPV6V4配置标准
  • C++基础(FreeRDP编译)
  • SRS流媒体服务器之本地测试rtc推流bug
  • Python 数据分析:numpy,抽提,整数数组索引。听故事学知识点怎么这么容易?
  • 第八讲——一元函数积分学的概念与性质
  • 【编译原理】期末
  • 设备树引入
  • 【Java--SQL】${}与#{}区别和危害
  • 【EDA软件】【联合Modelsim 同步FIFO仿真】
  • git 挑选:git cherry-pick
  • springboot+Vue逍遥大药房管理系统
  • python中学物理实验模拟:瞬间推力与摩擦力作用下的物体运动
  • 【数据标注师】目标跟踪标注
  • 概述-4-通用语法及分类
  • Word之空白页删除2
  • 基于Pandas和FineBI的昆明职位数据分析与可视化实现(二)- 职位数据清洗与预处理
  • UniApp Vue3 模式下实现页面跳转的全面指南
  • SQL关键字三分钟入门:ROW_NUMBER() —— 窗口函数为每一行编号
  • FreeSWITCH配置文件解析(2) dialplan 拨号计划中xml 的action解析
  • 西门子S7-200 SMART PLC:小型自动化领域的高效之选
  • C语言---常见的字符函数和字符串函数介绍
  • STM32[笔记]--7.MDK5调试功能
  • 关于ubuntu 20.04系统安装分区和重复登录无法加载桌面的问题解决
  • 医疗标准集中标准化存储与人工智能智能更新协同路径研究(上)
  • 基于Spring Boot的网上购物平台设计与实现
  • 最后的生还者2:重制版 免安 中文离线运行版+整合包
  • Vue 项目中 Excel 导入导出功能笔记
  • 【数据标注师】3D标注