Problem p01340 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;

int H, W;
char c[10][10];
int gr[10][10];

string dir = "LURD";

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

int getIndex(int x, int y, int dx, int dy) {
  x += dx;
  y += dy;
  while (!(x < 0 || y < 0 || x >= W || y >= H)) {
    if (gr[y][x] != -1 && usd[gr[y][x]] == 0) {
      return gr[y][x];
    }
    x += dx;
    y += dy;
  }
  // cout << x << " " << y << "(" << dx << " " << dy << ")" << endl;
  return -1;
}
int kx[32];
int ky[32];
int done2[32];
int test(int g, int d) {
  if (done2[g]++)
    return 0;
  int ans = 1;
  for (int i = 0; i < 4; i++) {
    if (i == 2)
      continue;
    int p = getIndex(kx[g], ky[g], dx[(d + i) & 3], dy[(d + i) & 3]);
    // cout << g << " " << dir[(d+i)&3] << " " << p << endl;
    if (p == -1)
      continue;
    ans += test(p, (d + i) & 3);
  }
  return ans;
}
char doit[32];
int cnt = 0;

int dfs(int g, int d, int r) {
  if (r == 1) {
    cout << doit << endl;
    exit(0);
  }
  memset(done2, 0, sizeof(done2));
  int w = test(g, d);
  if (w != r) {
    return 0;
  }
  usd[g] = 1;
  for (int i = 0; i < 4; i++) {
    doit[cnt - r] = dir[(d + i) & 3];
    if (i == 2)
      continue;
    int p = getIndex(kx[g], ky[g], dx[(d + i) & 3], dy[(d + i) & 3]);
    if (p == -1)
      continue;
    dfs(p, (d + i) & 3, r - 1);
  }
  usd[g] = 0;
}
int main() {
  memset(gr, -1, sizeof(gr));
  cin >> H >> W;
  int sd, sp;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> c[i][j];
      if (c[i][j] != '.') {
        kx[cnt] = j;
        ky[cnt] = i;
        gr[i][j] = cnt++;

        if (c[i][j] != 'o') {
          sd = dir.find(c[i][j]);
          sp = cnt - 1;
        }
      }
    }
  }
  dfs(sp, sd, cnt);
  while (1) {
  }
}

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

int H, W;
char c[10][10];
int gr[10][10];

string dir = "LURD";

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

int getIndex(int x, int y, int dx, int dy) {
  x += dx;
  y += dy;
  while (!(x < 0 || y < 0 || x >= W || y >= H)) {
    if (gr[y][x] != -1 && usd[gr[y][x]] == 0) {
      return gr[y][x];
    }
    x += dx;
    y += dy;
  }
  // cout << x << " " << y << "(" << dx << " " << dy << ")" << endl;
  return -1;
}
int kx[32];
int ky[32];
int done2[32];
int test(int g, int d) {
  if (done2[g]++)
    return 0;
  int ans = 1;
  for (int i = 0; i < 4; i++) {
    if (i == 2)
      continue;
    int p = getIndex(kx[g], ky[g], dx[(d + i) & 3], dy[(d + i) & 3]);
    // cout << g << " " << dir[(d+i)&3] << " " << p << endl;
    if (p == -1)
      continue;
    ans += test(p, (d + i) & 3);
  }
  return ans;
}
char doit[32];
int cnt = 0;

int dfs(int g, int d, int r) {
  if (r == 1) {
    cout << doit << endl;
    exit(0);
  }
  memset(done2, 0, sizeof(done2));
  int w = test(g, d);
  /*if( w != r ){
          return 0;
  }*/
  usd[g] = 1;
  for (int i = 0; i < 4; i++) {
    doit[cnt - r] = dir[(d + i) & 3];
    if (i == 2)
      continue;
    int p = getIndex(kx[g], ky[g], dx[(d + i) & 3], dy[(d + i) & 3]);
    if (p == -1)
      continue;
    dfs(p, (d + i) & 3, r - 1);
  }
  usd[g] = 0;
}
int main() {
  memset(gr, -1, sizeof(gr));
  cin >> H >> W;
  int sd, sp;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> c[i][j];
      if (c[i][j] != '.') {
        kx[cnt] = j;
        ky[cnt] = i;
        gr[i][j] = cnt++;

        if (c[i][j] != 'o') {
          sd = dir.find(c[i][j]);
          sp = cnt - 1;
        }
      }
    }
  }
  dfs(sp, sd, cnt);
  while (1) {
  }
}