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 ...
随机推荐
- C# 列出并删除一个文件夹下的所有MD5值相同的文件
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- [python 学习] 类
#!/usr/bin/python # -*- encoding:utf-8 -*- class Animal: animal_num = 0 class Dog(Animal): #类帮助文档 't ...
- 安装phpredis扩展以及phpRedisAdmin工具
先从phpredis的git拿到最新的源码包:wget https://github.com/nicolasff/phpredis/archive/master.tar.gz 然后解压到进入目录:ta ...
- 考研结束-开启新生活---markdown语法
markdown语法 考研结束,正式开始提高自己的技术储备. 第一步当然是找到自己原先的博客园,记录下自己的足迹 将博客园设置为markdown编辑器 找到一篇关于markdown的语法介绍 原博文链 ...
- OSS重磅推出OSS Select——使用SQL选取文件的内容
对象存储OSS(Object Storage Service)具有海量.可靠.安全.高性能.低成本的特点.OSS提供标准.低频.归档类型,覆盖多种数据从热到冷的存储需求,单个文件的大小从1字节到48. ...
- 使用 Visual Studio 调试器附加到运行的进程
为什么调试附加进程? Visual Studio 调试器可以附加到在 Visual Studio 外运行的进程. 可以使用此附加功能执行以下操作: 调试并非在 Visual Studio 中创建的应用 ...
- windows 系统再重启后,USB口失效(鼠标、U盘都无法识别)的过程及解决方法
今天都差点忘记写随笔.今天在工作中,将电脑重启了一次,悲催了.重启完成后,鼠标无法使用了.最初认为 鼠标的问题,就一直"砸",但后来换了鼠标,仍然不能使用,开始认为没这边简单,拿出 ...
- CF1182 D Complete Mirror——思路
题目:http://codeforces.com/contest/1182/problem/D 很好的思路是从度数为1的点和直径来入手. 找一条直径.看看直径的两个端点是否合法. 如果都不合法,那么根 ...
- InputStream类的available()方法
InputStream类的available()方法 这个方法可以在读写操作前先得知数据流里有多少个字节可以读取需要注意的是,如果这个方法用在从本地文件读取数据时,一般不会遇到问题,但如果是用于网络操 ...
- mybatis获取数据库自增id
http://blog.csdn.net/dyllove98/article/details/8866357 http://www.iteye.com/problems/86864 insert标签中 ...