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

#include <stdio.h>
#include <string.h>
#include <vector>
#define maxn 111
using namespace std;

int mp[maxn][maxn];
char s[maxn][maxn];
int dir[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
char dd[4][5] = {"E", "S", "W", "N"};
int dic[maxn][maxn][4];

struct I {
  int x, y, d;
};
vector<I> vec;

int H, W;
long long L;

int check(int x, int y) {
  if (x > 0 && x <= H && y > 0 && y <= W && mp[x][y] == 1) {
    return 1;
  }
  return 0;
}
void step(int x, int y, int sd) {

  I tmp;
  tmp.x = x;
  tmp.y = y;
  tmp.d = sd;
  vec.push_back(tmp);

  if (dic[x][y][sd] == 1) {
    return;
  }

  dic[x][y][sd] = 1;

  int xx, yy;
  while (1) {
    xx = x + dir[sd][0];
    yy = y + dir[sd][1];
    if (check(xx, yy)) {
      step(xx, yy, sd);
      break;
    }
    sd = (sd + 1) % 4;
  }
  return;
}

int main() {
  while (scanf("%d%d%lld", &H, &W, &L)) {
    if (H == 0 && W == 0 && L == 0) {
      break;
    }
    memset(mp, 0, sizeof(mp));
    memset(s, 0, sizeof(s));
    for (int i = 0; i < H; i++) {
      scanf("%s", s[i]);
    }
    int si, sj, sdir;
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        if (s[i][j] == '#') {
          mp[i + 1][j + 1] = 0;
        } else {
          mp[i + 1][j + 1] = 1;
          if (s[i][j] != '.') {
            si = i + 1;
            sj = j + 1;
            if (s[i][j] == 'E') {
              sdir = 0;
            } else if (s[i][j] == 'S') {
              sdir = 1;
            } else if (s[i][j] == 'W') {
              sdir = 2;
            } else {
              sdir = 3;
            }
          }
        }
      }
    }
    memset(dic, 0, sizeof(dic));

    // dic[si][sj][sdir] = 1;
    int num = 0;
    vec.clear();
    step(si, sj, sdir);

    int sz = vec.size();
    int t = 0;

    // puts("**************************");
    // printf("%d\\n",sz);

    for (int i = 0; i < sz; i++) {
      if (vec[i].x == vec[sz - 1].x && vec[i].y == vec[sz - 1].y &&
          vec[i].d == vec[sz - 1].d) {
        t = i;
        break;
      }
    }

    int s = sz - t - 1;
    int ans = 0;

    // printf("%d %d\\n",s, t);
    if (L < t) {
      ans = (int)L;
    } else {
      ans = (int)t + (int)((L - t) % s);
    }

    // printf("%d\\n",ans);
    // int ansi = vec[L].x, ansj = vec[L].y, ansd = vec[L].d;

    printf("%d %d %s\\n", vec[ans].x, vec[ans].y, dd[vec[ans].d]);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>
#include <string.h>

int H, W;
long long L;
int Map[105][105];
char vect[] = {'N', 'E', 'S', 'W'};
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};

int main() {
  //   freopen("3.txt", "r", stdin);
  while (scanf("%d %d %lld", &H, &W, &L) != EOF && (H || W || L)) {
    memset(Map, 0, sizeof(Map));
    getchar();
    char vet;
    int x, y;
    char t;
    for (int i = 1; i <= H; ++i) {
      for (int j = 1; j <= W; ++j) {
        scanf("%c", &t);
        if (t != '.' && t != '#') {
          Map[i][j] = 1;
          x = i;
          y = j;
          vet = t;
        }
        if (t == '.') {
          Map[i][j] = 1;
        }
      }
      getchar();
    }
    int now;
    for (int i = 0; i < 4; ++i) {
      if (vect[i] == vet) {
        now = i;
        break;
      }
    }
    int nextx, nexty;
    long long step = 0;
    while (step != L) {
      nextx = x + dx[now];
      nexty = y + dy[now];
      if (Map[nextx][nexty]) {
        ++step;
        x = nextx;
        y = nexty;
        continue;
      } else {
        while (true) {
          now = (now + 1) % 4;
          nextx = x + dx[now];
          nexty = y + dy[now];
          if (Map[nextx][nexty]) {
            break;
          }
        }
        ++step;
        x = nextx;
        y = nexty;
        continue;
      }
    }

    printf("%d %d %c\\n", x, y, vect[now]);
  }
  return 0;
}