Guest User

Untitled

a guest
Mar 17th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. class Node {
  4. public int area;
  5. public int row;
  6. public int col;
  7.  
  8. public Node(int area, int row, int col) {
  9. this.area = area;
  10. this.row = row;
  11. this.col = col;
  12. }
  13. }
  14.  
  15. class Solution {
  16. static class Compare implements Comparator<Node> {
  17. public int compare(Node one, Node two) {
  18. if (one.area != two.area) {
  19. return Integer.compare(one.area, two.area);
  20. } else {
  21. return Integer.compare(one.row, two.row);
  22. }
  23. }
  24. }
  25.  
  26. public static void main(String args[]) throws Exception {
  27. Scanner sc = new Scanner(System.in);
  28. int T;
  29. T = sc.nextInt();
  30. for (int test_case = 1; test_case <= T; test_case++) {
  31. int n = sc.nextInt();
  32. int[][] map = new int[n][n];
  33. for (int a = 0; a < n; a++) {
  34. for (int b = 0; b < n; b++) {
  35. map[a][b] = sc.nextInt();
  36. }
  37. }
  38. boolean[][] checked = new boolean[n][n];
  39. int count = 0;
  40. Compare cmp = new Compare();
  41. PriorityQueue<Node> pq = new PriorityQueue<Node>(1, cmp);
  42. for (int a = 0; a < n; a++) {
  43. for (int b = 0; b < n; b++) {
  44. if (map[a][b] != 0 && !checked[a][b]) {
  45. count++;
  46. int row = rowsolve(a, b, map, n);
  47. int col = colsolve(a, b, map, n);
  48. int area = row * col;
  49. for (int c = 0; c < row; c++) {
  50. for (int d = 0; d < col; d++) {
  51. checked[a + c][b + d] = true;
  52. }
  53. }
  54. pq.add(new Node(area, row, col));
  55. }
  56. }
  57. }
  58. System.out.print("#" + test_case + " " + count + " ");
  59. while (!pq.isEmpty()) {
  60. Node no = pq.poll();
  61. System.out.print(no.row + " " + no.col+" ");
  62. }
  63. System.out.println();
  64. }
  65. sc.close();
  66. }
  67.  
  68. private static int colsolve(int a, int b, int[][] map, int n) {
  69. if (b == n) {
  70. return 0;
  71. } else {
  72. if (map[a][b + 1] != 0) {
  73. return colsolve(a, b + 1, map, n) + 1;
  74. } else {
  75. return 1;
  76. }
  77. }
  78. }
  79.  
  80. private static int rowsolve(int a, int b, int[][] map, int n) {
  81. if (a == n) {
  82. return 0;
  83. } else {
  84. if (map[a + 1][b] != 0) {
  85. return rowsolve(a + 1, b, map, n) + 1;
  86. } else {
  87. return 1;
  88. }
  89. }
  90. }
  91. }
Add Comment
Please, Sign In to add comment