zoukankan      html  css  js  c++  java
  • JZOJ 2137. 【GDKOI2004】城市统计 (Standard IO)

    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求出每个点的权值,然后跑一遍二位前缀和就好啦。
     
    代码如下:
     1 #include <cstdio>
     2 #include <iostream>
     3 #include <cstring>
     4 #include <string>
     5 #include <cstdlib>
     6 #include <algorithm>
     7 #define N 157
     8 using namespace std;
     9 int dis[N][N], dist[N][N], n, r, T, list[N * N][2], map[N][N];
    10 bool v[N][N];
    11 int dx[4] = {0, 1, 0, -1};
    12 int dy[4] = {1, 0, -1, 0};
    13 
    14 inline int max(int a, int b) {    return a > b ? a : b; }
    15 inline int min(int a, int b) {    return a < b ? a : b; }
    16 
    17 void readln()
    18 {
    19     for (int i = 1; i <= n; i++)
    20         for (int j = 1; j <= n; j++)
    21                 scanf("%d", &map[i][j]);    
    22 }
    23 
    24 inline bool    check(int x, int y)
    25 {
    26     if (x >= 1 && y >= 1 && x <= n && y <= n && !v[x][y])    return 1;
    27     return 0;
    28 }
    29 
    30 inline int abs(int x) { if (x < 0)    return -x;}
    31 
    32 inline int bfs(int x, int y)
    33 {
    34     int head = 0, tail = 0;
    35     list[++tail][0] = x, list[tail][1] = y;
    36     while (head <= tail)
    37     {
    38         head++;
    39         int q = list[head][0], p = list[head][1];
    40         for (int i = 0; i <= 3; i++)
    41             if (check(q + dx[i], p + dy[i]))
    42             {
    43                 v[q + dx[i]][p + dy[i]] = 1;
    44                 if (map[q + dx[i]][p + dy[i]])
    45                 {
    46                     for (int j = 1; j <= tail; j++)
    47                         v[list[j][0]][list[j][1]] = 0;
    48                     v[q + dx[i]][p + dy[i]] = 0;
    49                     return abs(q + dx[i] - x) + abs(p + dy[i] - y);
    50                 }
    51                 list[++tail][0] = q + dx[i];
    52                 list[tail][1] = p + dy[i];
    53             }
    54     }
    55 }
    56 
    57 void xgm()
    58 {
    59     for (int i = 1; i <= n; i++)
    60         for (int j = 1; j <= n; j++)
    61             dist[i][j] = dist[i - 1][j] + dist[i][j - 1] - dist[i - 1][j - 1] + dis[i][j]; 
    62 }
    63 
    64 void writeln()
    65 {
    66     for (int i = 1; i <= n; i++)
    67     {
    68         for (int j = 1; j <= n - 1; j++)
    69             printf("%d ", dist[min(i + r, n)][min(j + r, n)] - dist[min(i + r, n)][max(j - r - 1, 0)] - dist[max(i - r - 1, 0)][min(j + r, n)] + dist[max(i - r - 1, 0)][max(j - r - 1, 0)]); 
    70         printf("%d
    ", dist[min(i + r, n)][min(n + r, n)] - dist[min(i + r, n)][max(n - r - 1, 0)] - dist[max(i - r - 1, 0)][min(n + r, n)] + dist[max(i - r - 1, 0)][max(n - r - 1, 0)]);
    71     }            
    72 }
    73 
    74 void work()
    75 {
    76     for (int i = 1; i <= n; i++)
    77         for (int j = 1; j <= n; j++)
    78             if (!map[i][j])    dis[i][j] = bfs(i, j);
    79     xgm();
    80     writeln();
    81 }
    82 
    83 int main()
    84 {
    85     scanf("%d", &T);
    86     while (T--)
    87     {
    88         memset(dis, 0, sizeof(dis));
    89         memset(dist, 0, sizeof(dist));
    90         memset(map, 0, sizeof(map));
    91         scanf("%d%d", &n, &r);
    92         readln();
    93         work();    
    94         if (T != 0)    printf("
    ");
    95     }
    96 }
    View Code
  • 相关阅读:
    开源的免费的对比工具
    win10 git bash 配置
    Java SSH 不使用终端也能调用环境变量中的指令
    MySQL WITH ROLLUP
    docker安装postgres
    开源的应用容器引擎
    清除浮动有哪几种方法
    js中的yield
    git的速学了解
    string/stringBuffer/StringBuilder的区别
  • 原文地址:https://www.cnblogs.com/traveller-ly/p/9338432.html
Copyright © 2011-2022 走看看