Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- class Node {
- public int area;
- public int row;
- public int col;
- public Node(int area, int row, int col) {
- this.area = area;
- this.row = row;
- this.col = col;
- }
- }
- class Solution {
- static class Compare implements Comparator<Node> {
- public int compare(Node one, Node two) {
- if (one.area != two.area) {
- return Integer.compare(one.area, two.area);
- } else {
- return Integer.compare(one.row, two.row);
- }
- }
- }
- public static void main(String args[]) throws Exception {
- Scanner sc = new Scanner(System.in);
- int T;
- T = sc.nextInt();
- for (int test_case = 1; test_case <= T; test_case++) {
- int n = sc.nextInt();
- int[][] map = new int[n][n];
- for (int a = 0; a < n; a++) {
- for (int b = 0; b < n; b++) {
- map[a][b] = sc.nextInt();
- }
- }
- boolean[][] checked = new boolean[n][n];
- int count = 0;
- Compare cmp = new Compare();
- PriorityQueue<Node> pq = new PriorityQueue<Node>(1, cmp);
- for (int a = 0; a < n; a++) {
- for (int b = 0; b < n; b++) {
- if (map[a][b] != 0 && !checked[a][b]) {
- count++;
- int row = rowsolve(a, b, map, n);
- int col = colsolve(a, b, map, n);
- int area = row * col;
- for (int c = 0; c < row; c++) {
- for (int d = 0; d < col; d++) {
- checked[a + c][b + d] = true;
- }
- }
- pq.add(new Node(area, row, col));
- }
- }
- }
- System.out.print("#" + test_case + " " + count + " ");
- while (!pq.isEmpty()) {
- Node no = pq.poll();
- System.out.print(no.row + " " + no.col+" ");
- }
- System.out.println();
- }
- sc.close();
- }
- private static int colsolve(int a, int b, int[][] map, int n) {
- if (b == n) {
- return 0;
- } else {
- if (map[a][b + 1] != 0) {
- return colsolve(a, b + 1, map, n) + 1;
- } else {
- return 1;
- }
- }
- }
- private static int rowsolve(int a, int b, int[][] map, int n) {
- if (a == n) {
- return 0;
- } else {
- if (map[a + 1][b] != 0) {
- return rowsolve(a + 1, b, map, n) + 1;
- } else {
- return 1;
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment