Blue Bridge Cup official website practice questions (blurred image)

Question description

Xiaolan has a black and white image, consisting of n×m pixels, with n rows from top to bottom and �m columns from left to right in each row. Each pixel is represented by a grayscale value between 0 and 255.

Now, Xiaolan is ready to blur the image. The operation method is:

For each pixel, sum up all the pixels in a 3×3 area centered on it (which may be 9 pixels or less) and divide it by the number of pixels in this range (rounded down) to get The value is the blurred result.

Please note that each pixel must be calculated and summed using the gray value in the original image.

Enter description

The first line of input contains two integers n, m.

Each line from line 2 to line n+1 contains m integers, representing the gray value of each pixel, and adjacent integers are separated by a space.

Among them, 1≤n,m≤100.

Output description

Output n lines, each line contains m integers, and adjacent integers are separated by spaces to represent the blurred image.

Input and output samples

Example 1

enter

3 4
0 0 0 255
0 0 255 0
0 30 255 255

output

0 42 85 127
5 60 116 170
7 90 132 191

operating restrictions

  • Maximum running time: 1s
  • Maximum running memory: 128M
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n=scan.nextInt();
        int m=scan.nextInt();
        int[][] a=new int[n+2][m+2];
        int[][] ans=new int[n+2][m+2];
        for(int i=0;i<=n+1;i++){
            for(int j=0;j<=m+1;j++){
                a[i][j]=266;
            }
        }
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                a[i][j]=scan.nextInt();
            }
        }
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                int sum=0,count=0;;
                if(a[i-1][j-1]!=266){ //1
                    sum=sum+a[i-1][j-1];
                    count++;
                }
                if(a[i-1][j]!=266){ //2
                    sum=sum+a[i-1][j];
                    count++;
                }
                if(a[i-1][j+1]!=266){ //3
                    sum=sum+a[i-1][j+1];
                    count++;
                }
                if(a[i][j-1]!=266){//4
                    sum=sum+a[i][j-1];
                    count++;
                }
                if(a[i][j]!=266){ //5
                    sum=sum+a[i][j];
                    count++;
                }
                if(a[i][j+1]!=266){ //6
                    sum=sum+a[i][j+1];
                    count++;
                }
                if(a[i+1][j-1]!=266){ //7
                    sum=sum+a[i+1][j-1];
                    count++;
                }
                if(a[i+1][j]!=266){ //8
                    sum=sum+a[i+1][j];
                    count++;
                }
                if(a[i+1][j+1]!=266){ //9
                    sum=sum+a[i+1][j+1];
                    count++;
                }
                int ans1=(int)Math.floor(sum/count);
                ans[i][j]=ans1;
            }
        }
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                System.out.printf(ans[i][j]+" ");
            }
            System.out.println();
        }
        scan.close();
    }
}

Guess you like

Origin blog.csdn.net/s44Sc21/article/details/132784164