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 ...
随机推荐
- python去除字符串中间空格的方法
1.使用字符串函数replace a = 'hello world' a.replace(' ', '') # 'helloworld' 2.使用字符串函数split a = ''.join(a.sp ...
- [HNOI2009]有趣的数列(卡塔兰数,线性筛)
[HNOI2009]有趣的数列 题目描述 我们称一个长度为2n的数列是有趣的,当且仅当该数列满足以下三个条件: (1)它是从1到2n共2n个整数的一个排列{ai}: (2)所有的奇数项满足a1< ...
- DOM 操作表格
操作表格<table>标签是 HTML 中结构最为复杂的一个,我们可以通过 DOM 来创建生成它,或者 HTML DOM 来操作它.(PS:HTML DOM 提供了更加方便快捷的方式来操作 ...
- 英国已有500万宽带用户接入并开始使用IPv6技术
2018年英国首家为客户提供IPv6的主要ISP.随着所有现有的符合条件的用户线路启用,约90%的固定宽带用户群接入并开始使用IPv6,为IPv6互联网增加了超过500万个新眼球. 英国IPv6项目于 ...
- 给mongodb设置密码
内容来自:https://segmentfault.com/a/1190000011554055 mongodb安装后是无需密码 Mongodb安装后自身是没有密码的,用户连接只需填写id地址,端口号 ...
- TFrecords读、写图片文件
参考:https://blog.csdn.net/u014802590/article/details/68495238 参考:https://www.2cto.com/kf/201709/68057 ...
- tensorflow2 keras.Callback logs
官方文档上表示logs内存的内容为 on_epoch_end: logs include `acc` and `loss`, and optionally include `val_loss` (if ...
- MySQL:MySQL日期数据类型、MySQL时间类型使用总结
MySQL 日期类型:日期格式.所占存储空间.日期范围 比较. 日期类型 存储空间 日期格式 日期范围------------ -------- ...
- Http发送Json
public static JSONObject post(String url,JSONObject json){ HttpClient client = new DefaultHttpClient ...
- (转)用Flink取代Spark Streaming!知乎实时数仓架构演进
转:https://mp.weixin.qq.com/s/e8lsGyl8oVtfg6HhXyIe4A AI 前线导读:“数据智能” (Data Intelligence) 有一个必须且基础的环节,就 ...