[Java] 202104-2

2021/4/11 20:29:18

本文主要是介绍[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] 202104-2的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程