Your dp array gets only partially initialized, so you'd be using uninitialized values. In debug builds uninitialized variables may be initialized anyway.
Also you could easily change it to use std::vector, which would avoid memory leaks and the general verbosity of creating the arrays. (Example code takes advantage of vector only partly, now you won't need to pass the row and column count either.)
Commented out code includes a fix for your code.
Code:
#include <stdio.h>
#include <vector>
#include <algorithm>
typedef std::vector<int> Row; //or Column?
typedef std::vector<Row> Grid;
inline int max(int a,int b,int c)
{
if(a>b)
if(a>c)
return a;
else
return c;
if(b>a)
if(b>c)
return b;
else
return c;
if(c>a)
if(c>b)
return c;
else
return b;
return b;
}
void solve(Grid& grid /*int **grid*/,int rows,int cols)
{
for(int j=0;j<rows;++j)
{
grid[j][0] = -9999;
grid[j][cols+1] = -9999;
}
Grid dp(rows, Row(cols + 2));
//int **dp = new int*[rows];
//for(int i = 0; i < rows; i++) {
// dp[i] = new int[cols+2];
// std::fill(dp[i], dp[i] + cols + 2, 0);
//}
for(int i=0;i<=cols+1;++i)
dp[0][i] = grid[0][i];
for(int k=1;k<rows;++k)
{
for(int n=1;n<=cols;++n)
{
int a,b,c,largest;
a = dp[k-1][n-1]+grid[k][n];
b = dp[k-1][n]+grid[k][n];
c = dp[k-1][n+1]+grid[k][n];
largest = max(a,b,c);
dp[k][n] = largest;
}
}
int solution = -9999;
for(int i=1;i<=cols;++i)
if(dp[rows-1][i] > solution)
solution = dp[rows-1][i];
printf("%d\n",solution);
}
int main()
{
int n;
scanf("%d",&n);
//int **grid;
while(n--)
{
int rows,cols;
scanf("%d %d",&rows,&cols);
Grid grid(rows, Row(cols + 2));
//grid = new int*[rows];
//for(int i = 0; i < rows; i++)
//grid[i] = new int[cols+2];
for(int i=0;i < rows;++i)
for(int j=1;j <= cols;++j)
{
int in;
scanf("%d",&in);
grid[i][j]=in;
}
solve(grid,rows,cols);
}
return 0;
}