SHOW:
|
|
- or go back to the newest paste.
1 | //Author - Saurav Kalsoor | |
2 | - | //House Renovation - JAVA |
2 | + | //House Renovation - KOTLIN |
3 | ||
4 | - | import java.util.*; |
4 | + | import java.util.* |
5 | import java.lang.* | |
6 | - | public class Test { |
6 | + | |
7 | - | |
7 | + | var sc: Scanner = Scanner(System.`in`) |
8 | - | static Scanner sc = new Scanner(System.in); |
8 | + | var area = 0 |
9 | - | static int area = 0; |
9 | + | |
10 | - | |
10 | + | fun main() { |
11 | - | |
11 | + | val n: Int = sc.nextInt() |
12 | - | public static void dfs(int[][] a,int i,int j,Boolean[][] visited) { |
12 | + | val m: Int = sc.nextInt() |
13 | - | if(visited[i][j]) return; |
13 | + | val q: Int = sc.nextInt() |
14 | - | |
14 | + | val a = Array(n) { IntArray(m) } |
15 | - | visited[i][j] = true; |
15 | + | for (i in 0 until n) { |
16 | - | area++; |
16 | + | for (j in 0 until m) { |
17 | - | if((a[i][j] & 1)==0) { |
17 | + | a[i][j] = sc.nextInt() |
18 | - | dfs(a,i,j-1,visited); |
18 | + | |
19 | - | } |
19 | + | |
20 | - | if((a[i][j] & 2)==0) { |
20 | + | val queries = IntArray(q) |
21 | - | dfs(a,i+1,j,visited); |
21 | + | for (i in queries.indices) { |
22 | - | } |
22 | + | queries[i] = sc.nextInt() |
23 | - | if((a[i][j] & 4)==0) { |
23 | + | |
24 | - | dfs(a,i,j+1,visited); |
24 | + | val result = houseRenovation(n, m, q, a, queries) |
25 | - | } |
25 | + | for (i in result.indices) { |
26 | - | if((a[i][j] & 8)==0) { |
26 | + | print(result[i].toString() + " ") |
27 | - | dfs(a,i-1,j,visited); |
27 | + | |
28 | - | } |
28 | + | println() |
29 | } | |
30 | - | |
30 | + | |
31 | - | public static int[] houseRenovation(int n, int m, int q, int[][] a,int[] queries){ |
31 | + | fun dfs(a: Array<IntArray>, i: Int, j: Int, visited: Array<BooleanArray>) { |
32 | - | Boolean[][] visited = new Boolean[n][m]; |
32 | + | if (visited[i][j]) return |
33 | - | |
33 | + | visited[i][j] = true |
34 | - | for(int i=0;i<n;i++) { |
34 | + | area++ |
35 | - | for(int j=0;j<m;j++) { |
35 | + | if (a[i][j] and 1 == 0) { |
36 | - | visited[i][j] = false; |
36 | + | dfs(a, i, j - 1, visited) |
37 | } | |
38 | if (a[i][j] and 2 == 0) { | |
39 | - | |
39 | + | dfs(a, i + 1, j, visited) |
40 | - | Map<Integer,Integer> hm = new HashMap<Integer,Integer>(); |
40 | + | |
41 | - | |
41 | + | if (a[i][j] and 4 == 0) { |
42 | - | for(int i=0;i<n;i++) { |
42 | + | dfs(a, i, j + 1, visited) |
43 | - | for(int j=0;j<m;j++) { |
43 | + | |
44 | - | if(visited[i][j] == false) { |
44 | + | if (a[i][j] and 8 == 0) { |
45 | - | area = 0; |
45 | + | dfs(a, i - 1, j, visited) |
46 | - | dfs(a,i,j,visited); |
46 | + | |
47 | - | hm.put(area,hm.getOrDefault(area, 0)+1); |
47 | + | } |
48 | - | } |
48 | + | |
49 | fun houseRenovation(n: Int, m: Int, q: Int, a: Array<IntArray>, queries: IntArray): IntArray { | |
50 | val visited = Array(n){BooleanArray(m)} | |
51 | - | |
51 | + | // val a = Array(n) { IntArray(m) } |
52 | - | int[] result = new int[q]; |
52 | + | for (i in 0 until n) { |
53 | - | for(int i=0;i<queries.length;i++) { |
53 | + | for (j in 0 until m) { |
54 | - | if(hm.containsKey(queries[i])) { |
54 | + | visited[i][j] = false |
55 | - | result[i] = hm.get(queries[i]); |
55 | + | |
56 | - | } |
56 | + | |
57 | - | else { |
57 | + | val hm: MutableMap<Int, Int> = HashMap<Int, Int>() |
58 | - | result[i] = 0; |
58 | + | for (i in 0 until n) { |
59 | - | } |
59 | + | for (j in 0 until m) { |
60 | if (visited[i][j] == false) { | |
61 | - | |
61 | + | area = 0 |
62 | - | return result; |
62 | + | dfs(a, i, j, visited) |
63 | hm.put(area, hm.getOrDefault(area, 0) + 1) | |
64 | - | |
64 | + | |
65 | - | public static void main(String[] args) { |
65 | + | |
66 | - | int n = sc.nextInt(); |
66 | + | |
67 | - | int m = sc.nextInt(); |
67 | + | val result = IntArray(q) |
68 | - | int q = sc.nextInt(); |
68 | + | for (i in queries.indices) { |
69 | - | |
69 | + | if (hm.containsKey(queries[i])) { |
70 | - | int[][] a = new int[n][m]; |
70 | + | result[i] = hm[queries[i]]!! |
71 | - | for(int i=0;i<n;i++) { |
71 | + | } else { |
72 | - | for(int j=0;j<m;j++) { |
72 | + | result[i] = 0 |
73 | - | a[i][j] = sc.nextInt(); |
73 | + | |
74 | } | |
75 | return result | |
76 | - | |
76 | + | } |
77 | - | int[] queries = new int[q]; |
77 | + |