Top Down approach giving TLE

I’ve tried the top down approach in java which is giving me TLE in last 5 cases. Here is the code :


import java.io.*;
import java.util.*;
public class Main
{
    
    public static long func(long dp[][],char matrix[][], int i, int j, int h , int w)
    {
        
        if((i>=h) ||(j>=w))
        {
            return 0;
        
        }
        if((i==h-1) && (j==w-1))
        {
            
            return 1;
        
        }
        
        if(dp[i][j]!=0)
        {
            return dp[i][j];
        }
        if(matrix[i][j]=='#')
        {  
            return 0;
        }
        long x=func(dp,matrix,i,j+1,h,w);
        long y=func(dp,matrix,i+1,j,h,w);
        
        
        //System.out.println(x+y);
        return dp[i][j]=(x+y)%1000000007l;
        
        
        
        
    }
    
    
    
    
	public static void main(String[] args) throws Exception {
		//System.out.println("Hello World");
		BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
		String s=br.readLine();
		String ss[]=s.split(" ");
		int h=Integer.parseInt(ss[0]);
		int w=Integer.parseInt(ss[1]);
		char ch[];
		char matrix[][]=new char[h][w];
		for(int i=0;i<h;i++)
		{
		    s=br.readLine();
		    ch=s.toCharArray();
		    for (int j=0;j<w;j++)
		    {
		        
		        matrix[i][j]=ch[j];
		    }
		    
		    
		    
		}
		
		long dp[][]=new long[h][w];
            
		long ans =func(dp,matrix,0,0,h,w);
		
		long mod=1000000007l;
		System.out.println(ans%mod);
		
		
	}
}

Can you please point out why is this giving TLE in few cases and how can we make it work.
Thanks .

Can you please share your code on ide.codingblocks.com with proper indentation? because here I may feel I can miss something as your code is not aligned properly

Hi ! here is the link to the code:

Thanks I will look into it and get back to you with the error

Try making your dp array of size [h+1][w+1] and check if it still gives error

Actually my code is giving me TLE on last 5 testcases , i’m not sure why i’m getting it even after using DP.
Else , it is running fine and not giving me any error.

I can send you the test cases if you want. You can dry run on them to see

I think its a good idea. Please do send me test cases .
Thanks .