CSES - Grid Paths

I can’t figure out what is wrong with this code .

#include<bits/stdc++.h>
#define mod 1000000007
#define ll long long
using namespace std;
void solve();
int main()
{
ios_base::sync_with_stdio(false);cin.tie(NULL);

#ifndef ONLINE_JUDGE
freopen(“input.txt”, “r”, stdin);
freopen(“error.txt”, “w”, stderr);
freopen(“output.txt”, “w”, stdout);
#endif

int t=1;
/is Single Test case?///cin>>t;
while(t–)
{
solve();
cout<<"\n";
}

cerr<<“time taken : “<<(float)clock()/CLOCKS_PER_SEC<<” secs”<<endl;
return 0;
}
void solve()
{
int n; cin>>n;
char a[n+1][n+1];
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= n; ++j)
{
cin>>a[i][j];
}
}
// vector< vector > dp(n+1, vector (n+1, 0)); //initialising a 2d vector with all elements to 0
int dp[n+1][n+1];

for (int i = 0; i <= n+1; ++i)
{
	for (int j = 0; j <= n+1; ++j)
	{
		dp[i][j] = 0;
	}
}
if(a[n][n]!='*')
	dp[n][n] = 1;
for (int i = n; i != 0; i--)
{
	for (int j = n; j != 0; j--)
	{
		if(a[i][j] == '*'){
			dp[i][j] = 0;
		}
		else{
			if(i == n and j == n)
				continue;
			dp[i][j] += ((dp[i+1][j])%mod + (dp[i][j+1])%mod) %mod;
			//cout<<dp[i][j]<<" ";
		}
	}
}
cout<<dp[1][1];

}

It is giving me wrong output for some test cases.
on this test case
10

.







this is giving me output 27560 instead of 27479
and some other test cases too.

Any help is appreciated.

Share your code link on cses
Btw mine is https://cses.fi/paste/64c88fb27fdff78418c661/ tell me if you want to know the approach

1 Like

When i = n and j runs from n to 1, you are using

dp[i][j] += ((dp[i+1][j])%mod + (dp[i][j+1])%mod) %mod;

This might be reading garbage values.
You need to inititalize the top row.

1 Like

i dont know how to do that although i still cant find the bug .
:frowning:

There is an option to share your code. There it will give your codes link

https://cses.fi/paste/c073fab0ca7da99b1a0210/

I would see your solution but I want to find out what is wrong with my code .
Thanks