B bearBaby loves sleeping
链接:https://ac.nowcoder.com/acm/contest/338/B
来源:牛客网
题目描述
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.
备注:
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的更多相关文章
- bearBaby loves sleeping(BFS)
时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 131072K,其他语言262144K 64bit IO Format: %lld 题目描述 Sleeping is a favorit ...
- Codeforces 390A( 模拟题)
Inna and Alarm Clock Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64 ...
- Jamie and Alarm Snooze
Description Jamie loves sleeping. One day, he decides that he needs to wake up at exactly hh: mm. Ho ...
- CodeForces-916A-jamie and Alarm Snooze(笨比题目)
链接: https://vjudge.net/problem/CodeForces-916A 题意: Jamie loves sleeping. One day, he decides that he ...
- Microsoft Loves Linux
微软新任CEO纳德拉提出的“Microsoft Loves Linux”,并且微软宣布.NET框架的开源,近期Microsoft不但宣布了Linux平台的SQL Server,还宣布了Microsof ...
- 5806 NanoApe Loves Sequence Ⅱ(尺取法)
传送门 NanoApe Loves Sequence Ⅱ Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/131072 K ...
- 5805 NanoApe Loves Sequence(想法题)
传送门 NanoApe Loves Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/131072 K ( ...
- CF444C. DZY Loves Colors[线段树 区间]
C. DZY Loves Colors time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces444C DZY Loves Colors(线段树)
题目 Source http://codeforces.com/problemset/problem/444/C Description DZY loves colors, and he enjoys ...
随机推荐
- LightOJ 1289 LCM from 1 to n(位图标记+素数筛
https://vjudge.net/contest/324284#problem/B 数学水题,其实就是想写下位图..和状压很像 题意:给n让求lcm(1,2,3,...,n),n<=1e8 ...
- django之路由的理解
一:路由 简单的路由过程图: 1. 路由的定义位置 路由定义方式一:主路由和子路由分开定义 主路由的定义 urls.py from django.conf.urls import url from d ...
- 为啥HashMap的默认容量是16
集合是Java开发日常开发中经常会使用到的,而作为一种典型的K-V结构的数据结构,HashMap对于Java开发者一定不陌生. 在日常开发中,我们经常会像如下方式以下创建一个HashMap: Map& ...
- NLP第一周
19-21周,每周学习15小时以上 基础:Python编程基础:基础的概览统计.了解线性代数:足够的时间投入. 完成9个课程项目,每个5小时-15小时 完成聊天机器人项目(40-80小时) Capst ...
- Hybris Commerce下单时遇到产品库存不足的解决办法
客户在Storefront下单试图购买一个产品时,遇到out of stock库存不足的错误,无法下单: 解决办法:登录Backoffice,Stock level菜单: 创建一个新的stock le ...
- tensorflow函数介绍(4)
1.队列的实现: import tensorflow as tf q=tf.FIFOQueue(2,'int32') #创建一个先进先出队列,指定队列中最多可以保存两个元素,并指定类型为整数. #先进 ...
- CentOS下安装Chrome浏览器中文显示为方框
执行如下三条命令 yum groupinstall "X Window System" -y yum -y groupinstall chinese-support yum -y ...
- hud 4347 The Closest M Points(KD-Tree)
传送门 解题思路 \(KD-Tree\)模板题,\(KD-Tree\)解决的是多维问题,它是一个可以储存\(K\)维数据的二叉树,每一层都被一维所分割.它的插入删除复杂度为\(log^2 n\),它查 ...
- 2019 年百度之星·程序设计大赛 - 复赛 Diversity (树形DP)
题目:https://vjudge.net/contest/325352#problem/A 题意:给你一棵树,每个点给一个区间,可以选区间里面任何一个数,然后问怎么安排得到最大边两点之间的差值和,求 ...
- 用三目运算,与if判断 函数调用 达到相同判定作用
三目运算符: 操作数1 ? 操作数2 : 操作数3 (操作数1位bool类型,操作数2和操作数3为两个相同的任何类型) 返回结果:如果操作数1判定结果为真,则将操作数2作为返回结果如果操作 ...