ASCII Art
InterviewTime limit1sMemory limit512 MB
Read an N by M grid of RGB triples, apply the given intensity formula, map each value to a character via the threshold table, and print the grid.
- Level
Easy2 of 10
- Topics
- Implementation, Math, Array
- Solved
- No attempts yet
Problem

A picture drawn with ASCII characters, like the one above, is called 'ASCII art'. Common image files (.jpg, .png, and so on) are stored by dividing the image into a two-dimensional grid of pixels at its resolution and keeping each pixel's information. That information has several parts, but the 'R', 'G', and 'B' values indicate how much of each of the three colors 'Red', 'Green', and 'Blue' is mixed in, and each value is an integer between 0 and 255 inclusive.
ASCII art draws a picture by assigning one character to each pixel of a grid, so any image file can be redrawn as ASCII art. Unlike an image file, which can have many colors, ASCII art cannot adjust color and can only adjust the shade within each pixel. If the original image is grayscale, the result can look fairly similar, but if it is made of many colors, it is hard to preserve the feel of the original.
However, if you can convert the original image to grayscale with a filter, and then convert it to ASCII art, the quality can improve. The following shows an example of one image turning into ASCII art.

To convert an image to grayscale, an Intensity function decides the ratio in which the three colors R, G, and B are mixed for each pixel. The Intensity function takes integers R, G, and B between 0 and 255 and returns one integer, and is defined below.
The result of this function is between 0 and 2,550,000 inclusive. A higher value means a pixel closer to white, and a lower value means a pixel closer to black. ASCII art is completed by converting each pixel, which the intensity function turned into a single integer, according to the table below.
If you have followed everything so far, you can build a fine ASCII Art Generator.
Given the resolution of an original image and the R, G, and B values of each pixel, write a program that prints the ASCII art of the image.
Input
The first line gives the height N and the width M of the picture. (1 ≤ N, M ≤ 400)
From the second line to the N+1-th line, each line contains 3M integers.
Among these, the 3j-2-th, 3j-1-th, and 3j-th integers on the i+1-th line mean R(i,j), G(i,j), B(i,j) respectively, and every value is between 0 and 255 inclusive.
Here R(i,j) is the R value of the pixel in row i, column j, G(i,j) is the G value of the pixel in row i, column j, and B(i,j) is the B value of the pixel in row i, column j.
Output
Print the given picture converted to ASCII art, in the form of an N×M grid.
Be careful not to print unneeded spaces at the end of lines.
Hint
The original picture of the sample looks like this.
