Google CodeJam 2022 – Qualification – 1. Punched Cards
Problem
A secret team of programmers is plotting to disrupt the programming language landscape and bring punched cards back by introducing a new language called Punched Card Python that lets people code in Python using punched cards! Like good disrupters, they are going to launch a viral campaign to promote their new language before even having the design for a prototype. For the campaign, they want to draw punched cards of different sizes in ASCII art.
The ASCII art of a punched card they want to draw is similar to an R×C matrix without the top-left cell. That means, it has (R⋅C)−1 cells in total. Each cell is drawn in ASCII art as a period (.
) surrounded by dashes (-
) above and below, pipes (|
) to the left and right, and plus signs (+
) for each corner. Adjacent cells share the common characters in the border. Periods (.
) are used to align the cells in the top row.
For example, the following is a punched card with R=3 rows and C=4 columns:
..+-+-+-+ ..|.|.|.| +-+-+-+-+ |.|.|.|.| +-+-+-+-+ |.|.|.|.| +-+-+-+-+
There are more examples with other sizes in the samples below. Given the integers R and C describing the size of a punched card, print the ASCII art drawing of it as described above.
Input
The first line of the input gives the number of test cases, T. T lines follow, each describing a different test case with two integers R and C: the number of rows and columns of the punched card that must be drawn.
Output
For each test case, output one line containing Case #x:
, where x is the test case number (starting from 1). Then, output (2⋅R)+1 additional lines with the ASCII art drawing of a punched card with R rows and C columns.
Limits
Time limit: 5 seconds.
Memory limit: 1 GB.
Test Set 1 (Visible Verdict)
1≤T≤81
2≤R≤10
2≤C≤10
Sample input
3
3 4
2 2
2 3
Sample output
Case #1:
..+-+-+-+
..|.|.|.|
+-+-+-+-+
|.|.|.|.|
+-+-+-+-+
|.|.|.|.|
+-+-+-+-+
Case #2:
..+-+
..|.|
+-+-+
|.|.|
+-+-+
Case #3:
..+-+-+
..|.|.|
+-+-+-+
|.|.|.|
+-+-+-+
Sample Case #1 is the one described in the problem statement. Sample Cases #2 and #3 are additional examples. Notice that the output for each case contains exactly R⋅C+3 periods.
Solution
There are multiple approaches to solve this easy task. I chose to compute the actual number of rows and columns in the output as (N-1)*2+3 and (M-1)*2+3 and then for each pair of indices i and j determine the rule which maps the pair (i,j) into a character on that spot.
There was no need to store the output matrix, the characters are printed immediately while iterating.
#include <iostream> #include <vector> using namespace std; int main(){ int t, q = 0; cin >> t; while (q++ < t) { int n, m, i, j; cin >> n >> m; // compute the actual number of rows in the output n = (n - 1) * 2 + 3; m = (m - 1) * 2 + 3; cout << "Case #" << q << ":" << endl; for (i=0;i<n;i++) { for (j=0;j<m;j++) { // handle top left corner if (i < 2 && j < 2) { cout << "."; continue; } // handle intersection coordinates if (i % 2 == 0 && j % 2 == 0) { cout << "+"; continue; } // handle pipes if (i % 2 == 0 && j % 2 == 1) cout << "-"; if (i % 2 == 1 && j % 2 == 0) cout << "|"; // everything else if ((i + j) % 2 == 0) cout << "."; } cout << endl; } } return 0; }