mercredi 31 octobre 2018

Need help avoiding integer overflow for Project Euler 15

I've been running into troubles with Integer overflow. The code works up till an input of 16, but I need it to work up till 20.

#include <iostream>
#include <cmath>
#include <stdlib.h>
#include <stdio.h>

using namespace std;
int grid(int);

int main(){
    int size;
    cin >> size;
    grid(size);

return 0;
}

int grid(int array){
    array = array+1;
    unsigned long long int map[array][array];
    for(int i = 0; i < array; ++i){ 
    // initialize grid distance
        map[i][0] =1;
        map[0][i]= 1;}
    for(int i =1; i < array; ++i){
        for(int j =1; j < array; ++j){
            map[i][j] = map[i-1][j] +map[i][j-1];}
}
    array = array -1;
return map[array][array]; //Return value to main function
}

Aucun commentaire:

Enregistrer un commentaire