[Programmers / JAVA] Level 1 Secret Map (17681)
[Programmers / JAVA] Level 1 Secret Map (17681)
| Rank | Language Used |
|---|---|
| Level 1 | 🖼️ JAVA |
Neo obtained a secret map that will tell him the location where Frodo usually hides his stash of emergency cash. But this secret map is encrypted with numbers, so to check the location, he needs to decrypt the code. Fortunately, he also found a memo explaining how to decrypt the map's code.
- The map is a square array with a side length of n, and each cell consists of either "blank" (" ") or "wall" ("#").
- The full map can be obtained by overlaying two maps. Let's call them "Map 1" and "Map 2". If either Map 1 or Map 2 has a wall at a given cell, that cell is also a wall on the full map. A cell that is blank in both Map 1 and Map 2 is also blank on the full map.
- "Map 1" and "Map 2" are each encrypted as integer arrays.
- The encrypted array is obtained by encoding each row of the map into a binary number, where wall cells are 1 and blank cells are 0, then taking the value of that binary number.
Write a program to help Neo decrypt the secret map's code so he can get his hands on Frodo's stash.
The input consists of the map's side length n and two integer arrays, arr1 and arr2.
- 1 ≦ n ≦ 16
- arr1 and arr2 are given as integer arrays of length n.
- When each element x of the integer array is converted to binary, its length is at most n. That is, 0 ≦ x ≦ 2n - 1.
Decrypt the original secret map and output it as an array of strings made up of '#' and blanks.
| Parameter | Value |
|---|---|
| n | 5 |
| arr1 | { 9, 20, 28, 18, 11 } |
| arr2 | { 30, 1, 21, 17, 28 } |
| output | { "#####","# # #", "### #", "# ##", "#####" } |
| Parameter | Value |
|---|---|
| n | 6 |
| arr1 | { 46, 33, 33, 22, 31, 50 } |
| arr2 | { 27, 56, 19, 14, 14, 10 } |
| output | { "######", "### #", "## ##", " #### ", " #####", "### # " } |
There are two maps, and to reveal the actual map, we need to overlay them. If either map has a wall (#) at a given cell, that cell is a wall.
Since 10101 -> # # #, we can treat # = 1 and blank = 0. If either value is 1 (#), that spot becomes 1 (#), so this is essentially implementing an OR operation.
| a | b | value |
|---|---|---|
| 1 | 1 | 1 |
| 1 | 0 | 1 |
| 0 | 1 | 1 |
| 0 | 0 | 0 |
An OR operation results in 1 if either a or b is 1.
All that's left is to convert the decimal number to binary, then replace 1 with # and 0 with a blank.
JAVA
private int[] dec2bin(int n, int num) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[n - i - 1] = num % 2; num /= 2; } return arr; }
The decimal-to-binary conversion method looks like the above. It converts the decimal number num into an n-digit binary number and returns it as an array.
Based on the returned array's values, split them into # and blanks to build the string.
JAVA
/** * Secret Map class * * @author RWB * @since 2021.12.12 Sun 16:54:09 */ class Solution { /** * Method that returns the answer * * @param n: [int] map size * @param arr1: [int[]] map 1 * @param arr2: [int[]] map 2 * * @return [String[]] answer */ public String[] solution(int n, int[] arr1, int[] arr2) { String[] map = new String[n]; for (int i = 0; i < n; i++) { int[] map1 = dec2bin(n, arr1[i]); int[] map2 = dec2bin(n, arr2[i]); StringBuilder builder = new StringBuilder(); for (int j = 0; j < n; j++) { // If either map has a wall at that spot, it's a wall (#) builder.append((map1[j] | map2[j]) == 1 ? "#" : " "); } map[i] = builder.toString(); } return map; } /** * Decimal-to-binary array conversion method * * @param n: [int] size * @param num: [int] decimal number * * @return [int[]] binary array */ private int[] dec2bin(int n, int num) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[n - i - 1] = num % 2; num /= 2; } return arr; } }
