Official

B - Draw Frame Editorial by en_translator


A cell \((i,j)\) is on the border if and only if:

  • \(i=1\),
  • \(i=H\),
  • \(j=1\), or
  • \(j=W\).

Use this property to determine the \(H\times W\) characters, and print them.

The following is sample code.

#include <iostream>
using namespace std;

int main() {
    int H, W;
    cin >> H >> W;

    for (int i = 0; i < H; ++i) {
        for (int j = 0; j < W; ++j) {
            if (i == 0 || i == H - 1 || j == 0|| j == W - 1) { // If it is a cell on the border
                cout << '#'; // print #
            } else { // otherwise
                cout << '.'; // print #
            }
        }
        cout << endl; // Add a newline
    }

    return 0;
}
H, W = map(int, input().split())

for i in range(H):
    for j in range(W):
        if i == 0 or i == H - 1 or j == 0 or j == W - 1: # If it is a cell on the border
            print('#', end='') # print #
        else: # otherwise
            print('.', end='') # print .
        print('')

posted:
last update: