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 ...
随机推荐
- Maya2014下载安装与激活
目录 1. 更多推荐 2. 下载地址 2.1. OneDrive 2.2. 其他下载地址 3. 激活步骤 1. 更多推荐 其他Maya版本的下载与激活:https://www.cnblogs.com/ ...
- Sql 语句中使用参数
using System; using System.Data; using System.Data.SqlClient; namespace ConsoleApplication2 { public ...
- 一、bootstrap-upload
一.bootstrap-upload 前端代码: @{ Layout = null; } <!DOCTYPE html> <html lang="zh-CN"&g ...
- utf8 中文
#include <iostream> #include <codecvt> #include <fstream> #include <string> ...
- 前端每日实战:21# 视频演示如何用纯 CSS 创作文本滑动特效的 UI 界面
效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/QrxxaW 可交互视频教程 此视频 ...
- flask之模板之继承
一:继承 基类模板base.html 中在进行挖坑 {% block 坑的名字%}{% endblock %} 子类模板test.html 中 通过 {% extends "base.ht ...
- AMROC可视化
备注 通过修改Filetype来改变输出数据的类型. hdf2tab.sh转换全部数据文件 hdf2tab.sh -m多维数据 <!DOCTYPE html PUBLIC "-//W3 ...
- LeetCode--058--最后一个单词(java)
给定一个仅包含大小写字母和空格 ' ' 的字符串,返回其最后一个单词的长度. 如果不存在最后一个单词,请返回 0 . 说明:一个单词是指由字母组成,但不包含任何空格的字符串. 示例: 输入: &quo ...
- 图片没有.png或者jpg后缀能不能加载?
是可以的,如:http://mmbiz.qpic.cn/mmbiz_png/MW1VecOrnw6HUMvEUNUxibLVtbY2vHF8QkFyviaickh8pnsapQ8gOosdq13SBX ...
- Oracle数据库一些操作信息
Oracle数据库如何查看当前用户角色权限及默认表空间查看当前用户的一些信息,包括用户拥有的角色权限信息.用户表空间以及用户和默认表空间的关系等--查看用户的角色权限1.查看当前用户拥有的角色权限信息 ...