Problem p03222 (C++)

Buggy submission (status: Runtime Error):
// #include "stdafx.h"
#include <algorithm>
#include <assert.h>
#include <climits>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>

using namespace std;

/*-----------------------------------------------------------------------------
　定義
 -------------------------------------------------------------------------------*/
#define REP(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define REPN(i, m, n) for (int(i) = m; (i) < (int)(n); ++(i))
#define INF 2e9
#define MOD (1000 * 1000 * 1000 + 7)
#define Ceil(x, n) (((((x)) + ((n)-1)) / n)) /* Nの倍数に切り上げ割り算 */
#define CeilN(x, n) (((((x)) + ((n)-1)) / n) * n) /* Nの倍数に切り上げ */
#define FloorN(x, n) ((x) - (x) % (n)) /* Nの倍数に切り下げ */
#define IsOdd(x) (((x)&0x01UL) == 0x01UL)
#define IsEven(x) (!IsOdd((x)))
#define BitSetV(Val, Bit) ((Val) |= (Bit))
#define BitTstV(Val, Bit) ((Val) & (Bit))
#define ArrayLength(x) (sizeof(x) / sizeof(x[0]))
#define MAX_QWORD ((QWORD)0xFFFFFFFFFFFFFFFF)
#define M_PI 3.14159265358979323846
typedef long long ll;
typedef unsigned long long int QWORD;
typedef pair<ll, ll> P;

/*-----------------------------------------------------------------------------
　処理
 -------------------------------------------------------------------------------*/
ll dfs(bool prevLineExist, int i, int num) {
  if ((num <= 0) || (num < i)) {
    return 1;
  }

  if (i == num) {
    return 1;
  }

  ll cnt = 0;
  int next = i + 1;
  if (prevLineExist) {
    // 引けない
    cnt = dfs(false, next, num);
  } else {
    // 引く or 引かない
    cnt = dfs(true, next, num);
    cnt += dfs(false, next, num);
  }

  cnt %= MOD;
  return cnt;
}

int main() {
  int H, W, K;
  cin >> H >> W >> K;

  //	vector<vector<ll>> dp(H + 1, vector<ll>(W, 0));
  static ll dp[20][20];
  dp[0][0] = 1;
  REP(i, H) {
    REP(j, W) {
      if (0 < dp[i][j]) {
        ll sum;
        ll ptnCnt;
        ll ptnNoneL = dfs(false, 0, j - 1);
        ll ptnExistL = dfs(false, 0, j - 2);
        ll ptnNoneR = dfs(false, j + 1, W - 1);
        ll ptnExistR = dfs(false, j + 2, W - 1);

        // ひだり
        if (j != 0) {
          ptnCnt = (ptnExistL * ptnNoneR) % MOD;
          sum = (dp[i][j] * ptnCnt) % MOD;
          dp[i + 1][j - 1] += sum;
          dp[i + 1][j - 1] %= MOD;
        }

        // まんなか
        ptnCnt = (ptnNoneL * ptnNoneR) % MOD;
        sum = (dp[i][j] * ptnCnt) % MOD;
        dp[i + 1][j] += sum;
        dp[i + 1][j] %= MOD;

        // みぎ
        if (j != W - 1) {
          ptnCnt = (ptnNoneL * ptnExistR) % MOD;
          sum = (dp[i][j] * ptnCnt) % MOD;
          dp[i + 1][j + 1] += sum;
          dp[i + 1][j + 1] %= MOD;
        }
      }
    }
  }
  cout << dp[H][K - 1] << endl;
  return 0;
}


Fixed submission:
// #include "stdafx.h"
#include <algorithm>
#include <assert.h>
#include <climits>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>

using namespace std;

/*-----------------------------------------------------------------------------
　定義
 -------------------------------------------------------------------------------*/
#define REP(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define REPN(i, m, n) for (int(i) = m; (i) < (int)(n); ++(i))
#define INF 2e9
#define MOD (1000 * 1000 * 1000 + 7)
#define Ceil(x, n) (((((x)) + ((n)-1)) / n)) /* Nの倍数に切り上げ割り算 */
#define CeilN(x, n) (((((x)) + ((n)-1)) / n) * n) /* Nの倍数に切り上げ */
#define FloorN(x, n) ((x) - (x) % (n)) /* Nの倍数に切り下げ */
#define IsOdd(x) (((x)&0x01UL) == 0x01UL)
#define IsEven(x) (!IsOdd((x)))
#define BitSetV(Val, Bit) ((Val) |= (Bit))
#define BitTstV(Val, Bit) ((Val) & (Bit))
#define ArrayLength(x) (sizeof(x) / sizeof(x[0]))
#define MAX_QWORD ((QWORD)0xFFFFFFFFFFFFFFFF)
#define M_PI 3.14159265358979323846
typedef long long ll;
typedef unsigned long long int QWORD;
typedef pair<ll, ll> P;

/*-----------------------------------------------------------------------------
　処理
 -------------------------------------------------------------------------------*/
ll dfs(bool prevLineExist, int i, int num) {
  if ((num <= 0) || (num < i)) {
    return 1;
  }

  if (i == num) {
    return 1;
  }

  ll cnt = 0;
  int next = i + 1;
  if (prevLineExist) {
    // 引けない
    cnt = dfs(false, next, num);
  } else {
    // 引く or 引かない
    cnt = dfs(true, next, num);
    cnt += dfs(false, next, num);
  }

  cnt %= MOD;
  return cnt;
}

int main() {
  int H, W, K;
  cin >> H >> W >> K;

  vector<vector<ll>> dp(H + 1, vector<ll>(W, 0));
  //	static ll dp[20][20];
  dp[0][0] = 1;
  REP(i, H) {
    REP(j, W) {
      if (0 < dp[i][j]) {
        ll sum;
        ll ptnCnt;
        ll ptnNoneL = dfs(false, 0, j - 1);
        ll ptnExistL = dfs(false, 0, j - 2);
        ll ptnNoneR = dfs(false, j + 1, W - 1);
        ll ptnExistR = dfs(false, j + 2, W - 1);

        // ひだり
        if (j != 0) {
          ptnCnt = (ptnExistL * ptnNoneR) % MOD;
          sum = (dp[i][j] * ptnCnt) % MOD;
          dp[i + 1][j - 1] += sum;
          dp[i + 1][j - 1] %= MOD;
        }

        // まんなか
        ptnCnt = (ptnNoneL * ptnNoneR) % MOD;
        sum = (dp[i][j] * ptnCnt) % MOD;
        dp[i + 1][j] += sum;
        dp[i + 1][j] %= MOD;

        // みぎ
        if (j != W - 1) {
          ptnCnt = (ptnNoneL * ptnExistR) % MOD;
          sum = (dp[i][j] * ptnCnt) % MOD;
          dp[i + 1][j + 1] += sum;
          dp[i + 1][j + 1] %= MOD;
        }
      }
    }
  }
  cout << dp[H][K - 1] << endl;
  return 0;
}