fork(10) download
  1. #include <iostream>
  2. using namespace std;
  3. bool vis[1001][1001];
  4. char c[1001][1001];
  5. int m,n;
  6. int rec(int i1,int j1)
  7. {
  8. //cout<<i1<<" "<<j1<<endl;
  9. if(i1+1<m&&!vis[i1+1][j1]&&c[i1+1][j1]=='f')
  10. {
  11. vis[i1+1][j1]=1;
  12. return 1;
  13. }
  14. if(i1+1<m&&!vis[i1+1][j1]&&c[i1+1][j1]=='b')
  15. {
  16. vis[i1+1][j1]=1;
  17. if(rec(i1+1,j1))
  18. return 1;
  19. vis[i1+1][j1]=0;
  20. }
  21. if(i1-1>=0&&!vis[i1-1][j1]&&c[i1-1][j1]=='f')
  22. {
  23. vis[i1-1][j1]=1;
  24. return 1;
  25. }
  26. if(i1-1>=0&&!vis[i1-1][j1]&&c[i1-1][j1]=='b')
  27. {
  28. vis[i1-1][j1]=1;
  29. if(rec(i1-1,j1))
  30. return 1;
  31. vis[i1-1][j1]=0;
  32. }
  33. if(j1+1<n&&!vis[i1][j1+1]&&c[i1][j1+1]=='f')
  34. {
  35. vis[i1][j1+1]=1;
  36. return 1;
  37. }
  38. if(j1+1<n&&!vis[i1][j1+1]&&c[i1][j1+1]=='b')
  39. {
  40. vis[i1][j1+1]=1;
  41. if(rec(i1,j1+1))
  42. return 1;
  43. vis[i1][j1+1]=0;
  44. }
  45. if(j1-1>=0&&!vis[i1][j1-1]&&c[i1][j1-1]=='f')
  46. {
  47. vis[i1][j1-1]=1;
  48. return 1;
  49. }
  50. if(j1-1>=0&&!vis[i1][j1-1]&&c[i1][j1-1]=='b')
  51. {
  52. vis[i1][j1-1]=1;
  53. if(rec(i1,j1-1))
  54. return 1;
  55. vis[i1][j1-1]=0;
  56. }
  57. return 0;
  58. }
  59. int main()
  60. {
  61. int t,i1,j1;
  62. cin>>t;
  63. while(t--)
  64. {
  65. cin>>m>>n;
  66. for(int i=0;i<m;i++)
  67. {
  68. for(int j=0;j<n;j++)
  69. {
  70. cin>>c[i][j];
  71. if(c[i][j]=='s')
  72. {
  73. i1=i;
  74. j1=j;
  75. }
  76. }
  77. }
  78. vis[i1][j1]=1;
  79. if(rec(i1,j1))
  80. for(int i=0;i<m;cout<<endl,i++)
  81. for(int j=0;j<n;vis[i][j]=0,j++)
  82. if(vis[i][j])
  83. cout<<c[i][j];
  84. else
  85. cout<<'-';
  86. else
  87. cout<<"The path doesn't exist."<<endl;
  88. vis[i1][j1]=0;
  89. }
  90. return 0;
  91. }
Success #stdin #stdout 0s 4856KB
stdin
2
6 5
bbbbw
bwwfw
bbbww
bwwww
bbbbw
wwwbs

4 4
bbbw
wwfw
bwww
bbbs
stdout
bbbb-
b--f-
b----
b----
bbbb-
---bs
The path doesn't exist.