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. Vertical roller mill from SBM

    Vertical roller mill has many different forms, but it works basically the same. All of these forms o ...

  2. 关于vi 分屏的一些指令

    分屏都是以ctrl + W(大写) 首先,ctrl+ W  , v    为切屏 之后用 :e 打开其他文件 ctrl + W , c 为关闭当前分屏 ctrl + W , h 为切换到左侧分屏   ...

  3. 几种复杂度的斐波那契数列的Java实现

    一:斐波那契数列问题的起源 13世纪初期,意大利数论家Leonardo Fibonacci在他的著作Liber Abaci中提出了兔子的繁殖问题: 如果一开始有一对刚出生的兔子,兔子的长大需要一个月, ...

  4. java打印堆栈信息

    StackTraceElement[] stackElements = new Throwable().getStackTrace(); if(stackElements != null){ for( ...

  5. 让zepto支持requirejs的方法

    window.Zepto = Zepto '$' in window || (window.$ = Zepto) if ( typeof define === "function" ...

  6. pycharm 更改字体and背景颜色

    File-settings-Appearance&Behavior-Appearance-Theme File-settings-Editor-font

  7. WAS 查看服务状态

    进入目录下/opt/IBM/WebSphere/AppServer/profiles/AppSrv01/bin 查看服务状态命令# ./serverStatus.sh 服务名 例如: [root@lo ...

  8. Java并发(五):并发,迭代器和容器

    在随后的博文中我会继续分析并发包源码,在这里,得分别谈谈容器类和迭代器及其源码,虽然很突兀,但我认为这对于学习Java并发很重要; ConcurrentModificationException: J ...

  9. spring boot 基础 多环境配置

    对于多环境的配置,各种项目构建工具的思路基本上一致,都是通过配置多份不同环境的配置文件来区分. 1. 首先我们先创建不同环境下的属性文件,截图如下: application.properties  是 ...

  10. table-列组

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...