【BZOJ 2252】 矩阵距离
【题目链接】
https://www.lydsy.com/JudgeOnline/problem.php?id=2252
【算法】
将所有是”1“的点入队,然后广度优先搜索,即可
【代码】
#include<bits/stdc++.h>
using namespace std;
#define MAXN 1010 const int dx[] = {,,-,};
const int dy[] = {-,,,}; int i,j,n,m,tx,ty;
int dist[MAXN][MAXN];
char s[MAXN][MAXN];
char val;
pair<int,int> cur;
queue< pair<int,int> > q; inline bool ok(int x,int y)
{
return x >= && x <= n && y >= && y <= m;
} int main()
{
memset(dist,,sizeof(dist));
scanf("%d%d",&n,&m);
for (i = ; i <= n; i++) scanf("%s",s[i]+);
for (i = ; i <= n; i++)
{
for (j = ; j <= m; j++)
{
if (s[i][j] == '')
{
q.push(make_pair(i,j));
dist[i][j] = ;
}
}
}
while (!q.empty())
{
cur = q.front();
q.pop();
for (i = ; i < ; i++)
{
tx = cur.first + dx[i];
ty = cur.second + dy[i];
if (ok(tx,ty) && dist[tx][ty] == -)
{
dist[tx][ty] = dist[cur.first][cur.second] + ;
q.push(make_pair(tx,ty));
}
}
}
for (i = ; i <= n; i++)
{
for (j = ; j <= m; j++)
{
printf("%d ",dist[i][j]);
}
printf("\n");
} return ; }
【BZOJ 2252】 矩阵距离的更多相关文章
- BZOJ 2252 矩阵距离
BFS. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm&g ...
- Bzoj 2252: [2010Beijing wc]矩阵距离 广搜
2252: [2010Beijing wc]矩阵距离 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 563 Solved: 274[Submit][ ...
- BZOJ 2252: [2010Beijing wc]矩阵距离
题目 2252: [2010Beijing wc]矩阵距离 Time Limit: 10 Sec Memory Limit: 256 MB Description 假设我们有矩阵,其元素值非零即1 ...
- BZOJ2252: [2010Beijing wc]矩阵距离
题解: 我脑子里都是翔??? bfs一下就行了 我居然还想什么kd tree!真是too naive,,, #include<cstdio> #include<cstdlib> ...
- 「CH2501」 矩阵距离 解题报告
CH2501 矩阵距离 描述 给定一个N行M列的01矩阵 A,\(A[i][j]\) 与 \(A[k][l]\) 之间的曼哈顿距离定义为: \(dist(A[i][j],A[k][l])=|i-k|+ ...
- bzoj 2252 [ 2010 Beijing wc ] 矩阵距离 —— 多源bfs
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2252 又没能自己想出来... 一直在想如何从每个1开始广搜更新答案,再剪剪枝,什么遇到1就不 ...
- [BZOJ 2738] 矩阵乘法 【分块】
题目链接:BZOJ - 2738 题目分析 题目名称 “矩阵乘法” 与题目内容没有任何关系..就像VFK的 A+B Problem 一样.. 题目大意是给定一个矩阵,有许多询问,每次询问一个子矩阵中的 ...
- [BZOJ]1059 矩阵游戏(ZJOI2007)
虽然说是一道水题,但小C觉得还是挺有意思的,所以在这里mark一下. Description 小Q是一个非常聪明的孩子,除了国际象棋,他还很喜欢玩一个电脑益智游戏——矩阵游戏.矩阵游戏在一个N*N黑白 ...
- BZOJ.4500.矩阵(差分约束 SPFA判负环 / 带权并查集)
BZOJ 差分约束: 我是谁,差分约束是啥,这是哪 太真实了= = 插个广告:这里有差分约束详解. 记\(r_i\)为第\(i\)行整体加了多少的权值,\(c_i\)为第\(i\)列整体加了多少权值, ...
随机推荐
- 离线安装Selenium
https://blog.csdn.net/poem_ruru/article/details/79032140
- tailf
功能说明:tailf命令几乎等同于tail -f,与tail -f不同的是,如果文件不增长,那么它不会去访问磁盘文件,也不会更改文件的访问时间.tailf命令在工作中的主要使命就是跟踪日志文件,首先将 ...
- 使用ScriptManager服务器控件前后台数据交互
前台页面信息: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebFor ...
- jenkins配置邮件通知
参考: https://www.cnblogs.com/imyalost/p/8781759.html 谢谢大佬~
- 让System.Drawing.Bitmap可以在linux运行
.net core的bitmap使用的是以下类库,但无法在linux运行 https://github.com/CoreCompat/CoreCompat 在linux运行需要安装runtime.li ...
- On branch master nothing to commit, working tree clean ERROR: Repository not found. fatal: Could not read from remote repository.
将gitbash部署hexo到github:hexo deploy 报以下错误: Administrator@liu MINGW64 /Hexo $ hexo d INFO Deploying: gi ...
- 取三级分销上下级用户id
//取上三级的用户idpublic function _get_up_third_id($member_id){ $up_id=array(); $invite_id=dbselect('invite ...
- 阅读《JavaScript设计模式》第一章心得
1.明白自己 明白了自己写的代码为什么难懂且臃肿,不方便阅读且效率低.最主要的是为什么整套流程下来只能我一个人写,因为这样的代码根本没有团队力,协同能力差.对js理解的不过透彻. 2.真正的学会对象与 ...
- 关于Spring的69个问题
Spring 概述 1. 什么是spring? Spring 是个java企业级应用的开源开发框架.Spring主要用来开发Java应用,但是有些扩展是针对构建J2EE平台的web应用.Spring ...
- Android MMS数据库存储说明
数据表 MMS模块总共包含17张表:addr.android_metadata.attachments.canonical_addresses.drm.part.pdu.pending_msgs.ra ...