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. Unity UGUI按钮添加点击事件

    1. 可视化创建及事件绑定 # 1 : 通过 Hierarchy 面板创建 UI > Button. 2 : 创建一个脚本 TestClick.cs, 定义了一个 Click 的 public ...

  2. 常见SQL优化方法

    SQL优化的一些方法 1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中对字段进行 null 值判断,否 ...

  3. 在Magento中用MySQL模拟队列发送电子邮件

    1. 需求 顾客在网站上购买特定商品并且这些商品的总金额超过特定金额后,使用email给顾客发送一个优惠券:假如某件商品已经降价了,则此商品的金额不计算在目标总金额内: 2. 需求分析 ①发送优惠券的 ...

  4. rem媒体查询

    @media only screen and (min-width: 1080px), only screen and (min-device-width:1080px) { html,body { ...

  5. echarts的title和legend重合解决(各种小细节)

    一:关于title与legend重叠 1.重合样子 2.解决办法: legend:{ show: true, top:"6%",//与上方的距离 可百分比% 可像素px }, 3. ...

  6. thinkphp搜索实现

    视图: <html lang="zh-cn"><head> <meta charset="UTF-8"><title& ...

  7. Android 仿电商app商品详情页按钮浮动效果

    1.效果图如下: 这效果用户体验还是很酷炫,今天我们就来讲解如何实现这个效果. 2.分析 为了方便理解,作图分析 如图所示,整个页面分为四个部分: 1.悬浮内容,floatView 2.顶部内容,he ...

  8. nbtscan ip地址

    查找网络(192.168.1.0)中netbios名字信息,对应命令如下: nbtscan 192.168.1.1-254 找到有netbios名字后,可以使用如下的命令查看这些主机运行的服务. nb ...

  9. 调用cmd命令行命令(借鉴)

    留待以后观看 ———————————————————————————————————————————————————————————————————————————— public class IP_ ...

  10. 查看SAP CRM和C4C的UI technical信息

    CRM 比如我们想看Quantity这个字段到底是绑在哪个模型上,选中该字段按F2: 就能知道是绑在Context node BTADMINI的QUANTITY字段上. C4C 同理,使用debugM ...