Tutorial
Click here to download the PDF of the problems or click here to see the challenge results.
Contents:
Problem E: Colony Structure
It’s the little details that are vital. Little things make big things happen.
John Wooden
This is a problem about patterns. The trick is to observe the patterns as N
changes and figure out different sections making up the entire structure. The following is the structure when N = 10
.

If we carefully consider a single row, say the fourth line, we can see that there are six dashes, followed by two hashes, followed by three pluses, followed by two hashes, followed by six dashes. The entire structure is made of rows where each row has five sections dashes, pluses, and hashes. The following image illustrates this and counts the elements in each section for each row.

When we divide a problem like this, it becomes easier to conquer individual pieces. We can clearly see the pattern for each section. For instance, from the first row to the last, left dashes decrease from 9 to zero and then increase to 9 again. In terms of N, this is decreasing from N-1 to zero and then increase to N-1 again. The same pattern applies to the right dashes.
The left hashes are simple to formulate their pattern. They are zero in the first and last rows and two for the remaining rows. Similarly, the right hashes are twos except for the first and last two rows where they are ones.
Lastly, the pluses are zeroes in the first and last two rows. From the third row, they increase in steps of twos, from 1 to the middle row. Then, they decrease in steps of twos back to 1.
We can implement the above patterns in the following nested loops. Note that there are many ways the above patterns could be implemented.
int N;
cin >> N;
for(int r = 1; r < 2*N - 2; r++){
//
for(int c = 0; c < abs(N-r-1); c++) cout << "-"; // Left dashes
if(r > 1 && r < 2*N-3) cout << "##"; // Left hashes
for(int c=0; c < 2*N-7 - 2*abs(r+1-N); c++) cout << "*"; // Pluses
if(r > 2 && r < 2*N-4) cout << "##"; // Double right hashes
else cout << "#"; // Single right hashes
for(int c = 0; c < abs(N-r-1); c++) cout << "-"; // Right dashes
cout << endl;
}
Code language: JavaScript (javascript)
One reply on “COVID-19 Challenge Tutorial”
This is awesome and nice platform in the of STEM.