Problem p01226 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;

int main() {
  int t;
  cin >> t;
  for (; t--;) {
    int h, w;
    cin >> h >> w;
    //      cout<<h<<" "<<w<<endl;
    char maps[h][w];
    int x, y;
    //      char move;     //^=0,>=1,v=2,<=3
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++) {
        cin >> maps[i][j];

        if (maps[i][j] == '^' || maps[i][j] == '>' || maps[i][j] == 'v' ||
            maps[i][j] == '<') {
          x = i;
          y = j;
        }
      }
    int n;
    cin >> n;
    for (; n--;) {
      char tmp;
      cin >> tmp;
      if (tmp == 'U') {
        maps[x][y] = '^';
        if (x > 0 && maps[x - 1][y] == '.') {
          maps[x][y] = '.';
          x--;
          maps[x][y] = '^';
        }
      }
      if (tmp == 'D') {
        maps[x][y] = 'v';
        if (x < h - 1 && maps[x + 1][y] == '.') {
          maps[x][y] = '.';
          x++;
          maps[x][y] = 'v';
        }
      }

      if (tmp == 'L') {
        maps[x][y] = '<';
        if (y > 0 && maps[x][y - 1] == '.') {
          maps[x][y] = '.';
          y--;
          maps[x][y] = '<';
        }
      }
      if (tmp == 'R') {
        maps[x][y] = '>';
        if (y < w - 1 && maps[x][y + 1] == '.') {
          maps[x][y] = '.';
          y++;
          maps[x][y] = '>';
        }
      }
      if (tmp == 'S') {
        int dx = 0, dy = 0;
        int tmpx = x, tmpy = y;
        if (maps[x][y] == '^')
          dx = -1;
        else if (maps[x][y] == 'v')
          dx = 1;
        else if (maps[x][y] == '<')
          dy = -1;
        else
          dy = 1;
        while (tmpx + dx < h && tmpy + dy < w) {
          tmpx += dx;
          tmpy += dy;
          if (maps[tmpx][tmpy] == '*') {
            maps[tmpx][tmpy] = '.';
            break;
          }
          if (maps[tmpx][tmpy] == '#') {
            break;
          }
        }
      }
    }

    for (int i = 0; i < h; i++, cout << endl)
      for (int j = 0; j < w; j++) {
        cout << maps[i][j];
      }
    if (t != 0)
      cout << endl;
  }
}

Fixed submission:
#include <iostream>
using namespace std;

int main() {
  int t;
  cin >> t;
  for (; t--;) {
    int h, w;
    cin >> h >> w;
    //      cout<<h<<" "<<w<<endl;
    char maps[h][w];
    int x, y;
    //      char move;     //^=0,>=1,v=2,<=3
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++) {
        cin >> maps[i][j];

        if (maps[i][j] == '^' || maps[i][j] == '>' || maps[i][j] == 'v' ||
            maps[i][j] == '<') {
          x = i;
          y = j;
        }
      }
    int n;
    cin >> n;
    for (; n--;) {
      char tmp;
      cin >> tmp;
      if (tmp == 'U') {
        maps[x][y] = '^';
        if (x > 0 && maps[x - 1][y] == '.') {
          maps[x][y] = '.';
          x--;
          maps[x][y] = '^';
        }
      }
      if (tmp == 'D') {
        maps[x][y] = 'v';
        if (x < h - 1 && maps[x + 1][y] == '.') {
          maps[x][y] = '.';
          x++;
          maps[x][y] = 'v';
        }
      }

      if (tmp == 'L') {
        maps[x][y] = '<';
        if (y > 0 && maps[x][y - 1] == '.') {
          maps[x][y] = '.';
          y--;
          maps[x][y] = '<';
        }
      }
      if (tmp == 'R') {
        maps[x][y] = '>';
        if (y < w - 1 && maps[x][y + 1] == '.') {
          maps[x][y] = '.';
          y++;
          maps[x][y] = '>';
        }
      }
      if (tmp == 'S') {
        int dx = 0, dy = 0;
        int tmpx = x, tmpy = y;
        if (maps[x][y] == '^')
          dx = -1;
        else if (maps[x][y] == 'v')
          dx = 1;
        else if (maps[x][y] == '<')
          dy = -1;
        else
          dy = 1;
        while (tmpx + dx < h && tmpy + dy < w && tmpx + dx >= 0 &&
               tmpy + dy >= 0) {
          tmpx += dx;
          tmpy += dy;
          if (maps[tmpx][tmpy] == '*') {
            maps[tmpx][tmpy] = '.';
            break;
          }
          if (maps[tmpx][tmpy] == '#') {
            break;
          }
        }
      }
    }

    for (int i = 0; i < h; i++, cout << endl)
      for (int j = 0; j < w; j++) {
        cout << maps[i][j];
      }
    if (t != 0)
      cout << endl;
  }
}