[GDKOI2004] City Statistics Problem Solution

It’s been a long time since I wrote a blog. I have time to write about it today.

Title description

The map of Zhongshan City is a n ∗ nn*nnA matrix of n , where the number 1 represents the commercial area, and the number 0 represents the residential area. In order to investigate the distance between residential and commercial areas in the city, and make an assessment of this, the mayor hopes that you can write a program to complete this task. The distance between a residential area i and a commercial area refers to the distance to the nearest commercial area j(∣ X i − X j ∣ + ∣ Y i − Y j ∣) (|Xi-Xj|+|Yi-Yj| )(XiXj+YiY j ) (you can understand that their ranks are different respectively), and what you will count is for each area k in the city, with it as the center(2 ∗ r + 1) ∗ (2 ∗ r + 1) (2*r+1)*(2*r+1)(2r+1)(2r+1 ) The sum of the distances from all residential areas to commercial areas in the matrix area. The result is also output in the form of n*n matrix.

enter

The first line is t, which means there are t groups of data below, and each group of data is separated by blank lines. The following: the
first line n, r (1 <= r <n <= 150) n,r(1<=r <n<=150)n,r(1<=r<n<=1 5 0 )
starting from the second line is ann ∗ nn*nnn matrix.

Output

t t t groupn ∗ nn*nnn matrix. Each group is separated by blank lines

Sample input

1
4 1
1 0 0 0
1 1 0 0
0 1 1 0
0 1 0 0

Sample output

1 4 9 8
2 5 10 9
2 4 7 7
2 3 4 4

Main idea:

Have n 2 n^2n2 size square matrix, there are0 00 and1 11 , find every0 00 The nearest1 1The Manhattan distance of 1 , the statistics arekkfor each areak , with it as the center(2 ∗ r + 1) ∗ (2 ∗ r + 1) (2*r+1)*(2*r+1)(2r+1)(2r+1 ) all0 0in the matrix area0 to1 1The sum of the distances of 1 . The result is alson 2 n^2n2 output in matrix form.

Ideas:

60~70 points:

Direct explosive force enumerate every 0 0The distance from 0 to 1, then violently enumerate(2 ∗ r + 1) ∗ (2 ∗ r + 1) (2*r+1)*(2*r+1)(2r+1)(2r+1 ) matrix, output.
Time complexity:O (n 4) O (n^4)O n4

100 points:

Consider wide search, first put 1 11 Put it in the queue and expand. Finally, use the two-dimensional prefix sum to calculate(2 ∗ r + 1) ∗ (2 ∗ r + 1) (2*r+1)*(2*r+1)(2r+1)(2r+1 ) matrix, output.
Time complexity:O (n 2) O (n^2)O n2

Code:

#include<bits/stdc++.h> //码风已毒瘤,勿喷
#define INF 0x7ffffff
#define rg register
#define F(i,a,b,c) for(rg int i=(a);i<=(b);i+=(c))
#define Fu(i,a,b) F(i,a,b,1)
#define Fd(i,a,b) for(rg int i=(a);i>=(b);i--)
#define clear(x) memset(x,0,sizeof(x))
#define fre(x) freopen(#x".in","r",stdin),freopen(#x".out","w",stdout)
#define close() fclose(stdin),fclose(stdout)
using namespace std;
int t,n,ri,a[155][155],f[155][155],sum[155][155],bj[155][155],q[100005][3],l,r;
int way[4][2]={
    
    {
    
    0,-1},{
    
    0,1},{
    
    1,0},{
    
    -1,0}};
int main() {
    
    
	fre(city);
	scanf("%d",&t);
	Fu(k,1,t){
    
    
		clear(f),clear(sum),clear(bj),clear(q),l=r=0;
		scanf("%d%d",&n,&ri);
		Fu(i,1,n){
    
    
			Fu(j,1,n){
    
    
				scanf("%d",&a[i][j]);
				if(a[i][j]==1)
				q[++r][1]=i,q[r][2]=j,bj[i][j]=1;
			}
		}
		while(l<r){
    
    
			l++;
			Fu(i,0,3){
    
    
				int xx=q[l][1]+way[i][0],yy=q[l][2]+way[i][1];
				if(xx>=1&&xx<=n&&yy>=1&&yy<=n&&bj[xx][yy]==0){
    
    
					r++;
					bj[xx][yy]=1;
					f[xx][yy]=q[l][0]+1,q[r][0]=q[l][0]+1,q[r][1]=xx,q[r][2]=yy;
				}
			}
		}
		Fu(i,1,n){
    
    
			int u=0;
			Fu(j,1,n){
    
    
				u+=f[i][j];
				sum[i][j]=sum[i-1][j]+u;
			}
		}
		Fu(i,1,n){
    
    
			Fu(j,1,n){
    
    
				int a=max(i-ri,1),b=max(j-ri,1),aa=min(i+ri,n),bb=min(j+ri,n);
				printf("%d ",sum[aa][bb]+sum[a-1][b-1]-sum[a-1][bb]-sum[aa][b-1]);
			}
			printf("\n");
		}
		printf("\n");
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/zhy_Learn/article/details/109413646