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

#include <cmath>
#include <iostream>
#include <strings.h>

using namespace std;

int global_block;
int global_agents[16][9];

int north(int x) {
  if (x < 3 || global_block == x - 3)
    return x;
  else
    return x - 3;
}
int east(int x) {
  if (x % 3 == 2 || global_block == x + 1)
    return x;
  else
    return x + 1;
}
int south(int x) {
  if (x >= 6 || global_block == x + 3)
    return x;
  else
    return x + 3;
}
int west(int x) {
  if (x % 3 == 0 || global_block == x - 1)
    return x;
  else
    return x - 1;
}

double compute(int energy, int start, int target, int block) {
  global_block = block;
  bzero(global_agents, sizeof(int) * 16 * 9);
  global_agents[0][start] = 1;

  for (int i = 0; i < energy; i++) {
    for (int pos = 0; pos < 9; pos++) {
      if (global_agents[i][pos] != 0) {
        global_agents[i + 1][north(pos)] += global_agents[i][pos];
        global_agents[i + 1][west(pos)] += global_agents[i][pos];
        global_agents[i + 1][south(pos)] += global_agents[i][pos];
        global_agents[i + 1][east(pos)] += global_agents[i][pos];
      }
    }
  }

  return pow(0.25, energy) * global_agents[energy][target];
}

int main(void) {
  while (true) {

    int energy;
    cin >> energy;
    if (energy == 0)
      break;
    char start, target, block;
    cin >> start >> target >> block;
    int s = start - 'A';
    int t = target - 'A';
    int b = block - 'A';
    // cout << "start = " << s << ", target = " << t << ", block = " << b <<
    // endl;
    cout << compute(energy, s, t, b) << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>

using namespace std;

double prob[9];
int global_block;

int north(int x) {
  if (x < 3 || global_block == x - 3)
    return x;
  else
    return x - 3;
}
int east(int x) {
  if (x % 3 == 2 || global_block == x + 1)
    return x;
  else
    return x + 1;
}
int south(int x) {
  if (x >= 6 || global_block == x + 3)
    return x;
  else
    return x + 3;
}
int west(int x) {
  if (x % 3 == 0 || global_block == x - 1)
    return x;
  else
    return x - 1;
}

void find(int energy, double p, int pos, int target) {
  // cout << "find(" << energy << "," << p << "," << pos << "," << target << ")"
  // << endl;
  if (energy == 0) {
    prob[pos] += p;
    return;
  }

  find(energy - 1, p * 0.25, north(pos), target);
  find(energy - 1, p * 0.25, west(pos), target);
  find(energy - 1, p * 0.25, south(pos), target);
  find(energy - 1, p * 0.25, east(pos), target);
}

double compute(int energy, int start, int target, int block) {
  for (int i = 0; i < 9; i++)
    prob[i] = 0.0;
  global_block = block;

  find(energy - 1, 0.25, north(start), target);
  find(energy - 1, 0.25, west(start), target);
  find(energy - 1, 0.25, south(start), target);
  find(energy - 1, 0.25, east(start), target);

  return prob[target];
}

int main(void) {
  while (true) {

    int energy;
    cin >> energy;
    if (energy == 0)
      break;
    char start, target, block;
    cin >> start >> target >> block;
    int s = start - 'A';
    int t = target - 'A';
    int b = block - 'A';
    // cout << "start = " << s << ", target = " << t << ", block = " << b <<
    // endl;
    cout << compute(energy, s, t, b) << endl;
  }

  return 0;
}