链接:https://ac.nowcoder.com/acm/contest/338/B
来源:牛客网

题目描述

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.
示例1

输入

复制

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 <iostream>
#include <stdio.h>
#include <queue>
#include<cstring>
using namespace std;
typedef pair<int, int> P;
#define max_n 102
#define max_m 102
#define inf 1000000
int N,M;
int map[max_n][max_m];
int direct[max_n][max_m];
int sx,sy;
int ans;
int dx[4]= {1,0,-1,0},dy[4]= {0,1,0,-1}; int bfs(int sx,int sy,int gx,int gy)
{
int nx,ny;
queue<P> Q;
memset(direct, inf, sizeof(direct));
Q.push(P(sx,sy));
direct[sx][sy]=0;
while (Q.size())
{
P q=Q.front();
Q.pop();
if (q.first==gx&&q.second==gy)
break;
else
{
for (int i=0; i<=3; i++)
{
nx=q.first+dx[i];
ny=q.second+dy[i];
if (nx<0||nx>N||ny<0||ny>M||map[nx][ny]==1||direct[nx][ny]<inf)
continue;
else
{
direct[nx][ny]=direct[q.first][q.second]+1;
Q.push(P(nx,ny));
}
}
}
}
return direct[gx][gy];
}
int main()
{
int gx,gy;
scanf("%d %d",&N,&M);
scanf("%d %d",&gx,&gy);
for (int i=0; i<N; i++)
for(int j = 0; j<M; j++)
{
scanf("%d",&map[i][j]);
} ans=bfs(0, 0, gx-1, gy-1);
printf("%d\n",ans);
return 0;
}

B bearBaby loves sleeping的更多相关文章

  1. bearBaby loves sleeping(BFS)

    时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 131072K,其他语言262144K 64bit IO Format: %lld 题目描述 Sleeping is a favorit ...

  2. Codeforces 390A( 模拟题)

    Inna and Alarm Clock Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64 ...

  3. Jamie and Alarm Snooze

    Description Jamie loves sleeping. One day, he decides that he needs to wake up at exactly hh: mm. Ho ...

  4. CodeForces-916A-jamie and Alarm Snooze(笨比题目)

    链接: https://vjudge.net/problem/CodeForces-916A 题意: Jamie loves sleeping. One day, he decides that he ...

  5. Microsoft Loves Linux

    微软新任CEO纳德拉提出的“Microsoft Loves Linux”,并且微软宣布.NET框架的开源,近期Microsoft不但宣布了Linux平台的SQL Server,还宣布了Microsof ...

  6. 5806 NanoApe Loves Sequence Ⅱ(尺取法)

    传送门 NanoApe Loves Sequence Ⅱ Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/131072 K ...

  7. 5805 NanoApe Loves Sequence(想法题)

    传送门 NanoApe Loves Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/131072 K ( ...

  8. CF444C. DZY Loves Colors[线段树 区间]

    C. DZY Loves Colors time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  9. Codeforces444C DZY Loves Colors(线段树)

    题目 Source http://codeforces.com/problemset/problem/444/C Description DZY loves colors, and he enjoys ...

随机推荐

  1. linux命令之 repeat 重复执行命令

    $ vim ~/.bashrc function repeat() { number=$1 shift echo $@ for n in $(seq $number); do $@ done } $ ...

  2. pycharm中能运行,但是往往py都要放到服务器上去跑,问题来了

    py文件在linux上运行,导包错误: 在py文件中添加项目的根目录: import sys sys.path.append('项目路径') sys.path.append(os.path.dirna ...

  3. django之数据模型类的字段分析

    一:表一的字段分析 class Sheep_Area(models.Model):# models.AutoField()自增列,要显示自定义的自增列,必须定义primary=True# area_i ...

  4. zabbix agentd错误日志解决办法

    公司新上了一台服务器,我安装了zabbix_agents软件包,并复制了zabbix server端的zabbix_agentd.conf到/etc/zabbix里面并修改了相关的参数,并启动了zab ...

  5. django权限之二级菜单

    遗漏知识点 1.构建表结构时,谁被关联谁就是主表,在层级删除的时候,删除子表的时候,主表不会被删除,反之删除主表的话,字表也会被删除, 使用related_name=None   反向查询,起名用的 ...

  6. LeetCode--049--字母异位词分组(java)

    给定一个字符串数组,将字母异位词组合在一起.字母异位词指字母相同,但排列不同的字符串. 示例: 输入: ["eat", "tea", "tan&quo ...

  7. 17 安全字符串 System.Security.SecureString

  8. servlet中中文正常显示,mysql数据库手动插入中文正常显示,servlet向mysql中插入中文显示乱码

    作者:http://5563447.blog.51cto.com/5553447/1422627 问题是:就是POST请求提交表单数据给servlet,通过JDBC插入Mysql,出现中文乱码. 解决 ...

  9. 如何从word文档复制内容到富文本编辑器

    在之前在工作中遇到在富文本编辑器中粘贴图片不能展示的问题,于是各种网上扒拉,终于找到解决方案,在这里感谢一下知乎中众大神以及TheViper. 通过知乎提供的思路找到粘贴的原理,通过TheViper找 ...

  10. SPFA的两个优化

    评测题:洛谷[模板]单源最短路径 不加任何优化: queue<int>q; void spfa(ll s) { ;i<=n;i++) d[i]=(ll)(); d[s]=;q.pus ...