Problem p00156 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define mp make_pair

template <class T> void pv(T a, T b) {
  for (T i = a; i != b; ++i)
    cout << *i << " ";
  cout << endl;
}
template <class T> void chmin(T &t, T f) {
  if (t > f)
    t = f;
}
template <class T> void chmax(T &t, T f) {
  if (t < f)
    t = f;
}
int in() {
  int x;
  scanf("%d", &x);
  return x;
}

vector<string> m;
vector<vint> res;

int main() {
  int h, w;
  while (cin >> w >> h, h) {
    m.clear();
    res.clear();
    int i, j;
    vint buf;
    for (i = 0; i < w; i++)
      buf.push_back(100100100);
    for (i = 0; i < h; i++) {
      string str;
      cin >> str;
      m.push_back(str);
      res.push_back(buf);
    }
    for (i = 0; i < w; i++)
      res[0][i] = res[h - 1][i] = 0;
    for (i = 0; i < h; i++)
      res[i][0] = res[i][w - 1] = 0;
    bool update = true;
    int xx[] = {1, -1, 0, 0}, yy[] = {0, 0, 1, -1};
    int k;
    while (update) {
      update = false;
      for (i = 1; i < h - 1; i++) {
        for (j = 1; j < w - 1; j++) {
          for (k = 0; k < 4; k++) {
            if (m[i + yy[k]][j + xx[k]] == '#' && m[i][j] != '#') {
              if (res[i + yy[k]][j + xx[k]] + 1 < res[i][j]) {
                res[i][j] = res[i + yy[k]][j + xx[k]] + 1;
                update = true;
              }
            } else {
              if (res[i + yy[k]][j + xx[k]] < res[i][j]) {
                res[i][j] = res[i + yy[k]][j + xx[k]];
                update = true;
              }
            }
          }
        }
      }
    }
    for (i = 0; i < h; i++) {
      for (j = 0; j < w; j++) {
        // cout<<res[i][j]<<" ";
        if (m[i][j] == '&') {
          cout << res[i][j] << endl;
        }
      }
      // cout<<endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define mp make_pair

template <class T> void pv(T a, T b) {
  for (T i = a; i != b; ++i)
    cout << *i << " ";
  cout << endl;
}
template <class T> void chmin(T &t, T f) {
  if (t > f)
    t = f;
}
template <class T> void chmax(T &t, T f) {
  if (t < f)
    t = f;
}
int in() {
  int x;
  scanf("%d", &x);
  return x;
}

char mt[110][110];
int res[110][110];

int main() {
  int n, m;
  while (n = in(), m = in()) {
    int i, j;
    for (i = 0; i < m; i++) {
      for (j = 0; j < n; j++) {
        cin >> mt[i][j];
        res[i][j] = 1000000000;
      }
    }
    int k;
    for (k = 0; k < n * m; k++) {
      for (i = 0; i < m; i++) {
        for (j = 0; j < n; j++) {
          if (i == 0 || j == 0 || i == m - 1 || j == n - 1) {
            if (mt[i][j] == '#')
              res[i][j] = 1;
            else
              res[i][j] = 0;
          } else {
            int p;
            int xx[] = {1, -1, 0, 0};
            int yy[] = {0, 0, 1, -1};
            for (p = 0; p < 4; p++) {
              if (0 <= i + yy[p] && i + yy[p] < m && 0 <= j + xx[p] &&
                  j + xx[p] < n) {
                if (mt[i][j] == '#')
                  chmin(res[i][j], res[i + yy[p]][j + xx[p]] + 1);
                else
                  chmin(res[i][j], res[i + yy[p]][j + xx[p]]);
              }
            }
          }
        }
      }
    }
    for (i = 0; i < m; i++) {
      for (j = 0; j < n; j++) {
        if (mt[i][j] == '&')
          cout << res[i][j] << endl;
        // cout<<res[i][j];
      }
      // cout<<endl;
    }
  }
  return 0;
}