bear Baby loves sleeping

题目描述

Sleeping is a favorite of little bearBaby, because the wetness of Changsha in winter is too uncomfortable. One morning, little bearBaby accidentally overslept. The result of being late is very serious. You are the smartest artificial intelligence. Now little bearBaby asks you to help him figure out the minimum time it takes to reach the teaching building.
The school map is a grid of n*m, each cell is either an open space or a building (cannot pass), and the bedroom of little bearBaby is at (1,1)—— the starting point coordinates.The teaching building is at (x, y)——the target point coordinates, he can only go up, down, left or right, it takes 1 minute for each step. The input data ensures that the teaching building is reachable.

输入描述
The first line has two positive integers n, m , separated by spaces(1 <= n, m <= 100), n for the row, m for the column
Next there are two positive integers x, y, separated by spaces(1 <= x <= n, 1 <= y <= m) indicating the coordinates of the teaching building
Next is a map of n rows and m columns, 0 indicate a open space and 1 indicate a obstacles.
输出描述
For each test case, output a single line containing an integer giving the minimum time little bearBaby takes to reach the teaching building, in minutes.

示例输入
5 4
4 3
0 0 1 0
0 0 0 0
0 0 1 0
0 1 0 0
0 0 0 1
输出
7

说明
For the input example, you could go like this:
(1,1)–>(1,2)–>(2,2)–>(2,3)–>(2,4)–>(3,4)–>(4,4)–>(4,3),so the minimum time is 7.
备注
First grid in the upper left corner is(1,1)

解:

很基础的bfs题,熟悉bfs很容易就能做出来

#include<bits/stdc++.h>
#include<string.h>
#include<queue>
using namespace std;
int inf=1000000;//用一个很大的数赋值满地图,方便计数
int m,n,i,j,t,k,xx,yy,a[101][101],b[101][101];
int dx[4]={0,1,0,-1},dy[4]={1,0,-1,0};//用于遍历上下左右四个方向
typedef pair<int,int> pp;//将坐标定义为一对数据
int bfs()
{pp p;//定义一个成对的变量queue<pp> que; //定义一个元素成对的队列que.push(pp(1,1));//将起点推入队列b[1][1]=0;//将起点赋值为0while(que.size())//队列长度不为0就继续循环{p=que.front();que.pop();//取出队列第一个数if(p.first==xx&&p.second==yy) break;//如果取出的是终点坐标,则跳出循环for(i=0;i<4;i++)//遍历四个方向{int nx=p.first+dx[i],ny=p.second+dy[i];if(nx>=1&&ny>=1&&nx<=n&&ny<=m&&a[nx][ny]!=1&&b[nx][ny]==inf)//不出界,不为障碍物,没有走过的坐标,则继续下一步{que.push(pp(nx,ny));//将该坐标推入队列,方便下一次从该坐标开始遍历b[nx][ny]=b[p.first][p.second]+1;//视为走一步,数值加一}}}return b[xx][yy];//返回终点的值
}
int main()
{scanf("%d%d",&n,&m);scanf("%d%d",&xx,&yy);for(i=1;i<=n;i++)for(j=1;j<=m;j++){scanf("%d",&a[i][j]);//输入地图b[i][j]=inf;//全部赋值为一个很大的数inf}k=bfs();printf("%d",k);}

有什么需要改进的欢迎大佬批评!

湖南大学14届新生赛--B--bearBabylovessleeping相关推荐

  1. HNUCM信息科学与工程学院第二届新生赛——正式赛

    HNUCM信息科学与工程学院第二届新生赛--正式赛 简单题 A:Yftc的字符串转换 题目描述 Ytfc来到了一个魔法密林,里面住着一只魔法兔子,Yftc想去见见这只魔法兔子,但是这个魔法密林很奇怪, ...

  2. HNUCM信息科学与工程学院第二届新生赛——热身赛

    HNUCM信息科学与工程学院第二届新生赛--热身赛 题目比较坑点较多,注意避坑,特别是数据范围和题意,因为是新生赛,所以还是选择使用了c语言书写,基本没有调用内置函数库,排序统一使用了冒泡排序,没有使 ...

  3. 蓝桥杯单片机14届省赛

    蓝桥杯单片机14届省赛代码(省一) 欢迎使用Markdown编辑器 main.c #include "HC138.h" #include "PCF8591.h" ...

  4. HNUCM湖南中医药大学信息科学与工程学院第二届新生赛

    正式赛 目录 正式赛 问题 A: Yftc的字符串转换 问题 B: XP的午餐 问题 C: XP的电灯 问题 D: XP的宝藏 问题 E: 小小yh的几何 问题 F: 我爱中国 问题 G: 小小yh的 ...

  5. L 苍天阻我寻你,此情坚贞如一(西南科技大学2021届新生赛)(线段树)

    苍天阻我寻你,此情坚贞如一 给定两个长度为nnn的数组a,ba, ba,b,满足−103≤ai,bi≤103-10 ^ 3 \leq a_i, b_i \leq 10 ^ 3−103≤ai​,bi​≤ ...

  6. 西南科技大学2021届新生赛 H.小凡出数据(构造)

    LINK 首先把输入的距离对从小到大排个序 那么距离为1,21,21,2的可以直接连起来 考虑距离为333的怎么办,设u,vu,vu,v间的距离为333 可能有两种来源,一是由1+21+21+2组成, ...

  7. 【SCAU 14年新生赛】17976 wzhp也想找女朋友 容斥定理 dfs

    Description 最近,wzhp身边有许多好朋友得到真爱. 看着好朋友与妹子成双成对,Wzhp也嫉妒起来了,他也想找个女朋友,所以他在他们班上举办了一次联谊晚会,希望能找到女票. 已知有好多个女 ...

  8. 蓝桥杯14届模拟赛第一期题解

    目录 一.数据类型检测 二.渐变色背景生成器 三.水果叠叠乐 四.element-ui 组件二次封装 五.http 模块应用 六.成语学习 七.学海无涯 一.数据类型检测 在前端开发中,js 有各种各 ...

  9. 蓝桥杯单片机第十四届省赛题目和程序答案

    目录 1.前言 2.题目 3.程序架构 3.1 display.c 3.2 ds1302.c 3.3 iic.c 3.4 onewire.c 3.5 main.c 主函数文件 3.6 环境配置 4. ...

  10. 第14届蓝桥杯省赛真题剖析-2023年5月7日Scratch编程初级组

    [导读]:超平老师的<Scratch蓝桥杯真题解析100讲>已经全部完成,后续会不定期解读蓝桥杯真题,这是Scratch蓝桥杯真题解析第135讲. 第14届蓝桥杯Scratch省赛真题,这 ...

最新文章

  1. 汇总|基于激光雷达的3D目标检测开源项目数据集
  2. 异步消息的传递-回调机制
  3. 无法解析的外部符号 __imp__timeGetTime@0
  4. 信息采集软件测试,Telegraf采集CPU数据,测试派详解
  5. 《Linux就该这么学》培训笔记_ch06_存储结构与磁盘划分
  6. java设计模式之UML③类图讲解多个类之间的关系
  7. Html常用标签元素
  8. mongodb查询内嵌文档
  9. FFmpeg 2.1 试用(新版支持HEVC,VP9)
  10. python格式化字符串%r_Python语法速查:3.字符串格式化
  11. 读取pcap获得端口_渗透技巧——获得Exchange GlobalAddressList的方法
  12. Rust 学习笔记——智能指针
  13. AD18绘制AHT20原理图及PCB电路设计
  14. 怎样裁切图片?如何将图片剪裁成需要的大小?
  15. Linux系统中dmesg命令使用详解
  16. TYVJ P1002 谁拿了最多奖学金 Label:模拟 水
  17. HTML+CSS大作业: 抗击疫情网页制作作业_疫情防控网页设计
  18. IE被劫持的手动解除
  19. 一对一、一对多、多对多(实操)
  20. java对excel加密_java poi对Excel文件加密

热门文章

  1. WARNING: AllowZoneDrifting is enabled.
  2. 超火的微信渐变国旗头像,一键生成!!
  3. 使用Qt给微信头像添加国旗
  4. MIMO天线市场深度研究分析报告
  5. 萨达萨达发神鼎飞丹砂
  6. 欢迎使用CSDN-markdown编辑器萨达所大所大所大所
  7. Verifying dml pool data
  8. 【散文】 漫步在春天
  9. 自制太阳能手机充电器
  10. 22021年江苏高考成绩查询,江苏省教育考试院2021年江苏高考成绩查询时间及系统入口...