Problem p01226 (C++) — buggy submission, verdict: Runtime Error

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

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

int direction(char c) {
  int dir;
  if (c == '^')
    dir = 1;
  else if (c == 'v')
    dir = 0;
  else if (c == '>')
    dir = 3;
  else
    dir = 2;
  return dir;
}

int main() {
  int T;
  cin >> T;
  for (int t = 0; t < T; t++) {
    int H, W, N;
    vector<string> board;
    string command;
    cin >> H >> W;
    for (int i = 0; i < H; i++) {
      string s;
      cin >> s;
      board.push_back(s);
    }
    cin >> N >> command;
    char c;
    int x, y;

    for (int i = 0; i < board.size(); i++) {
      for (int j = 0; j < board[i].size(); j++) {
        c = board[i][j];
        if (c == '^' || c == 'v' || c == '>' || c == '<') {
          x = j;
          y = i;
        }
      }
    }
    vector<string> res = board;
    for (int i = 0; i < command.size(); i++) {
      if (command[i] == 'S') {
        int dir = direction(res[y][x]);
        int nx = x + dx[dir], ny = y + dy[dir];
        while (0 <= nx && nx < W && 0 <= ny && ny < H) {
          if (res[ny][nx] == '*') {
            res[ny][nx] = '.';
            break;
          } else if (res[ny][nx] == '#') {
            break;
          }

          nx += dx[dir];
          ny += dy[dir];
        }
      } else {
        int c = command[i];
        if (c == 'U') {
          if (y > 0 && res[y - 1][x] == '.') {
            res[y][x] = '.';
            y--;
          }
          res[y][x] = '^';
        } else if (c == 'D') {
          if (y < H - 1 && res[y + 1][x] == '.') {
            res[y][x] = '.';
            y++;
          }
          res[y][x] = 'v';
        } else if (c == 'R') {
          if (x < W - 1 && res[y][x + 1] == '.') {
            res[y][x] = '.';
            x++;
          }
          res[y][x] = '>';
        } else {
          if (x > 0 && res[y][x - 1] == '.') {
            res[y][x] = '.';
            x--;
          }
          res[y][x] = '<';
        }
      }
    }

    if (t)
      cout << endl;
    for (int i = 0; i < res.size(); i++)
      cout << res[i] << endl;
  }
}

Error: 0

Fixed submission:

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

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

int direction(char c) {
  int dir;
  if (c == '^')
    dir = 1;
  else if (c == 'v')
    dir = 0;
  else if (c == '>')
    dir = 3;
  else
    dir = 2;
  return dir;
}

int main() {
  int T;
  cin >> T;
  for (int t = 0; t < T; t++) {
    int H, W, N;
    vector<string> board;
    string command;
    cin >> H >> W;
    for (int i = 0; i < H; i++) {
      string s;
      cin >> s;
      board.push_back(s);
    }
    cin >> N >> command;
    char c;
    int x, y;

    for (int i = 0; i < board.size(); i++) {
      for (int j = 0; j < board.size(); j++) {
        c = board[i][j];
        if (c == '^' || c == 'v' || c == '>' || c == '<') {
          x = j;
          y = i;
        }
      }
    }
    vector<string> res = board;
    for (int i = 0; i < command.size(); i++) {
      if (command[i] == 'S') {
        int dir = direction(res[y][x]);
        int nx = x + dx[dir], ny = y + dy[dir];
        while (0 <= nx && nx < W && 0 <= ny && ny < H) {
          if (res[ny][nx] == '*') {
            res[ny][nx] = '.';
            break;
          } else if (res[ny][nx] == '#') {
            break;
          }

          nx += dx[dir];
          ny += dy[dir];
        }
      } else {
        int c = command[i];
        if (c == 'U') {
          if (y > 0 && res[y - 1][x] == '.') {
            res[y][x] = '.';
            y--;
          }
          res[y][x] = '^';
        } else if (c == 'D') {
          if (y < H - 1 && res[y + 1][x] == '.') {
            res[y][x] = '.';
            y++;
          }
          res[y][x] = 'v';
        } else if (c == 'R') {
          if (x < W - 1 && res[y][x + 1] == '.') {
            res[y][x] = '.';
            x++;
          }
          res[y][x] = '>';
        } else {
          if (x > 0 && res[y][x - 1] == '.') {
            res[y][x] = '.';
            x--;
          }
          res[y][x] = '<';
        }
      }

      //					cout << x << "," << y << endl;
    }

    if (t)
      cout << endl;
    for (int i = 0; i < res.size(); i++)
      cout << res[i] << endl;
  }
}