I m trying to find the number of ways you can get from the top-left grid to the bottom right grid using dynamic programing so I m using map<pair<int, int>, int>
for memoization.
When I try to insert the k value in the map it doesn't get stored in my map, only my c
and r
integers get stored. Why is this happening?
#include <iostream>
#include <bits/stdc++.h>
#include <algorithm>
#include <stdbool.h>
#include <map>
using namespace std;
int grid(int r , int c , map<pair<int,int> ,int>& m1);
int main()
{
map<pair<int ,int>,int> m1;
int k=grid(2,3 ,m1);
cout<<k<<endl;
for(auto& it1:m1){
cout<<it1.first.first<<"\t"<<it1.first.second<<"\t"<<it1.second<<endl;
}
}
int grid(int r , int c , map<pair<int,int> ,int>& m1)
{
if(r==0 || c==0) return 0;
if(r==1 && c==1) return 1;
if(m1[{r,c}]!=NULL){return m1[{r,c}];}
int k=grid(r-1,c,m1)+grid(r,c-1,m1);
m1.insert({{r,c},k});
cout<<"k:"<<k<<endl;
return grid(r-1,c,m1)+grid(r,c-1,m1);
}
Output:-----------------------------------
k:1
k:1
k:1
k:1
k:1
k:2
k:1
k:1
k:3
k:1
k:1
k:1
k:1
k:1
k:2
k:1
k:1
3
1 2 0
1 3 0
2 1 0
2 2 0
2 3 0