fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define pb push_back
  4. #define mp make_pair
  5. #define nline cout<<"\n"
  6. #define fast ios_base::sync_with_stdio(false),cin.tie(0)
  7. #define ain(A, B, C) assert(IN(A, B, C))
  8. #define ull unsigned long long int
  9. #define ll long long int
  10. #define pii pair<int,int>
  11. #define MAXX 1009
  12. #define fr(a,b,i) for(int i=a;i<b;i++)
  13. vector<int>G[MAXX];
  14. char mat[MAXX][MAXX];
  15. int n,cnt=0;
  16. string s="";
  17. string final;
  18. void backtrack(int x,int y)
  19. {
  20. if(x>n-1 or y>n-1 or x<0 or y<0)return ;
  21. s+=mat[x][y];
  22. if(s.size()==4)
  23. {
  24. if(s==final)
  25. cnt++;
  26. return ;
  27. }
  28.  
  29. backtrack(x-1,y-1);
  30. backtrack(x+1,y+1);
  31. backtrack(x+1,y-1);
  32. backtrack(x-1,y+1);
  33. }
  34. int main()
  35. {
  36. fast;
  37. cin>>n;
  38. for(int i=0;i<n;i++)
  39. for(int j=0;j<n;j++)
  40. cin>>mat[i][j];
  41. cin>>final;
  42. backtrack(0,0);
  43. cout<<cnt<<endl;
  44. return 0;
  45. }
  46.  
  47.  
Success #stdin #stdout 0s 4456KB
stdin
Standard input is empty
stdout
0