Problem p02735 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

typedef pair<int, int> ii;
typedef pair<ll, ll> pll;

int main(int argc, char *argv[]) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int h, w;
  cin >> h >> w;
  vector<vector<bool>> grid(h, vector<bool>(w));
  vector<vector<vector<pair<ii, ii>>>> sol(h, vector<vector<pair<ii, ii>>>(w));

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      char tmp;
      cin >> tmp;
      grid[i][j] = tmp == '#';
    }
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      bool equal = false;
      pair<ii, ii> p1, p2;

      if (i > 0 && j > 0) {
        if (sol[i - 1][j].size() == sol[i][j - 1].size()) {
          if (grid[i][j]) {
            equal = true;
            p1 = sol[i - 1][j].back();
            p2 = sol[i][j - 1].back();
          } else {
            sol[i][j] = sol[i - 1][j];
          }
        } else if (sol[i - 1][j].size() > sol[i][j - 1].size()) {
          sol[i][j] = sol[i][j - 1];
        } else {
          sol[i][j] = sol[i - 1][j];
        }
      } else if (j > 0) {
        sol[i][j] = sol[i][j - 1];
      } else if (i > 0) {
        sol[i][j] = sol[i - 1][j];
      }

      if (grid[i][j]) {
        if (equal) {
          if ((p1.second.first == i - 1 && p1.second.second == j) ||
              (p1.second.first == i && p1.second.second == j - 1)) {
            sol[i][j] = sol[i - 1][j];
            sol[i][j].back() = make_pair(p1.first, ii(i, j));
          } else if ((p2.second.first - 1 == i && p2.second.second == j) ||
                     (p2.second.first == i && p2.second.second == j - 1)) {
            sol[i][j] = sol[i][j - 1];
            sol[i][j].back() = make_pair(p2.first, ii(i, j));
          } else {
            sol[i][j] = sol[i - 1][j];
            sol[i][j].emplace_back(ii(i, j), ii(i, j));
          }
          equal = false;
        } else if (sol[i][j].empty()) {
          sol[i][j].emplace_back(ii(i, j), ii(i, j));
        } else {
          pair<ii, ii> p;
          p = sol[i][j].back();
          if ((p.second.first == i - 1 && p.second.second == j) ||
              (p.second.first == i && p.second.second == j - 1))
            sol[i][j].back() = make_pair(p.first, ii(i, j));
          else
            sol[i][j].emplace_back(ii(i, j), ii(i, j));
        }
      }
    }
  }

  /*
  for ( auto l : sol ) {
          for ( auto v : l ) {
                  cout << v.size() << " ";
          }
          cout << "\n";
  }
  */

  cout << sol.back().back().size() << "\n";

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

typedef pair<int, int> ii;
typedef pair<ll, ll> pll;

int main(int argc, char *argv[]) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int h, w;
  cin >> h >> w;
  vector<vector<bool>> grid(h, vector<bool>(w));
  vector<vector<vector<pair<ii, ii>>>> sol(h, vector<vector<pair<ii, ii>>>(w));

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      char tmp;
      cin >> tmp;
      grid[i][j] = tmp == '#';
    }
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      bool equal = false;
      pair<ii, ii> p1, p2;

      if (i > 0 && j > 0) {
        if (sol[i - 1][j].size() == sol[i][j - 1].size()) {
          if (grid[i][j] && !sol[i - 1][j].empty()) {
            equal = true;
            p1 = sol[i - 1][j].back();
            p2 = sol[i][j - 1].back();
          } else {
            sol[i][j] = sol[i - 1][j];
          }
        } else if (sol[i - 1][j].size() > sol[i][j - 1].size()) {
          sol[i][j] = sol[i][j - 1];
        } else {
          sol[i][j] = sol[i - 1][j];
        }
      } else if (j > 0) {
        sol[i][j] = sol[i][j - 1];
      } else if (i > 0) {
        sol[i][j] = sol[i - 1][j];
      }

      if (grid[i][j]) {
        if (equal) {
          if ((p1.second.first == i - 1 && p1.second.second == j) ||
              (p1.second.first == i && p1.second.second == j - 1)) {
            sol[i][j] = sol[i - 1][j];
            sol[i][j].back() = make_pair(p1.first, ii(i, j));
          } else if ((p2.second.first - 1 == i && p2.second.second == j) ||
                     (p2.second.first == i && p2.second.second == j - 1)) {
            sol[i][j] = sol[i][j - 1];
            sol[i][j].back() = make_pair(p2.first, ii(i, j));
          } else {
            sol[i][j] = sol[i - 1][j];
            sol[i][j].emplace_back(ii(i, j), ii(i, j));
          }
          equal = false;
        } else if (sol[i][j].empty()) {
          sol[i][j].emplace_back(ii(i, j), ii(i, j));
        } else {
          pair<ii, ii> p;
          p = sol[i][j].back();
          if ((p.second.first == i - 1 && p.second.second == j) ||
              (p.second.first == i && p.second.second == j - 1))
            sol[i][j].back() = make_pair(p.first, ii(i, j));
          else
            sol[i][j].emplace_back(ii(i, j), ii(i, j));
        }
      }
    }
  }

  /*
  for ( auto l : sol ) {
          for ( auto v : l ) {
                  cout << v.size() << " ";
          }
          cout << "\n";
  }
  */

  cout << sol.back().back().size() << "\n";

  return 0;
}