Read the Chessboard
InterviewTime limit1sMemory limit128 MB
Given an ASCII-art chessboard, list each side's pieces in standard notation, grouped by type in a fixed order and sorted by position rules.
- Level
Easy3 of 10
- Topics
- Implementation, String, Sorting, Simulation
- Solved
- No attempts yet
Problem
Read a picture of a chessboard position and print it in standard chess notation.
Input
The input is an ASCII-art picture of a chessboard with pieces placed on it. White's pieces are shown as upper-case letters and Black's pieces as lower-case letters. Each letter is one of "K" (King), "Q" (Queen), "R" (Rook), "B" (Bishop), "N" (Knight), or "P" (Pawn). The board outline is drawn with plus ("+"), minus ("-"), and pipe ("|") characters. Black squares are filled with colons (":") and white squares with dots (".").
Output
Print two lines. The first line is the string "White: " followed by the description of White's pieces. The second line is the string "Black: " followed by the description of Black's pieces.
The description is a comma-separated list of terms, one per piece. Each term is a single upper-case letter for the piece type ("K", "Q", "R", "B", "N"; the letter is omitted for pawns), immediately followed by the piece's position in standard chess notation: a lower-case column letter from "a" to "h" ("a" is the leftmost column of the input) and a single row digit from 1 to 8 (row 8 is the top row of the input).
Within each player's list the pieces must appear in this order: King ("K"), Queens ("Q"), Rooks ("R"), Bishops ("B"), Knights ("N"), then pawns. Because of captures and promotions the piece counts may differ from the initial position. When two pieces of the same type occur, White's pieces are ordered with the smaller row number first and Black's pieces with the larger row number first; if two such pieces share a row, the one with the smaller column letter comes first.