Easy code
\n# Code\n\nclass Solution {\npublic:\nint val;\nbool is_valid(int n,int r,int c, string &s,int index)\n{\n    if(r>=n||c>=n||r<0||c<0||index>=s.size())\n     r
\n# Code\n```\nclass Solution {\npublic:\nint val;\nbool is_valid(int n,int r,int c, string &s,int index)\n{\n    if(r>=n||c>=n||r<0||c<0||index>=s.size())\n     return false;\n     return true;\n}\nvoid dfs(int n,int r,int c, string &s,int index)\n{\n    if(r>=n||c>=n||r<0||c<0||index>=s.size())\n     return;\n    \n      if(s[index]==\'R\'&&is_valid(n,r,c+1,s,index))//check if present index is valid and then go for +1 index\n      {\n           val++;\n         dfs(n,r,c+1,s,index+1);\n      }\n      if(s[index]==\'D\'&&is_valid(n,r+1,c,s,index))\n      {\n           val++;\n         dfs(n,r+1,c,s,index+1);\n      }\n      if(s[index]==\'L\'&&is_valid(n,r,c-1,s,index))\n      {\n           val++;\n         dfs(n,r,c-1,s,index+1);\n      }\n      if(s[index]==\'U\'&&is_valid(n,r-1,c,s,index))\n      {\n           val++;\n         dfs(n,r-1,c,s,index+1);\n      }\n}\n    vector<int> executeInstructions(int n, vector<int>& startPos, string s) {\n        vector<int>ans;\n        for(int i=0;i<s.size();i++)\n        {\n            val=0;\n            dfs(n,startPos[0],startPos[1],s,i);\n            ans.push_back(val);//since val will be incre 1 time extra even if the next cell is valid\n        }\n        return ans;\n    }\n};\n```
['Depth-First Search', 'C++']