Japan Plotter Driver

Interview

Time limit1sMemory limit128 MB

Summary
Emulate a plotter's POINT, TEXT, LINE, CLEAR, and PRINT commands on an ASCII grid, merging overlapping characters by fixed rules, and frame each finished picture.
Level

Medium7 of 10

Topics
Simulation, Implementation, Matrix, Math
Solved
No attempts yet

Problem

The company you work for produces plotter devices that can draw nice pictures. To support customers who do not own the special hardware, you have been asked to write an emulation driver that simulates the plotter and prints its picture on a computer screen.

The plotter is driven by a simple language of a few drawing commands:

  • POINT x y — makes a little circle at the given coordinates.
  • TEXT x y txt — displays a line of text at the given coordinates.
  • LINE x1 y1 x2 y2 — draws a line between the two specified points.
  • CLEAR x1 y1 x2 y2 — erases the given rectangle.
  • PRINT — prints an output page and terminates the current job.

The emulation driver represents the picture with a few ASCII characters, one character being the basic unit of the coordinate system. The top-left character has coordinates (1, 1). The X-axis points right and the Y-axis points down.

The commands are emulated as follows:

  • POINT: the driver puts a lowercase letter o at the given coordinates.
  • TEXT: shows a single line of text; the first character is placed at the given coordinates and the text always runs to the right.
  • LINE: simulates a straight line between the two points. The line is formed by one of the characters dash (-), pipe (|), slash (/), or backslash (\), according to its direction.
  • CLEAR: fills the rectangular area with spaces, including the bounding rows and columns.
  • PRINT: prints the picture surrounded by a neat frame made of plus (+), minus (-), and pipe (|) characters.

When several objects are drawn onto a single character, the following rules apply:

  • If the same character is drawn several times, it is used unchanged.
  • If only pipe and minus characters are involved, the result is a plus sign (+).
  • If only slashes and backslashes are involved, the result is a lowercase letter x.
  • Otherwise, an asterisk (*) is displayed.

Before a script is given to the driver, a special preprocessor rejects every invalid command. You may therefore assume that all coordinates lie within the page. Also, in a LINE command the two points are always different and the line is strictly vertical, horizontal, or at an angle of 45° to the axes. No assumption is made about the relative position of the two points in the LINE and CLEAR commands. The text in a TEXT command is always composed only of uppercase letters and digits.

Input

The input consists of several scripts. Each script begins with a line containing two integers X and Y separated by a space (1≤X,Y≤751 \le X, Y \le 75); these numbers give the dimensions of the page. Every following line contains exactly one of the commands above. The commands are always uppercase and their arguments are separated by a single space.

The PRINT command is always the last command of a script. A new script begins after the PRINT command. The input is terminated by two zeros, which do not form a script.

Output

For each script, output the emulated picture created as specified above. Separate two consecutive pictures with one blank line.

Examples1

  1. Example 1

    Input
    20 10
    LINE 3 2 11 10
    LINE 3 10 11 2
    LINE 20 3 8 3
    TEXT 6 8 TEST
    LINE 19 1 19 10
    LINE 17 10 17 1
    LINE 16 1 16 10
    LINE 13 6 20 6
    CLEAR 20 5 15 7
    LINE 18 1 18 10
    TEXT 12 10 NICEPICTURE
    POINT 1 1
    POINT 3 2
    PRINT
    1 1
    POINT 1 1
    CLEAR 1 1 1 1
    PRINT
    3 3
    LINE 2 1 2 3
    LINE 1 2 3 2
    LINE 2 3 2 1
    LINE 3 2 1 2
    LINE 2 1 2 3
    LINE 1 2 3 2
    PRINT
    0 0
    
    Expected output
    +--------------------+
    |o              |||| |
    |  *       /    |||| |
    |   \   --*-----++++-|
    |    \   /      |||| |
    |     \ /         |  |
    |      x     --   |  |
    |     / \         |  |
    |    /TES*      |||| |
    |   /     \     |||| |
    |  /       \NICE****U|
    +--------------------+
    
    +-+
    | |
    +-+
    
    +---+
    | | |
    |-+-|
    | | |
    +---+