fork download
  1. #include<iostream>
  2. #include<algorithm>
  3. #include<string>
  4. using namespace std;
  5. int main()
  6. {
  7. string s="lrud";
  8. int n,m;
  9. cin>>n>>m;
  10. char a[n][m];
  11. int x,y,p,q;
  12. for(int i=0;i<n;i++)
  13. {
  14. scanf("%s",&a[i]);
  15. }
  16.  
  17. string c;
  18. cin>>c;
  19. sort(s.begin(),s.end());
  20. for(int i=0;i<n;i++)
  21. {
  22. for(int j=0;j<m;j++)
  23. {
  24.  
  25. if(a[i][j]=='S')
  26. {
  27. y=i;
  28. x=j;
  29. }
  30. else if(a[i][j]=='E')
  31. {
  32. q=i;
  33. p=j;
  34. }
  35. }
  36. }
  37. int ans,count=0;
  38. char ch;
  39. int k;
  40. int stx=x;
  41. int sty=y;
  42. do
  43. {
  44. ans=0;
  45. x=stx;
  46. y=sty;
  47. for(int i=0;i<c.length();i++)
  48. {
  49. ch=s[c[i]-'0'];
  50. k=0;
  51. if(ch=='l' && x>0 && a[x-1][y]!='#')
  52. {
  53. x--;
  54. k=1;
  55. }
  56. if(ch=='r' && x<m-1 && a[x+1][y]!='#')
  57. {
  58. x++;
  59. k=1;
  60. }
  61. if(ch=='u' && y>0 && a[x][y-1]!='#')
  62. {
  63. y--;
  64. k=1;
  65. }
  66. if(ch=='d' && y<n-1 && a[x][y+1]!='#')
  67. {
  68. y++;
  69. k=1;
  70. }
  71.  
  72. if(k==0)
  73. break;
  74. if(x==p && y==q)
  75. {
  76. ans=1;
  77. break;
  78. }
  79. }
  80. if(ans)
  81. count++;
  82. }while(next_permutation(s.begin(),s.end()));
  83.  
  84. printf("%d",count);
  85. return 0;
  86. }
Runtime error #stdin #stdout 0s 4232KB
stdin
Standard input is empty
stdout
Standard output is empty