编程语言
首页 > 编程语言> > [Java] 202104-2

[Java] 202104-2

作者:互联网

import java.io.*;
import java.util.*;

class Main {
	private int n;
	private int[][] A;
	private int[][] mat;

	private int neighbor(int I, int J, int r)
	{
		int sum = 0;
		for (int i = I - r; i <= I + r; i++)
		{
			for (int j = J - r; j <= J + r; j++)
			{
				if (i >= 0 && j >= 0 && i < n && j < n)
				{
					sum += A[i][j];
				}
			}
		}
		return sum;
	}

	private int build(int I, int J, int r)
	{
		int ans = mat[I - 1][J];
		if (I - 1 - r >= 0)
		{
			for (int j = J - r; j <= J + r; j++)
			{
				if (j >= 0 && j < n)
				{
					ans -= A[I - 1 - r][j];
				}
			}
		}	
		if (I + r < n)
		{
			for (int j = J - r; j <= J + r; j++)
			{
				if (j >= 0 && j < n)
				{
					ans += A[I + r][j];
				}
			}
		}
		return ans;
	}

	private void count(int r)
	{
		for (int j = 0; j < n; j++)
		{
			mat[0][j] = neighbor(0, j, r);
		}
		for (int i = 1; i < n; i++)
		{
			for (int j = 0; j < n; j++)
			{
				mat[i][j] = build(i, j, r);
			}
		}
	}

	private double result(int i, int j, int r)
	{
		int u_r = Math.min(r, j - 0);
		int d_r = Math.min(r, n - 1 - j);
		int l_r = Math.min(r, i - 0);
		int r_r = Math.min(r, n - 1 - i);
		int amount = (u_r + d_r + 1) * (l_r + r_r + 1);
		return 1.0 * mat[i][j] / amount;
	}

	public Main() throws IOException
	{
		BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
		PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));

		StringTokenizer st = new StringTokenizer(f.readLine());
		this.n = Integer.parseInt(st.nextToken());
		int L = Integer.parseInt(st.nextToken());
		int r = Integer.parseInt(st.nextToken());
		int t = Integer.parseInt(st.nextToken());

		this.A = new int[n][n];
		this.mat = new int[n][n];
		for (int i = 0; i < n; i++)
		{
			st = new StringTokenizer(f.readLine());
			for(int j = 0; j < n; j++)
			{
				A[i][j] = Integer.parseInt(st.nextToken());
			}
		}

		count(r);
		/*
		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j < n; j++)
			{
				out.print(mat[i][j] + " ");
			}
			out.println();
		}
		*/

		int ans = 0;
		for (int i = 0; i < n; i++)
		{
			for(int j = 0; j < n; j++)
			{
				if (result(i, j, r) <= t)
				{
					ans++;
				}
			}
		}
		out.println(ans);
		out.close();
		f.close();
	}
	public static void main(String[] args) throws IOException
	{
		new Main();
	}
}

标签:Java,mat,int,private,++,ans,new,202104
来源: https://blog.csdn.net/weixin_41714373/article/details/115603151