Problem p03167 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <stdlib.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#undef INT_MIN
#undef INT_MAX
#define LL long long
#define INT_MIN -2147483647
#define INT_MAX 1000000009
#define LL_MIN -9223372036854775807
#define LL_MAX 9223372036854775807
#define MP make_pair
#define PB push_back
#define MOD 1000000007

int H, W;
string grid[1009];
int dp[50][50];

// dp[i][j] >> (1,1)から(i+1,j+1)にたどり着くまでの経路数
// dp[i][j] = dp[i-1][j] + dp[i][j-1](i!=0 && j!=0)
//          = dp[0][j-1] (i==0)
//          = dp[i-1][0] (j==0)
//          = 0 (grid[i][j] == '#')

int main() {
  cin >> H >> W;
  for (int i = 0; i < H; i++) {
    cin >> grid[i];
    for (int j = 0; j < W; j++)
      dp[i][j] = 0;
  }
  dp[0][0] = 1;
  // cout << "dp[" << 0 << "][" << 0 << "] = " << dp[0][0] << endl;
  for (int i = 1; i < H; i++) {
    if (grid[i][0] != '#')
      dp[i][0] = dp[i - 1][0];
    //	cout << "dp[" << i << "][" << 0 << "] = " << dp[i][0] << endl;
  }
  for (int j = 1; j < W; j++) {
    if (grid[0][j] != '#')
      dp[0][j] = dp[0][j - 1];
    //	cout << "dp[" << 0 << "][" << j << "] = " << dp[0][j] << endl;
  }

  for (int i = 1; i < H; i++) {
    for (int j = 1; j < W; j++) {
      if (grid[i][j] != '#')
        dp[i][j] = (dp[i - 1][j] + dp[i][j - 1]) % MOD;
      //		cout << "dp[" << i << "][" << j << "] = " << dp[i][j] <<
      //endl;
    }
  }
  cout << dp[H - 1][W - 1] % MOD << endl;

  return 0;
}

/*
#include<algorithm>
#include<iostream>
#include<utility>
using namespace std;

#undef INT_MIN
#undef INT_MAX
#define LL long long
#define INT_MIN -2147483647
#define INT_MAX 2147483647
#define LL_MIN -9223372036854775807
#define LL_MAX 9223372036854775807

int N,W;
pair<int,int> b[100];
int w[105],v[105];
LL dp[105][100100];
int main(){
        cin >> N >> W;
        for(int i = 0; i<N; i++){
                for(int j=0; j<=W; j++){
                        dp[i][j]=-1;
                }
        }
        for(int i=0; i<N; i++){
                int we,va;
                cin >> we >> va;
                b[i]=make_pair(we,va);
        }
        for(int i = 0; i <= W; i++) dp[N][i] = 0;

        for(int i = N-1; i >= 0; i--){
                for(int j = W; j >= 0; j--){
                        dp[i][j] = dp[i+1][j];//not serect i
                        if(j-b[i].first >= 0){
                                dp[i][j]= max( dp[i][j], dp[i+1][j-b[i].first] +
b[i].second );
                        }
                }
        }

        cout << dp[0][W] << endl;
        return 0;
}

*/

// union find & prime
/*
#include<algorithm>
#include<iostream>
#include<map>
#include<string>
#include<utility>
#include<vector>

using namespace std;
int n;
int num[100100];
vector<pair<int,int>>sortednum;
int par[100010];
map<int,int>ppid;

int neid(int x){
        if(par[x]==x)return x;
        else{
                return par[x]=neid(par[x]);
        }
}

void primenum(int inow, int NUM){
        for(int i=2; i*i<=NUM; i++){
                if(NUM%i==0){
                        if(ppid.count(i)==0){
                                ppid[i]=inow;
                        }
                        else{
                                par[neid(ppid[i])]=inow;
                        }
                        while(NUM%i==0)NUM/=i;
                }
        }
        if(NUM!=1){
                if(ppid.count(NUM)==0){
                        ppid[NUM]=inow;
                }
                else{
                        par[neid(ppid[NUM])]=inow;
                }

        }
}

int main(){
        cin >> n;
        for(int i=0; i<n; i++){
                cin >> num[i];
                sortednum.push_back(make_pair(num[i],i));
                par[i]=i;
                primenum(i,num[i]);
        }

        sort(sortednum.begin(),sortednum.end());
        int ans=1;

        for(int i=0; i<n; i++){
                if(neid(i)!=neid(sortednum[i].second)){
                        ans=0;
                        break;
                }
        }
        cout << ans << endl;

        return 0;
}
*/

// djikstra
/*
#include <algorithm>
#include <cassert>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <tuple>
#include <unordered_map>
#include <vector>
using namespace std;
int INF = 10000000;
priority_queue< pair<int,int> > que; //INF-cost,i
int cost[1000];
for(int i=0; i<1000; i++) cost[i] = INF;
cost[0] = 0;
que.push(make_pair(INF-0, 0));
while(!que.empty){
        int now = que.top().second;
        for(int i=0; i<tonari[now].size(); i++){
                if(cost[tonari[now][i]] > cost[now] +
distance[now][tonari[now][i]){ cost[tonari[now][i]] = cost[now] +
distance[now][tonari[now][i]; que.push(make_pair(INF-cost[tonari[now][i]],
tonari[now][i]));
                }
        }
        que.pop();
}
*/

Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <stdlib.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#undef INT_MIN
#undef INT_MAX
#define LL long long
#define INT_MIN -2147483647
#define INT_MAX 1000000009
#define LL_MIN -9223372036854775807
#define LL_MAX 9223372036854775807
#define MP make_pair
#define PB push_back
#define MOD 1000000007

int H, W;
string grid[1009];
int dp[1009][1009];

// dp[i][j] >> (1,1)から(i+1,j+1)にたどり着くまでの経路数
// dp[i][j] = dp[i-1][j] + dp[i][j-1](i!=0 && j!=0)
//          = dp[0][j-1] (i==0)
//          = dp[i-1][0] (j==0)
//          = 0 (grid[i][j] == '#')

int main() {
  cin >> H >> W;
  for (int i = 0; i < H; i++) {
    cin >> grid[i];
    for (int j = 0; j < W; j++)
      dp[i][j] = 0;
  }
  dp[0][0] = 1;
  // cout << "dp[" << 0 << "][" << 0 << "] = " << dp[0][0] << endl;
  for (int i = 1; i < H; i++) {
    if (grid[i][0] != '#')
      dp[i][0] = dp[i - 1][0];
    //	cout << "dp[" << i << "][" << 0 << "] = " << dp[i][0] << endl;
  }
  for (int j = 1; j < W; j++) {
    if (grid[0][j] != '#')
      dp[0][j] = dp[0][j - 1];
    //	cout << "dp[" << 0 << "][" << j << "] = " << dp[0][j] << endl;
  }

  for (int i = 1; i < H; i++) {
    for (int j = 1; j < W; j++) {
      if (grid[i][j] != '#')
        dp[i][j] = (dp[i - 1][j] + dp[i][j - 1]) % MOD;
      //		cout << "dp[" << i << "][" << j << "] = " << dp[i][j] <<
      //endl;
    }
  }
  cout << dp[H - 1][W - 1] % MOD << endl;

  return 0;
}

/*
#include<algorithm>
#include<iostream>
#include<utility>
using namespace std;

#undef INT_MIN
#undef INT_MAX
#define LL long long
#define INT_MIN -2147483647
#define INT_MAX 2147483647
#define LL_MIN -9223372036854775807
#define LL_MAX 9223372036854775807

int N,W;
pair<int,int> b[100];
int w[105],v[105];
LL dp[105][100100];
int main(){
        cin >> N >> W;
        for(int i = 0; i<N; i++){
                for(int j=0; j<=W; j++){
                        dp[i][j]=-1;
                }
        }
        for(int i=0; i<N; i++){
                int we,va;
                cin >> we >> va;
                b[i]=make_pair(we,va);
        }
        for(int i = 0; i <= W; i++) dp[N][i] = 0;

        for(int i = N-1; i >= 0; i--){
                for(int j = W; j >= 0; j--){
                        dp[i][j] = dp[i+1][j];//not serect i
                        if(j-b[i].first >= 0){
                                dp[i][j]= max( dp[i][j], dp[i+1][j-b[i].first] +
b[i].second );
                        }
                }
        }

        cout << dp[0][W] << endl;
        return 0;
}

*/

// union find & prime
/*
#include<algorithm>
#include<iostream>
#include<map>
#include<string>
#include<utility>
#include<vector>

using namespace std;
int n;
int num[100100];
vector<pair<int,int>>sortednum;
int par[100010];
map<int,int>ppid;

int neid(int x){
        if(par[x]==x)return x;
        else{
                return par[x]=neid(par[x]);
        }
}

void primenum(int inow, int NUM){
        for(int i=2; i*i<=NUM; i++){
                if(NUM%i==0){
                        if(ppid.count(i)==0){
                                ppid[i]=inow;
                        }
                        else{
                                par[neid(ppid[i])]=inow;
                        }
                        while(NUM%i==0)NUM/=i;
                }
        }
        if(NUM!=1){
                if(ppid.count(NUM)==0){
                        ppid[NUM]=inow;
                }
                else{
                        par[neid(ppid[NUM])]=inow;
                }

        }
}

int main(){
        cin >> n;
        for(int i=0; i<n; i++){
                cin >> num[i];
                sortednum.push_back(make_pair(num[i],i));
                par[i]=i;
                primenum(i,num[i]);
        }

        sort(sortednum.begin(),sortednum.end());
        int ans=1;

        for(int i=0; i<n; i++){
                if(neid(i)!=neid(sortednum[i].second)){
                        ans=0;
                        break;
                }
        }
        cout << ans << endl;

        return 0;
}
*/

// djikstra
/*
#include <algorithm>
#include <cassert>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <tuple>
#include <unordered_map>
#include <vector>
using namespace std;
int INF = 10000000;
priority_queue< pair<int,int> > que; //INF-cost,i
int cost[1000];
for(int i=0; i<1000; i++) cost[i] = INF;
cost[0] = 0;
que.push(make_pair(INF-0, 0));
while(!que.empty){
        int now = que.top().second;
        for(int i=0; i<tonari[now].size(); i++){
                if(cost[tonari[now][i]] > cost[now] +
distance[now][tonari[now][i]){ cost[tonari[now][i]] = cost[now] +
distance[now][tonari[now][i]; que.push(make_pair(INF-cost[tonari[now][i]],
tonari[now][i]));
                }
        }
        que.pop();
}
*/