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

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

int main() {

  while (1) {

    int x, y, z;
    cin >> x >> y >> z;
    if (x == 0 && y == 0 && z == 0)
      break;

    // init
    vector<int> mapp(y + 1, 0);
    vector<int> val(y + 1, 0);
    vector<int> v(x);
    for (int i = 0; i < x; i++)
      cin >> v[i];

    vector<int> n(z), e(z), a(z);
    for (int i = 0; i < z; i++) {
      cin >> n[i] >> e[i] >> a[i];
      mapp[n[i]] = e[i];
      if (e[i] == 3)
        a[i] *= -1;
      val[n[i]] = a[i];
    }
    double p = 1.0 / (double)x;

    long double dp[52][5200] = {};
    dp[0][0] = 1.0;

    for (int i = 0; i < y; i++) {
      for (int j = 0; j < y * 100; j++) {
        for (int k = 0; k < x; k++) {

          int ni = i + v[k], nj = j;
          if (ni >= y)
            ni = y;

          if (mapp[ni] == 1)
            ni += val[ni];
          else if (mapp[ni] == 2 || mapp[ni] == 3)
            nj += val[ni];

          if (nj <= 0)
            nj = 0;
          if (ni >= y)
            ni = y;
          dp[ni][nj] += dp[i][j] * p;
        }
      }
    }
    long double ans = 0.0;
    for (int i = 0; i < y * 100 + 1; i++) {
      ans += dp[y][i] * i;
    }
    cout << (int)ans << endl;
  }
}

Error: TLE

Fixed submission:

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

double ans;
vector<int> v;
vector<int> mapp;
vector<int> val;

void solve(int co, double p, int mon, bool flag) {
  // cout << "( " <<co << ", " << p << ", " << mon << " "<<") \\n";
  if (mapp.size() - 1 <= co) {
    // cout << " ans : "<< ans << "\\n";
    if (mon <= 0)
      mon = 0.0;
    ans += ((double)mon) * p;
    return;
  }

  double plob = (double)(1.0 / (double)v.size());
  // cout << "plob:" << plob <<"\\n";
  p *= plob;
  for (int i = 0; i < v.size(); i++) {
    //    cout << v[i] << "\\n";
    co += v[i];
    if (mapp[co] == 1 &&
        flag == false) { // テゥツ?イテ」ツつ?」ツδ榲」ツつケテ」ツ?ェテ」ツつ?
      int hoge = co + val[co];
      flag = true;
      //      cout << "( " <<co << " " << p << " " << mon << " "<<")" << endl;
      solve(hoge, p, mon, flag);
      flag = false;
    } else if ((mapp[co] == 2 || mapp[co] == 3) &&
               flag == false) { // テゥツ?妥」ツδ榲」ツつケテ」ツ?ェテ」ツつ?
      mon += val[co];
      flag = false;
      //      cout << "( " <<co << " " << p << " " << mon << " "<<")" << endl;
      solve(co, p, mon, flag);
      mon -= val[co];
    } else {
      flag = false;
      //      cout << "( " <<co << " " << p << " " << mon << " "<<")" << endl;
      solve(co, p, mon, flag);
    }
    co -= v[i];
  }
  p *= v.size();
  return;
}

int main() {

  while (1) {

    int x, y, z;
    cin >> x >> y >> z;
    if (x == 0 && y == 0 && z == 0)
      break;

    // init
    ans = 0.0;
    vector<int> tmpmapp(y + 1, 0);
    mapp = tmpmapp;
    vector<int> tmpval(y + 1, 0);
    val = tmpval;

    vector<int> tmpv(x);
    for (int i = 0; i < x; i++)
      cin >> tmpv[i];
    v = tmpv;

    vector<int> n(z), e(z), a(z);
    for (int i = 0; i < z; i++) {
      cin >> n[i] >> e[i] >> a[i];
      mapp[n[i]] = e[i];
      if (e[i] == 3)
        a[i] *= -1;
      val[n[i]] = a[i];
    }

    // debug
    /*
    cout << "map :";
    for(int i=0; i<mapp.size(); i++){
      cout << " " << mapp[i] ;
    }
    cout << endl;
    cout << "val :";
    for(int i=0; i<val.size(); i++){
      cout << " " << val[i] ;
    }
    cout << endl;
    cout << "v   :";
    for(int i=0; i<v.size(); i++){
      cout << " " << v[i] ;
    }
    cout << endl;
    */
    double p = 1.0;
    int co = 0, mon = 0;
    bool flag = false;
    solve(co, p, mon, flag);
    cout << (int)ans << endl;
  }
}