Problem p03725 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
// #define MOD 998244353
#define INF 1000000010
#define EPS 1e-9
#define F first
#define S second

#define debug(x) cout << x << endl;
#define repi(i, x, n) for (int i = x; i < n; i++)
#define rep(i, n) repi(i, 0, n)
#define lp(i, n) repi(i, 0, n)
#define repn(i, n) for (int i = n; i >= 0; i--)
#define int long long
#define endl "\n"

typedef pair<int, int> PII;
typedef pair<int, string> PIS;
typedef pair<string, int> PSI;

int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int h, w, k;
  cin >> h >> w >> k;
  char mp[h][w];
  PII mpp[h][w]; // first->残り移動回数 second->残り壁枚数
  int ans[h][w];
  rep(i, h) {
    rep(j, w) { ans[i][j] = 0; }
  }
  int ANS = INF;
  PII st;
  queue<PII> bfs, bfs2;
  rep(i, h) {
    rep(j, w) {
      cin >> mp[i][j];
      if (mp[i][j] == 'S') {
        st.F = i;
        st.S = j;
        bfs.push(st);
        mpp[i][j] = make_pair(k, 0);
        ans[i][j] = 1;
      }
    }
  }
  while (!bfs.empty()) {
    PII p = bfs.front();
    bfs.pop();
    bfs2.push(p);
    rep(i, 4) {
      int X = p.F + dx[i], Y = p.S + dy[i];
      if (X >= 0 && X < h && Y >= 0 && Y < w) {
        if (mp[X][Y] == '.') {
          if (mpp[p.F][p.S].F != 0) {
            mpp[X][Y].F = mpp[p.F][p.S].F - 1;
            mpp[X][Y].S = mpp[p.F][p.S].S;
            ans[X][Y] = ans[p.F][p.S];
            bfs.push(make_pair(X, Y));
          }
        }
      }
    }
  }
  while (!bfs2.empty()) {
    PII p = bfs2.front();
    bfs2.pop();
    rep(i, 4) {
      int X = p.F + dx[i], Y = p.S + dy[i];
      if (X >= 0 && X < h && Y >= 0 && Y < w) {

        if (ans[X][Y] == 0) {
          // cout<<X<<" "<<Y<<endl;
          if (mp[X][Y] == '.') {
            if (mpp[p.F][p.S].F != 0) {
              mpp[X][Y].F = mpp[p.F][p.S].F - 1;
              mpp[X][Y].S = mpp[p.F][p.S].S;
              ans[X][Y] = ans[p.F][p.S];
              bfs2.push(make_pair(X, Y));
            } else {
              mpp[X][Y].F = k - 1;
              mpp[X][Y].S = mpp[p.F][p.S].S + k;
              ans[X][Y] = ans[p.F][p.S] + 1;
              bfs2.push(make_pair(X, Y));
            }
          } else if (mp[X][Y] == '#') {
            if (mpp[p.F][p.S].S != 0) {
              if (mpp[p.F][p.S].F != 0) {
                mpp[X][Y].F = mpp[p.F][p.S].F - 1;
                mpp[X][Y].S = mpp[p.F][p.S].S - 1;
                ans[X][Y] = ans[p.F][p.S];
                bfs2.push(make_pair(X, Y));
              } else {
                mpp[X][Y].F = k - 1;
                mpp[X][Y].S = mpp[p.F][p.S].S + k - 1;
                ans[X][Y] = ans[p.F][p.S] + 1;
                bfs2.push(make_pair(X, Y));
              }
            } else {
              mpp[X][Y].F = k - 1;
              mpp[X][Y].S = mpp[p.F][p.S].S + k - 1;
              ans[X][Y] = ans[p.F][p.S] + 1;
              bfs2.push(make_pair(X, Y));
            }
          }
        }
      }
    }
  }
  /* rep(i,h){
    rep(j,w){
      cout<<ans[i][j]<<" ";
    }
    cout<<endl;
    }*/

  /* rep(i,h){
    rep(j,w){
      cout<<'('<<mpp[i][j].F<<","<<mpp[i][j].S<<") ";
    }
    cout<<endl;
    }*/
  rep(i, h) { ANS = min(ANS, min(ans[i][0], ans[i][w - 1])); }
  rep(i, w) { ANS = min(ANS, min(ans[0][i], ans[h - 1][i])); }
  cout << ANS << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
// #define MOD 998244353
#define INF 1000000010
#define EPS 1e-9
#define F first
#define S second

#define debug(x) cout << x << endl;
#define repi(i, x, n) for (int i = x; i < n; i++)
#define rep(i, n) repi(i, 0, n)
#define lp(i, n) repi(i, 0, n)
#define repn(i, n) for (int i = n; i >= 0; i--)
#define int long long
#define endl "\n"

typedef pair<int, int> PII;
typedef pair<int, string> PIS;
typedef pair<string, int> PSI;

int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int h, w, k;
  cin >> h >> w >> k;
  char mp[h][w];
  PII mpp[h][w]; // first->残り移動回数 second->残り壁枚数
  int ans[h][w];
  rep(i, h) {
    rep(j, w) { ans[i][j] = 0; }
  }
  int ANS = INF;
  PII st;
  queue<PII> bfs, bfs2;
  rep(i, h) {
    rep(j, w) {
      cin >> mp[i][j];
      if (mp[i][j] == 'S') {
        st.F = i;
        st.S = j;
        bfs.push(st);
        mpp[i][j] = make_pair(k, 0);
        ans[i][j] = 1;
      }
    }
  }
  while (!bfs.empty()) {
    PII p = bfs.front();
    bfs.pop();
    bfs2.push(p);
    rep(i, 4) {
      int X = p.F + dx[i], Y = p.S + dy[i];
      if (X >= 0 && X < h && Y >= 0 && Y < w) {
        if (mp[X][Y] == '.') {
          if (mpp[p.F][p.S].F != 0 && ans[X][Y] == 0) {
            mpp[X][Y].F = mpp[p.F][p.S].F - 1;
            mpp[X][Y].S = mpp[p.F][p.S].S;
            ans[X][Y] = ans[p.F][p.S];
            bfs.push(make_pair(X, Y));
          }
        }
      }
    }
  }
  while (!bfs2.empty()) {
    PII p = bfs2.front();
    bfs2.pop();
    rep(i, 4) {
      int X = p.F + dx[i], Y = p.S + dy[i];
      if (X >= 0 && X < h && Y >= 0 && Y < w) {

        if (ans[X][Y] == 0) {
          // cout<<X<<" "<<Y<<endl;
          if (mp[X][Y] == '.') {
            if (mpp[p.F][p.S].F != 0) {
              mpp[X][Y].F = mpp[p.F][p.S].F - 1;
              mpp[X][Y].S = mpp[p.F][p.S].S;
              ans[X][Y] = ans[p.F][p.S];
              bfs2.push(make_pair(X, Y));
            } else {
              mpp[X][Y].F = k - 1;
              mpp[X][Y].S = mpp[p.F][p.S].S + k;
              ans[X][Y] = ans[p.F][p.S] + 1;
              bfs2.push(make_pair(X, Y));
            }
          } else if (mp[X][Y] == '#') {
            if (mpp[p.F][p.S].S != 0) {
              if (mpp[p.F][p.S].F != 0) {
                mpp[X][Y].F = mpp[p.F][p.S].F - 1;
                mpp[X][Y].S = mpp[p.F][p.S].S - 1;
                ans[X][Y] = ans[p.F][p.S];
                bfs2.push(make_pair(X, Y));
              } else {
                mpp[X][Y].F = k - 1;
                mpp[X][Y].S = mpp[p.F][p.S].S + k - 1;
                ans[X][Y] = ans[p.F][p.S] + 1;
                bfs2.push(make_pair(X, Y));
              }
            } else {
              mpp[X][Y].F = k - 1;
              mpp[X][Y].S = mpp[p.F][p.S].S + k - 1;
              ans[X][Y] = ans[p.F][p.S] + 1;
              bfs2.push(make_pair(X, Y));
            }
          }
        }
      }
    }
  }
  /* rep(i,h){
    rep(j,w){
      cout<<ans[i][j]<<" ";
    }
    cout<<endl;
    }*/

  /* rep(i,h){
    rep(j,w){
      cout<<'('<<mpp[i][j].F<<","<<mpp[i][j].S<<") ";
    }
    cout<<endl;
    }*/
  rep(i, h) { ANS = min(ANS, min(ans[i][0], ans[i][w - 1])); }
  rep(i, w) { ANS = min(ANS, min(ans[0][i], ans[h - 1][i])); }
  cout << ANS << endl;
  return 0;
}