Official

B - Bombs Editorial by en_translator


Implement the following algorithm with a for statement.

  1. Prepare a boolean array \(v\) of shape \(R\times C\), initialized with False.
  2. For each square \((i,j)\) with a bomb, do the following.
    • For each square \((k,l)\) that the explosion of the bomb at \((i,j)\) affects, let \(v[k][l]\) be True.
  3. For each square \((i,j)\) such that \(v[i][j]\) is True, let \(B[i][j]\) be ..

Note that you should not directly set \(B[k][l]\) to . in the step 2 above because, if \((k, l)\) has a bomb, you may ignore the effect of that bomb.

Sample code (C++):

#include<bits/stdc++.h>

using namespace std;

int main() {
    int r, c;
    cin >> r >> c;
    vector <string> b(r);
    for (string &s: b) cin >> s;
    
    vector blasted(r, vector<bool>(c));
    for (int i = 0; i < r; i++) {
        for (int j = 0; j < c; j++) {
            if (!isdigit(b[i][j])) continue;
            int power = b[i][j] - '0';
            for (int ni = 0; ni < r; ni++) {
                for (int nj = 0; nj < c; nj++) {
                    if (abs(i - ni) + abs(j - nj) <= power) blasted[ni][nj] = true;
                }
            }
        }
    }
    
    for (int i = 0; i < r; i++) {
        for (int j = 0; j < c; j++) {
            if (blasted[i][j]) b[i][j] = '.';
        }
    }
    
    for (string &s: b) cout << s << '\n';
}

posted:
last update: