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

BFS 走迷宫

 

#include<bits/stdc++.h>
using namespace std;
int a[100][100],v[100][100];//访问数组  n,m<=100
struct point
{int x;int y;int step;
};
queue<point> r;//申请队列 
int dx[4]={0,1,0,-1};//四个方向 右下左上 
int dy[4]={1,0,-1,0};
int main()
{
/*
5 4
1 1 2 1
1 1 1 1
1 1 2 1
1 2 1 1
1 1 1 2
1 1 4 3
*/ //输入int n,m,startx,starty,p,q;scanf("%d%d",&n,&m);for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)scanf("%d",&a[i][j]);scanf("%d%d%d%d",&startx,&starty,&p,&q); //BFSpoint start;start.x=startx; start.y=starty;start.step=0;r.push(start);//将起点入队v[startx][starty]=1;//已经访问int flag=0;while(!r.empty()){int x=r.front().x,y=r.front().y;if(x==p&&y==q){flag=1;printf("%d",r.front().step);break;}for(int k=0;k<=3;k++){int tx,ty;//四方向试探tx=x+dx[k];ty=y+dy[k];if(a[tx][ty]==1 &&v[tx][ty]==0){//入队point temp;temp.x=tx;temp.y=ty;temp.step=r.front().step+1;r.push(temp);v[tx][ty]=1; } }r.pop();//拓展完了队首元素出队 }if(flag==0)printf("-1");return 0;
}

http://www.lryc.cn/news/536876.html

相关文章:

  • 【Linux系统】—— 简易进度条的实现
  • Qt 中使用 SQLite 数据库的完整指南
  • 数智化时代的工单管理:从流程驱动到数据驱动-亿发
  • Large Language Model Distilling Medication Recommendation Model
  • floodfill算法系列一>被围绕的区域
  • Redis 01 02章——入门概述与安装配置
  • windows基于cpu安装pytorch运行faster-whisper-large-v3实现语音转文字
  • AI大模型(如GPT、BERT等)可以通过自然语言处理(NLP)和机器学习技术,显著提升测试效率
  • 【Prometheus】prometheus黑盒监控balckbox全面解析与应用实战
  • CSS实现单行、多行文本溢出显示省略号(…)
  • 服务器中部署大模型DeepSeek-R1 | 本地部署DeepSeek-R1大模型 | deepseek-r1部署详细教程
  • 元学习之孪生网络Siamese Network
  • 深入HBase——引入
  • Python创建FastApi项目模板
  • TCNE 网络安全
  • 车规MCU处理器选择Cortex-M7还是Cortex-R52?
  • 什么是计算机中的 “终端”?
  • LeetCode刷题---字符串---819
  • SSH IBM AIX服务器相关指标解读
  • Wireshark TS | 再谈虚假的 TCP Spurious Retransmission
  • 基于kafka、celery的日志收集报警项目
  • QML使用ChartView绘制饼状图
  • 头歌实验--面向对象程序设计
  • DeepSeek-R1 蒸馏 Qwen 和 Llama 架构 企业级RAG知识库
  • App UI自动化--Appium学习--第二篇
  • 【SpringBoot实现全局API限频】 最佳实践
  • Day1 25/2/14 FRI
  • 开发板适配之I2C-RTC
  • vuedraggable固定某一item的记录
  • 我的新书《青少年Python趣学编程(微课视频版)》出版了!