ibragimova_mariam

Фермер (Динамика, max квадрат из 1)

May 3rd, 2020
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. import java.io.*;
  2. import java.math.BigInteger;
  3. import java.util.*;
  4.  
  5. import static java.lang.Math.max;
  6. import static java.lang.Math.min;
  7.  
  8. class Pair<A, B> {
  9. A fst;
  10. B snd;
  11.  
  12. public Pair(A fst, B snd) {
  13. this.fst = fst;
  14. this.snd = snd;
  15. }
  16.  
  17. public A getFst() {
  18. return fst;
  19. }
  20.  
  21. public B getSnd() {
  22. return snd;
  23. }
  24. }
  25.  
  26. public class Main {
  27.  
  28. public static void main(String[] args) throws IOException {
  29. //Scanner sc = new Scanner(System.in);//new File("lis.in"));//System.in);
  30. BufferedReader bd = new BufferedReader(new InputStreamReader(System.in));
  31.  
  32. int n = Integer.parseInt(bd.readLine());
  33.  
  34. String[] a = new String[n];
  35. for (int i = 0; i < n; i++) {
  36. a[i] = bd.readLine();
  37. }
  38.  
  39. int[][] s = new int[n + 1][n + 1];
  40. for (int i = 1; i <= n; i++) {
  41. for (int j = 1; j <= n; j++)
  42. {
  43. s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + (a[i - 1].charAt(j - 1) == '0' ? 0 : 1);
  44. }
  45. }
  46.  
  47. long ans = 0;
  48. long currentSquareSum;
  49. for (int k = 1; k <= n; k++)
  50. {
  51. for (int r = 0; r < n - k + 1; r++)
  52. {
  53. for (int c = 0; c < n - k + 1; c++)
  54. {
  55. currentSquareSum = s[r + k][c + k] - s[r + k][c] - s[r][c + k] + s[r][c];
  56. if (k * k == currentSquareSum)
  57. ans = currentSquareSum;
  58. }
  59. }
  60. }
  61. System.out.println(ans);
  62. }
  63. }
Add Comment
Please, Sign In to add comment