fork download
  1. #include<iostream>
  2. #include<cstring>
  3. #include<cstdio>
  4. using namespace std;
  5. char str[1001][1001];
  6. bool mat[1001][1001];
  7. int n,m;
  8. bool func(int a,int b)
  9. {
  10. //cout<<a<<" "<<b<<"\n";
  11. if(str[a][b]=='f')
  12. return true;
  13. if(a<n-1 && (str[a+1][b]=='b' ||str[a+1][b]=='f' ) && mat[a+1][b]==false)
  14. {mat[a][b]=true;bool x=func(a+1,b);if(x){str[a][b]='-';return true;}}
  15. if(a>0 && (str[a-1][b]=='b' || str[a-1][b]=='f')&& mat[a-1][b]==false)
  16. {mat[a][b]=true;bool x=func(a-1,b);if(x){str[a][b]='-';return true;}}
  17. if(b<m-1 && (str[a][b+1]=='b' || str[a][b+1]=='f')&& mat[a][b+1]==false)
  18. {mat[a][b]=true;bool x=func(a,b+1);if(x){str[a][b]='-';return true;}}
  19. if(b>0 && (str[a][b-1]=='b' || str[a][b-1]=='f')&& mat[a][b-1]==false)
  20. {mat[a][b]=true;bool x=func(a,b-1);if(x){str[a][b]='-';return true;}}
  21. return false;
  22. }
  23. main()
  24. {
  25. int t,a,b;
  26.  
  27. scanf("%d",&t);
  28. for(int i=0;i<t;i++)
  29. {
  30. scanf("%d %d",&n,&m);
  31. for(int j=0;j<n;j++)
  32. for(int k=0;k<m;k++)
  33. mat[j][k]=false;
  34. for(int j=0;j<n;j++)
  35. scanf("%s",&str[j]);
  36. for(int j=0;j<n;j++)
  37. for(int k=0;k<m;k++)
  38. if(str[j][k]=='s')
  39. {a=j;b=k;break;}
  40. if(func(a,b))
  41. {
  42. for(int j=0;j<n;j++)
  43. for(int k=0;k<m;k++)
  44. {if(str[j][k]=='-')
  45. str[j][k]='b';
  46. else if(str[j][k]=='w' || str[j][k]=='b')
  47. str[j][k]='-';}
  48. str[a][b]='s';
  49. for(int j=0;j<n;j++)
  50. printf("%s\n",str[j]);
  51.  
  52.  
  53. }
  54. else
  55. printf("The path doesn't exist.\n");
  56.  
  57. }
  58.  
  59.  
  60.  
  61.  
  62. // system("pause");
  63. }
Success #stdin #stdout 0s 4856KB
stdin
1
7 7
wwwwwww
wfbwbbw
wbwwwbw
wbbbbbw
wwwwwbw
wwwwwsw
wwwwwww
stdout
-------
-f-----
-b-----
-bbbbb-
-----b-
-----s-
-------