AcWing 753. 平方矩阵 I

gao79138 / 2023-05-03 / 原文

AcWing 753. 平方矩阵 I

1. 地址

    https://www.acwing.com/problem/content/755/

2. 题解

#include <iostream>
#include <cstdio>
#include <cmath>

using namespace std;

int main(){
    int m[101][101];
    //该题的思路:
    //判断每个点距离上下左右四条边的最小值,该最小值就是这个点的值。
    //注意:这里使用坐标从0开始
    //设矩阵为4x4且当前点为(0,0) 那么,该点距离上边的值为0,距离左边的值为0,距离下边的值为3,距离右边的值为3
    //转换成一般形式:矩阵为nxn且当前点为(i,j).那么距离上边的距离为i,距离左边的值为j,距离下边的值为n-i-1,距离右边的值为n-j-1
    //最后,由于下标从0开始,因此最终结果需要+1
    while(true){
        int n;
        scanf("%d",&n);
        if(n == 0){
            break;
        }
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                int top = i;
                int bottom = n-i-1;
                int left = j;
                int right = n-j-1;
                m[i][j] = min(min(top,bottom),min(left,right))+1;
                printf("%d ",m[i][j]);
            }
            printf("\n");
        }
        printf("\n");
        
    }
    
    return 0;
}