Problem solution · C++

ABC424 D — 2x2 Erasing 2

ABC424 D — 2x2 Erasing 2: a C++ solution using dynamic programming. Learn the idea, check the complexity, and read the full code, with credit to michimani AtCoder Solutions.

Technique
Dynamic programming
Source
michimani AtCoder Solutions
Length
62 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Dynamic programming

For ABC424 D — 2x2 Erasing 2, the implementation records answers for smaller states and reuses them to build the requested result without repeating work.

  1. Define precisely what one DP state represents.
  2. Establish the base cases before transitions are evaluated.
  3. Process states in dependency order and combine only already-known values.

Code notes

  • 62 lines of C++ from the credited upstream file abc424_d.cpp.
  • The implementation visibly relies on sequence storage, cached states.
  • 8 loop blocks detected.

Complexity

Multiply the number of reachable states by the work performed for each transition, then include the stored state table in memory usage.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from michimani AtCoder Solutions by michimani and is used under the MIT licence.

Full codeABC424 D — 2x2 Erasing 2 · C++C++
Use this to learn the idea, then write your own version.
#include <algorithm>#include <iostream>#include <vector> using namespace std; int main() {    int t;    cin >> t;     while (t--) {        int h, w;        cin >> h >> w;         vector<string> g(h);        for (int i = 0; i < h; i++) cin >> g[i];         vector<vector<int>> dp(h + 1, vector<int>(1 << w, 1e9));        dp[0][0] = 0;         for (int i = 0; i < h; i++) {            for (int prev_mask = 0; prev_mask < (1 << w); prev_mask++) {                if (dp[i][prev_mask] == 1e9) continue;                 for (int cur_mask = 0; cur_mask < (1 << w); cur_mask++) {                    int changes = 0;                    bool valid = true;                     for (int j = 0; j < w; j++) {                        if (g[i][j] == '#' && !(cur_mask & (1 << j))) changes++;                    }                     if (i > 0) {                        for (int j = 0; j < w - 1; j++) {                            bool tl = (prev_mask & (1 << j)) != 0;                            bool tr = (prev_mask & (1 << (j + 1))) != 0;                            bool bl = (cur_mask & (1 << j)) != 0;                            bool br = (cur_mask & (1 << (j + 1))) != 0;                             if (tl && tr && bl && br) {                                valid = false;                                break;                            }                        }                    }                     if (valid)                        dp[i + 1][cur_mask] = min(dp[i + 1][cur_mask],                                                  dp[i][prev_mask] + changes);                }            }        }         int ans = 1e9;        for (int mask = 0; mask < (1 << w); mask++) ans = min(ans, dp[h][mask]);         cout << ans << endl;    }     return 0;} 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗