Algorithm


Problem Name: 2 AD-HOC - beecrowd | 1357

Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/1357

In Braille

 

By Vinicius Santos Brazil

Timelimit: 1

The Braille system, designed by Louis Braille in 1825, revolutionized written communication for blind and visually impaired persons. Braille, a blind Frenchman, developed a tactile language where each element is represented by a cell with six dot positions, arranged in three rows and two columns. Each dot position can be raised or not, allowing for 64 different configurations which can be felt by trained fingers. The figure below shows the Braille representation for the decimal digits (a black dot indicates a raised position).

In order to develop a new software system to help teachers to deal with blind or visual impaired students, a Braille dictionary module is necessary. Given a message, composed only by digits, your job is to translate it to or from Braille. Can you help?

 

Input

 

Each test case is described using three or five lines. The first line contains an integer D representing the number of digits in the message (1 ≤ D ≤ 100). The second line contains a single uppercase letter 'S' or 'B'. If the letter is 'S', the next line contains a message composed of D decimal digits that your program must translate to Braille. If the letter is 'B', the next three lines contain a message composed of D Braille cells that your program must translate from Braille. Braille cells are separated by single spaces. In each Braille cell a raised position is denoted by the character '*' (asterisk), while a not raised position is denoted by the character '.' (dot).

The last test case is followed by a line containing one zero.

 

Output

 

For each test case print just the digits of the corresponding translation, in the same format as the input (see the examples for further clarification).

 

 

 

Sample Input Sample Output

10
S
1234567890
3
B
*. *. **
.. *. ..
.. .. ..
2
S
00
0

*. *. ** ** *. ** ** *. .* .*
.. *. .. .* .* *. ** ** *. **
.. .. .. .. .. .. .. .. .. ..
123
.* .*
** **
.. ..

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming


#include <cstring>
#include <iostream>
#include <string>

using namespace std;

int main()
{
    ios::sync_with_stdio(false);
    int d;
    string cod[3][20];
    for (int i = 0; i  <  3; ++i) {
        for (int j = 0; j  <  20; j += 2) {
            if ((j == 2 || j == 4 || j == 10 || j == 16) && i == 0) {
                cod[i][j] = '*';
                cod[i][j + 1] = '.';
            }
            if ((j == 6 || j == 8 || j == 12 || j == 14) && i == 0) {
                cod[i][j] = '*';
                cod[i][j + 1] = '*';
            }
            if ((j == 18 || j == 0) && i == 0) {
                cod[i][j] = '.';
                cod[i][j + 1] = '*';
            }
            if ((j == 2 || j == 6) && i == 1) {
                cod[i][j] = '.';
                cod[i][j + 1] = '.';
            }
            if ((j == 4 || j == 12 || j == 18) && i == 1) {
                cod[i][j] = '*';
                cod[i][j + 1] = '.';
            }
            if ((j == 8 || j == 10) && i == 1) {
                cod[i][j] = '.';
                cod[i][j + 1] = '*';
            }
            if ((j == 14 || j == 16 || j == 0) && i == 1) {
                cod[i][j] = '*';
                cod[i][j + 1] = '*';
            }
            if (i == 2) {
                cod[i][j] = '.';
                cod[i][j + 1] = '.';
            }
        }
    }
    for (cin >> d; d != 0; cin >> d) {
        char l;
        string num, bra[3];
        string::iterator str;
        cin.ignore();
        cin >> l;
        if (l == 'S') {
            cin.ignore();
            getline(cin, num);
            str = num.begin();
            for (int i = 0; i  <  3; ++i) {
                int p = 0;
                char resp[30] = "";
                for (str = num.begin(); str != num.end(); str++) {
                    if (p > 0)
                        cout << " ";
                    if ((*str) == '1') {
                        cout << cod[i][2];
                        cout << cod[i][3];
                    }
                    if ((*str) == '2') {
                        cout << cod[i][4];
                        cout << cod[i][5];
                    }
                    if ((*str) == '3') {
                        cout << cod[i][6];
                        cout << cod[i][7];
                    }
                    if ((*str) == '4') {
                        cout << cod[i][8];
                        cout << cod[i][9];
                    }
                    if ((*str) == '5') {
                        cout << cod[i][10];
                        cout << cod[i][11];
                    }
                    if ((*str) == '6') {
                        cout << cod[i][12];
                        cout << cod[i][13];
                    }
                    if ((*str) == '7') {
                        cout << cod[i][14];
                        cout << cod[i][15];
                    }
                    if ((*str) == '8') {
                        cout << cod[i][16];
                        cout << cod[i][17];
                    }
                    if ((*str) == '9') {
                        cout << cod[i][18];
                        cout << cod[i][19];
                    }
                    if ((*str) == '0') {
                        cout << cod[i][0];
                        cout << cod[i][1];
                    }
                    p++;
                }
                cout << "\n";
            }
        } else {
            cin.ignore();
            for (int i = 0; i  <  3; ++i)
                getline(cin, bra[i]);
            int a = 0;
            for (int j = 0; j  <  bra[a].size(); j += 3) {
                if (bra[a][j] == '*' && bra[a][j + 1] == '.') {
                    if (bra[a + 1][j] == '.' && bra[a + 1][j + 1] == '.')
                        cout << "1";
                    if (bra[a + 1][j] == '*' && bra[a + 1][j + 1] == '.')
                        cout << "2";
                    if (bra[a + 1][j] == '.' && bra[a + 1][j + 1] == '*')
                        cout << "5";
                    if (bra[a + 1][j] == '*' && bra[a + 1][j + 1] == '*')
                        cout << "8";
                }
                if (bra[a][j] == '*' && bra[a][j + 1] == '*') {
                    if (bra[a + 1][j] == '.' && bra[a + 1][j + 1] == '.')
                        cout << "3";
                    if (bra[a + 1][j] == '.' && bra[a + 1][j + 1] == '*')
                        cout << "4";
                    if (bra[a + 1][j] == '*' && bra[a + 1][j + 1] == '.')
                        cout << "6";
                    if (bra[a + 1][j] == '*' && bra[a + 1][j + 1] == '*')
                        cout << "7";
                }
                if (bra[a][j] == '.' && bra[a][j + 1] == '*') {
                    if (bra[a + 1][j] == '*' && bra[a + 1][j + 1] == '.')
                        cout << "9";
                    if (bra[a + 1][j] == '*' && bra[a + 1][j + 1] == '*')
                        cout << "0";
                }
            }
            cout << "\n";
        }
    }
    return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
10
S
1234567890
3
B
*. *. **
.. *. ..
.. .. ..
2
S
00
0

Output

x
+
cmd
*. *. ** ** *. ** ** *. .* .*
.. *. .. .* .* *. ** ** *. **
.. .. .. .. .. .. .. .. .. ..
123
.* .*
** **
.. ..

#2 Code Example with Javascript Programming

Code - Javascript Programming


const { readFileSync } = require("fs")
const lines = readFileSync("/dev/stdin", "utf8").split("\n")

const input = (function* (lines) {
	for (const line of lines) yield line
})(lines)


class BrailleDigits {
	static #DIGITS_BRAILLE_PLANNED = Object.freeze({
		1: "*.....",
		2: "*.*...",
		3: "**....",
		4: "**.*..",
		5: "*..*..",
		6: "***...",
		7: "****..",
		8: "*.**..",
		9: ".**...",
		0: ".***.."
	})

	// Protected Like
	static get digits() {
		return BrailleDigits.#DIGITS_BRAILLE_PLANNED
	}
}


class BrailleDigitEncoder extends BrailleDigits {
	/** @param {string} digits */

	static fromDigits(digits) {
		const plannedSymbols = digits.split("").map((digit) => {
			return (this.digits[digit] ?? "")
				.replace(/([.*]{1,2})([.*]{1,2})([.*]{1,2})/, "$1-$2-$3")
				.split("-")
		})

		const LevelPlaceholder = new Array(digits.length).fill("") // Improving performance creating once

		// Levels
		const L1 = LevelPlaceholder.map((_, i) => plannedSymbols[i][0]).join(" ")
		const L2 = LevelPlaceholder.map((_, i) => plannedSymbols[i][1]).join(" ")
		const L3 = LevelPlaceholder.map((_, i) => plannedSymbols[i][2]).join(" ")

		return [L1, L2, L3].join("\n")
	}
}


class BrailleDigitDecoder extends BrailleDigits {

	/** @param {string[][]} brailles  */

	static toDigits(brailles) {
		const len = brailles[0].length
		const digitsPlainedSymbols = new Array(len).fill("")

		for (let i = 0; i  <  len; i++) {
			digitsPlainedSymbols[i] += brailles[0][i]
			digitsPlainedSymbols[i] += brailles[1][i]
			digitsPlainedSymbols[i] += brailles[2][i]
		}

		const Bentries = Object.entries(this.digits)

		const decodedText = digitsPlainedSymbols.map((sym) => {
			const [digit] = Bentries.find(([, Bsymbol]) => sym == Bsymbol) ?? [""]
			return digit
		})

		return decodedText
	}
}


function main() {
	const responses = []

	for (let size = input.next(); size.done == false && size.value != "0"; size = input.next()) {
		const code = input.next().value

		if (code == "S") {
			const digits = input.next().value
			responses.push(BrailleDigitEncoder.fromDigits(digits))
		} else if (code == "B") {

			const braille = [
				input.next().value.split(" "),
				input.next().value.split(" "),
				input.next().value.split(" ")
			]

			responses.push(BrailleDigitDecoder.toDigits(braille).join(""))
		}
	}

	console.log(responses.join("\n"))
}

main()
Copy The Code & Try With Live Editor

Input

x
+
cmd
10
S
1234567890
3
B
*. *. **
.. *. ..
.. .. ..
2
S
00
0

Output

x
+
cmd
*. *. ** ** *. ** ** *. .* .*
.. *. .. .* .* *. ** ** *. **
.. .. .. .. .. .. .. .. .. ..
123
.* .*
** **
.. ..
Advertisements

Demonstration


Previous
#1343 Beecrowd Online Judge Solution 1343 Runner Pawns Solution in C, C++, Java, Js and Python
Next
#1366 Beecrowd Online Judge Solution 1366 Sticks Game Solution in C, C++, Java, Js and Python