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

#include <iostream>
#include <string>
using namespace std;

int w, h, tx, ty, d;
int dx[] = {0, 0, 1, -1};
int dy[] = {-1, 1, 0, 0};
char dir[256];
string f[21];

void solve(string s) {
  for (int j = 0; j < (int)s.size(); j++) {
    char c = s[j];
    if (c == 'S') {
      int sx = tx;
      int sy = ty;
      for (;;) {
        int mx = sx + dx[dir[d]];
        int my = sy + dy[dir[d]];
        if (mx < 0 || my < 0 || mx >= w || my >= h) {
          break;
        } else if (f[my][mx] == '.' || f[my][mx] == '-') {
          sx = mx;
          sy = my;
        } else if (f[my][mx] == '*') {
          f[my][mx] = '.';
          break;
        } else if (f[my][mx] == '#') {
          break;
        }
      }
    } else {
      d = dir[c];
      int mx = tx + dx[dir[d]];
      int my = ty + dy[dir[d]];
      if (mx < 0 || my < 0 || mx >= w || my >= h) {
        continue;
      } else if (f[my][mx] == '.') {
        tx = mx;
        ty = my;
      }
    }
  }
}

int main() {
  int t, n;
  string s;

  dir['U'] = '^';
  dir['D'] = 'v';
  dir['R'] = '>';
  dir['L'] = '<';
  dir['^'] = 0;
  dir['v'] = 1;
  dir['>'] = 2;
  dir['<'] = 3;

  cin >> t;
  for (int i = 0; i < t; i++) {
    cin >> h >> w;
    for (int y = 0; y < h; y++) {
      cin >> f[y];
      for (int x = 0; x < w; x++) {
        char c = f[y][x];
        if (c == '>' || c == '<' || c == '^' || c == 'v') {
          tx = x;
          ty = y;
          d = c;
          f[y][x] = '.';
        }
      }
    }
    cin >> n >> s;
    solve(s);
    f[ty][tx] = d;
    for (int y = 0; y < h; y++) {
      cout << f[y] << endl;
    }
    if (i != t - 1)
      cout << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>
using namespace std;

int w, h, tx, ty, d;
int dx[] = {0, 0, 1, -1};
int dy[] = {-1, 1, 0, 0};
char dir[256];
string f[21];

void solve(string s) {
  for (int j = 0; j < (int)s.size(); j++) {
    char c = s[j];
    if (c == 'S') {
      int sx = tx;
      int sy = ty;
      for (;;) {
        int mx = sx + dx[dir[d]];
        int my = sy + dy[dir[d]];
        if (mx < 0 || my < 0 || mx > w || my > h) {
          break;
        } else if (f[my][mx] == '.' || f[my][mx] == '-') {
          sx = mx;
          sy = my;
        } else if (f[my][mx] == '*') {
          f[my][mx] = '.';
          break;
        } else if (f[my][mx] == '#') {
          break;
        }
      }
    } else {
      d = dir[c];
      int mx = tx + dx[dir[d]];
      int my = ty + dy[dir[d]];
      if (mx < 0 || my < 0 || mx > w || my > h) {
        continue;
      } else if (f[my][mx] == '.') {
        tx = mx;
        ty = my;
      }
    }
  }
}

int main() {
  int t, n;
  string s;

  dir['U'] = '^';
  dir['D'] = 'v';
  dir['R'] = '>';
  dir['L'] = '<';
  dir['^'] = 0;
  dir['v'] = 1;
  dir['>'] = 2;
  dir['<'] = 3;

  cin >> t;
  for (int i = 0; i < t; i++) {
    cin >> h >> w;
    for (int y = 0; y < h; y++) {
      cin >> f[y];
    }
    for (int y = 0; y < h; y++) {
      for (int x = 0; x < w; x++) {
        char c = f[y][x];
        if (c == '>' || c == '<' || c == '^' || c == 'v') {
          tx = x;
          ty = y;
          d = c;
          f[y][x] = '.';
        }
      }
    }
    cin >> n;
    cin >> s;
    solve(s);
    f[ty][tx] = d;
    for (int y = 0; y < h; y++) {
      cout << f[y] << endl;
    }
    if (i != t - 1)
      cout << endl;
  }
}