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