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. SpringBoot | 第十二章:RabbitMQ的集成和使用

    前言 上节讲了缓存数据库redis的使用,在实际工作中,一般上在系统或者应用间通信或者进行异步通知(登录后发送短信或者邮件等)时,都会使用消息队列进行解决此业务场景的解耦问题.这章节讲解下消息队列Ra ...

  2. 搭建Node.js Redis开发环境

    创建项目 初始化为node项目 $npm init   安装redis   安装@types/node, @types/redis, typescript   初始化TypeScript   配置ts ...

  3. 锁丶threading.local丶线程池丶生产者消费者模型

    一丶锁 线程安全: 线程安全能够保证多个线程同时执行时程序依旧运行正确, 而且要保证对于共享的数据,可以由多个线程存取,但是同一时刻只能有一个线程进行存取. import threading v = ...

  4. 使用FusionCharts创建可更新数据的JavaScript图表

    先创建一个简单的图表,然后改变它的数据(请参见下面的代码).图表最初据显示8月份的销售数据,当用户点击按钮时改为显示9月份的销售数据.每个月都有单独的XML文件,代码如下: <html> ...

  5. 命令行启动mysql服务

    在<计算机网络>课程中曾学过net命令,可以用于启动后台服务.在mysql中,net命令用于启动后台服务器进程mysqld,即后台服务. 不过,如果在普通用户模式下net start my ...

  6. System Center Configuration Manager 2016 域准备篇(Part4)

    步骤4.创建系统管理容器 注意:在Active Directory域控制器服务器(AD01)上以本地管理员身份执行以下操作 有关您为何这样做的详细信息,请参阅https://docs.microsof ...

  7. jquery UI_tabs

    1.tab使用 <!doctype html> <html lang="en"> <head> <meta charset="u ...

  8. IOS 拖拽事件(手势识别)

    @interface NJViewController () @property (weak, nonatomic) IBOutlet UIView *customView; @end @implem ...

  9. hdu-1556 Color the ball---树状数组+区间修改单点查询

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1556 题目大意: Problem Description N个气球排成一排,从左到右依次编号为1,2 ...

  10. 【BZOJ3668】[NOI2014] 起床困难综合症(位运算思想)

    点此看题面 大致题意: 给定一些位运算操作,让你在\(0\sim m\)范围内选一个初始值,使其在经过这些运算后得到的结果最大. 前置技能:关于位运算 作为一道位运算的题,如果你不知道什么是位运算,那 ...