2137. 【GDKOI2004】城市统计 (Standard IO)

Time Limits: 1000 ms  Memory Limits: 128000 KB  Detailed Limits  

Goto ProblemSet

Description

  中山市的地图是一个n*n的矩阵,其中标号为1的表示商业区,标号为0的表示居民区。为了考察市内居民区与商业区的距离,并对此作出评估,市长希望你能够编写一个程序完成这一任务。
  居民区i到商业区的距离指的是到距离它最近的商业区j的距离(|Xi-Xj|+|Yi-Yj|),而你将统计的是对于城市中的每一个区域k,以它为中心,所有满足max(|Xk-Xm|,|Yk-Ym|)<=r的区域m到商业区距离之和。结果同样以n*n的矩阵形式输出。
 

Input

         第一行为t,表示以下有t组数据,每组数据之间以空行隔开,以下:
   第一行为n,r(1<=r<n<=150)   
          第二行起为一个n*n的矩阵。

Output

  t组n*n的矩阵。每组用空行隔开
 

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 
 
做法:先用bfs求出每个点的权值,然后跑一遍二位前缀和就好啦。
 
代码如下:
 #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)的更多相关文章

  1. JZOJ 5326. LCA 的统计 (Standard IO)

    5326. LCA 的统计 (Standard IO) Time Limits: 1000 ms Memory Limits: 131072 KB Description Input Output S ...

  2. JZOJ 5258. 友好数对 (Standard IO)

    5258. 友好数对 (Standard IO) Time Limits: 1000 ms Memory Limits: 524288 KB Detailed Limits Description I ...

  3. JZOJ 1775. 合并果子2 (Standard IO)

    1775. 合并果子2 (Standard IO) Time Limits: 1000 ms Memory Limits: 65536 KB Description 在一个果园里,多多已经将所有的果子 ...

  4. JZOJ 5257. 小X的佛光 (Standard IO)

    5257. 小X的佛光 (Standard IO) Time Limits: 2000 ms Memory Limits: 524288 KB Description Input Output Sam ...

  5. JZOJ 5307. 【NOIP2017提高A组模拟8.18】偷窃 (Standard IO)

    5307. [NOIP2017提高A组模拟8.18]偷窃 (Standard IO) Time Limits: 1000 ms Memory Limits: 262144 KB Description ...

  6. JZOJ 5286. 【NOIP2017提高A组模拟8.16】花花的森林 (Standard IO)

    5286. [NOIP2017提高A组模拟8.16]花花的森林 (Standard IO) Time Limits: 1000 ms Memory Limits: 131072 KB Descript ...

  7. 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 ...

  8. JZOJ 1349. 最大公约数 (Standard IO)

    1349. 最大公约数 (Standard IO) Time Limits: 1000 ms Memory Limits: 65536 KB Description 小菜的妹妹小诗就要读小学了!正所谓 ...

  9. JZOJ 1736. 扑克游戏 (Standard IO)

    1736. 扑克游戏 (Standard IO) Time Limits: 1000 ms Memory Limits: 128000 KB Description 有一棵无穷大的满二叉树,根为sta ...

随机推荐

  1. 牛客网Java刷题知识点之基本类型的自动转换和基本类型的强制转换

    不多说,直接上干货! TypeConvertDemo.java //自动类型转换 class TypeConvertDemo { public static void main(String[] ar ...

  2. PHP实例:使用PHPExcel导入Excel2003文档和Excel2007文档到MySQL数据库中

    如果要使用phpExcelReader将Excel 数据导入到mysql 数据库,请读者点击这个文章查看. 使用phpExcelReader将Excel 数据导入到mysql 数据库. 下面我们介绍另 ...

  3. SpringBoot | 第零章:前言

    缘起 前段时间公司领导叫编写一两课关于springboot的基础知识培训课程,说实话,也是今年年初才开始接触了SpringBoot这个脚手架,使用了之后才发现打开了一个新世界.再之后也没有一些系统的学 ...

  4. kotlin查看编译后的Java代码

    java学一下kotlin,由于用的是同样的jvm,那就说明他们的字节码文件应该是一样的,那么,如果我们能看到编译后的文件,那么学的更快了. 操作 1.打开一个.kt文件 2.在Android Stu ...

  5. httpclient通过post提交到webapi

    var client = new HttpClient(); var url = BASConfig.Instance.SiteSettingsModule.SyncWorkLogAppUrl; va ...

  6. Nginx 安装(CentOS )非yum安装

    Nginx 安装(CentOS ) 一.安装编译工具及库文件 yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-d ...

  7. vue-pos : 子组件与子组件通讯

    子组件与子组件通讯: 例子子组件1 要与子组件2 通讯 步骤1 : 在父组件新建一个 vue 对象 : const eventHub = new Vue() 步骤2 : 子组件1 发起事件 :this ...

  8. npm warn weex @1.0.0 no repository field

    玩weex出现nmp安装问题总是包这个错,但是其实是安装成功的 npm warn weex@1.0.0 no repository field. 看字面意思大概是package.json里缺少repo ...

  9. ArrayList,Vector, LinkedList 的存储性能和特性

    ArrayList 和Vector他们底层的实现都是一样的,都是使用数组方式存储数据,此数组元素数大于实际存储的数据以便增加和插入元素,它们都允许直接按序号索引元素,但是插入元素要涉及数组元素移动等内 ...

  10. OSS基本概念介绍

    存储空间(Bucket): 存储空间是用于存储对象(Object)的容器,所有的对象都必须隶属于某个存储空间. 可以设置和修改存储空间属性用来控制地域.访问权限.生命周期等,这些属性设置直接作用于该存 ...