JZOJ 2137. 【GDKOI2004】城市统计 (Standard IO)
2137. 【GDKOI2004】城市统计 (Standard IO)
Description
居民区i到商业区的距离指的是到距离它最近的商业区j的距离(|Xi-Xj|+|Yi-Yj|),而你将统计的是对于城市中的每一个区域k,以它为中心,所有满足max(|Xk-Xm|,|Yk-Ym|)<=r的区域m到商业区距离之和。结果同样以n*n的矩阵形式输出。
Input
第一行为n,r(1<=r<n<=150)
第二行起为一个n*n的矩阵。
Output
Sample Input
Sample Input1:
1
4 1
1 0 0 0
1 1 0 0
0 1 1 0
0 1 0 0 Sample Input2:
2
10 4
1 0 0 0 0 0 0 0 0 1
0 1 0 0 0 0 0 0 1 0
0 0 1 0 0 0 0 1 0 0
0 0 0 1 0 0 1 0 0 0
0 0 0 0 1 1 0 0 0 0
0 0 0 0 1 1 0 0 0 0
0 0 0 1 0 0 1 0 0 0
0 0 1 0 0 0 0 1 0 0
0 1 0 0 0 0 0 0 1 0
1 0 0 0 0 0 0 0 0 1 10 9
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
Sample Output
Sample Output1:
1 4 9 8
2 5 10 9
2 4 7 7
2 3 4 4 Sample Output2:
40 50 57 63 70 70 63 57 50 40
50 60 68 76 86 86 76 68 60 50
57 68 76 85 97 97 85 76 68 57
63 76 85 94 107 107 94 85 76 63
70 86 97 107 120 120 107 97 86 70
70 86 97 107 120 120 107 97 86 70
63 76 85 94 107 107 94 85 76 63
57 68 76 85 97 97 85 76 68 57
50 60 68 76 86 86 76 68 60 50
40 50 57 63 70 70 63 57 50 40 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <cstdlib>
#include <algorithm>
#define N 157
using namespace std;
int dis[N][N], dist[N][N], n, r, T, list[N * N][], map[N][N];
bool v[N][N];
int dx[] = {, , , -};
int dy[] = {, , -, }; inline int max(int a, int b) { return a > b ? a : b; }
inline int min(int a, int b) { return a < b ? a : b; } void readln()
{
for (int i = ; i <= n; i++)
for (int j = ; j <= n; j++)
scanf("%d", &map[i][j]);
} inline bool check(int x, int y)
{
if (x >= && y >= && x <= n && y <= n && !v[x][y]) return ;
return ;
} inline int abs(int x) { if (x < ) return -x;} inline int bfs(int x, int y)
{
int head = , tail = ;
list[++tail][] = x, list[tail][] = y;
while (head <= tail)
{
head++;
int q = list[head][], p = list[head][];
for (int i = ; i <= ; i++)
if (check(q + dx[i], p + dy[i]))
{
v[q + dx[i]][p + dy[i]] = ;
if (map[q + dx[i]][p + dy[i]])
{
for (int j = ; j <= tail; j++)
v[list[j][]][list[j][]] = ;
v[q + dx[i]][p + dy[i]] = ;
return abs(q + dx[i] - x) + abs(p + dy[i] - y);
}
list[++tail][] = q + dx[i];
list[tail][] = p + dy[i];
}
}
} void xgm()
{
for (int i = ; i <= n; i++)
for (int j = ; j <= n; j++)
dist[i][j] = dist[i - ][j] + dist[i][j - ] - dist[i - ][j - ] + dis[i][j];
} void writeln()
{
for (int i = ; i <= n; i++)
{
for (int j = ; j <= n - ; j++)
printf("%d ", dist[min(i + r, n)][min(j + r, n)] - dist[min(i + r, n)][max(j - r - , )] - dist[max(i - r - , )][min(j + r, n)] + dist[max(i - r - , )][max(j - r - , )]);
printf("%d\n", dist[min(i + r, n)][min(n + r, n)] - dist[min(i + r, n)][max(n - r - , )] - dist[max(i - r - , )][min(n + r, n)] + dist[max(i - r - , )][max(n - r - , )]);
}
} void work()
{
for (int i = ; i <= n; i++)
for (int j = ; j <= n; j++)
if (!map[i][j]) dis[i][j] = bfs(i, j);
xgm();
writeln();
} int main()
{
scanf("%d", &T);
while (T--)
{
memset(dis, , sizeof(dis));
memset(dist, , sizeof(dist));
memset(map, , sizeof(map));
scanf("%d%d", &n, &r);
readln();
work();
if (T != ) printf("\n");
}
}
JZOJ 2137. 【GDKOI2004】城市统计 (Standard IO)的更多相关文章
- JZOJ 5326. LCA 的统计 (Standard IO)
5326. LCA 的统计 (Standard IO) Time Limits: 1000 ms Memory Limits: 131072 KB Description Input Output S ...
- JZOJ 5258. 友好数对 (Standard IO)
5258. 友好数对 (Standard IO) Time Limits: 1000 ms Memory Limits: 524288 KB Detailed Limits Description I ...
- JZOJ 1775. 合并果子2 (Standard IO)
1775. 合并果子2 (Standard IO) Time Limits: 1000 ms Memory Limits: 65536 KB Description 在一个果园里,多多已经将所有的果子 ...
- JZOJ 5257. 小X的佛光 (Standard IO)
5257. 小X的佛光 (Standard IO) Time Limits: 2000 ms Memory Limits: 524288 KB Description Input Output Sam ...
- JZOJ 5307. 【NOIP2017提高A组模拟8.18】偷窃 (Standard IO)
5307. [NOIP2017提高A组模拟8.18]偷窃 (Standard IO) Time Limits: 1000 ms Memory Limits: 262144 KB Description ...
- JZOJ 5286. 【NOIP2017提高A组模拟8.16】花花的森林 (Standard IO)
5286. [NOIP2017提高A组模拟8.16]花花的森林 (Standard IO) Time Limits: 1000 ms Memory Limits: 131072 KB Descript ...
- JZOJ 5305. 【NOIP2017提高A组模拟8.18】C (Standard IO)
5305. [NOIP2017提高A组模拟8.18]C (Standard IO) Time Limits: 1000 ms Memory Limits: 131072 KB Description ...
- JZOJ 1349. 最大公约数 (Standard IO)
1349. 最大公约数 (Standard IO) Time Limits: 1000 ms Memory Limits: 65536 KB Description 小菜的妹妹小诗就要读小学了!正所谓 ...
- JZOJ 1736. 扑克游戏 (Standard IO)
1736. 扑克游戏 (Standard IO) Time Limits: 1000 ms Memory Limits: 128000 KB Description 有一棵无穷大的满二叉树,根为sta ...
随机推荐
- (转)Linux系统重要子目录及内容小结
Linux系统重要子目录及内容小结 原文:http://blog.csdn.net/xiaolong361/article/details/52318834 1.首先来介绍下根目录下的一些重要目录含义 ...
- IDEA使用汇总
1. 常用配置 File --> Settings (Ctrl + Alt + S) 1).提示不区分大小写: Editor-->Genereal-->Code Completion ...
- android 开发-ListView与ScrollView事件冲突处理(事件分发机制处理)
ListView和ScrollView都存在滚动的效果,所以一般不建议listView和scrollView进行嵌套使用,但有些需求则需要用到两者嵌套.在android的学习中学了一种事件分发处理机制 ...
- 检查python以及django是否安装配置成功
首先说明下,我使用pycharm作为开发的IDE,在第一次创建django项目的时候,会自动安装django包的.(网上也有很多单独安装的方法),环境变量配置成功后,就是用下面的方法检测安装成功与否. ...
- hibernate课程 初探单表映射3-1 单一主键
本节简介: 1 单一主键的两种赋值方式:手动赋值(assigned)和自动赋值(native) 2 mysql和oracle赋值的不同形式 3 demo 2 native由底层数据库生成标识符,如果是 ...
- while循环,break和continue,运算符,格式化输出
一丶while循环 while条件: 代码块(循环体) #数数 打印1-100 count = 1 while count <= 100: print(count) count += 1 执行顺 ...
- JavaScript 创建对象的七种方式
转自:xxxgitone.github.io/2017/06/10/JavaScript创建对象的七种方式/ JavaScript创建对象的方式有很多,通过Object构造函数或对象字面量的方式也可以 ...
- ubuntu 16.04安装nVidia显卡驱动和cuda/cudnn踩坑过程
安装深度学习框架需要使用cuda/cudnn(GPU)来加速计算,而安装cuda/cudnn,首先需要安装nvidia的显卡驱动. 我在安装的整个过程中碰到了驱动冲突,循环登录两个问题,以至于最后不得 ...
- [转]Jetson TX1 开发教程(1)配置与刷机
开箱 Jetson TX1是英伟达公司新出的GPU开发板,拥有世界上先进的嵌入式视觉计算系统,提供高性能.新技术和极佳的开发平台.在进行配置和刷机工作之前,先来一张全家福: 可以看到,Jetson T ...
- Spring IoC和AOP的介绍
基于Spring Framework 版本:5.0.2.RELEASE IoC 概念:传统Java开发中,程序通过new主动创建对象实例,而Spring有专门的IoC容器来创建对象,具体来说就是在Sp ...