fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. void is_palimdrom(string str,int & count)
  5. {
  6. int l = 0;
  7. int h =str.size()- 1;
  8. while (h > l)
  9. {
  10. if (str[l++] != str[h--])
  11. {
  12. return;
  13. }
  14. }
  15. count++;
  16. }
  17.  
  18. void DFS_util(vector<vector<char>>arr,int x,int y,string str,int & count)
  19. {
  20. cout<<str<<endl;
  21. int m=arr.size();
  22. int n=arr[0].size();
  23. if(x==m-1 && y==n-1)
  24. {
  25. is_palimdrom(str,count);
  26.  
  27. }
  28. else if( x<m && y<n)
  29. {
  30. DFS_util(arr, x+1, y, str+arr[x][y], count);
  31. DFS_util(arr, x, y+1, str+arr[x][y], count);
  32. }
  33. return ;
  34. }
  35. int main()
  36. {
  37. int T;
  38. cin>>T;
  39. while(T--)
  40. {
  41. int n,m;
  42. cin>>n,m;
  43. vector<vector<char>>arr(n,(vector<char>(m,0)));
  44. for(int i=0;i<n;i++)
  45. {
  46. for(int j=0;j<m;j++)
  47. {
  48. cin>>arr[i][j];
  49. }
  50. }
  51.  
  52. int count=0;
  53. //string str="";
  54. DFS_util(arr,0,0,"a",count);
  55. cout<<count<<endl;
  56. }
  57.  
  58. return 0;
  59. }
Success #stdin #stdout 0s 4392KB
stdin
2
3 4
a a a b b a a a a b b a
2 1
s l
stdout
a
0
a
0