fork download
  1. #include <iostream>
  2. #define FIN "mouse.in"
  3. #define FOUT "mouse.out"
  4.  
  5. using namespace std;
  6.  
  7. int n,m,k,x,y,z,t,
  8. mat[11][11],//the matrix holds the table
  9. sol[2][100];//simutate the stack on which we hold the roads.
  10.  
  11. const int dx[] = {-1,0,1,0};//to identify the index x
  12. const int dy[] = {0,1,0,-1};//to identify the index y
  13.  
  14. void read() {
  15. //freopen(FIN, "r", stdin);
  16. cin>>n>>m>>x>>y>>z>>t;
  17. for(int i = 1; i <= n; ++i)
  18. for(int j = 1; j <= m; ++j)
  19. cin>>mat[i][j];
  20. }
  21.  
  22. void display() {
  23. int i,j;
  24. cout<<n<<" "<<m<<endl<<x<<" "<<y<<endl<<z<<" "<<t<<endl;
  25. for(i = 0; i <= n+1; ++i) {
  26. for(j = 0; j <= m+1; ++j) {
  27. cout<<mat[i][j]<<" ";
  28. }
  29. cout<<"\n";
  30. }
  31. }
  32.  
  33. void border() {
  34. int c;
  35. for(c = 0; c <= n + 1; c++) mat[c][0] = mat[c][m+1] = 1;
  36. for(c = 0; c <= m + 1; c++) mat[0][c] = mat[n+1][c] = 1;
  37. }
  38.  
  39. void bkt(int x, int y) {
  40.  
  41. int i;
  42. if(x == z && y == t) {
  43. for(int s = 1; s<=k; ++s) cout<<"("<<sol[0][s]<<","<<sol[1][s]<<")";
  44. cout<<"("<<z<<","<<t<<")";
  45. cout<<endl;
  46. return;
  47. }
  48.  
  49. if(mat[x][y] == 0) {
  50. mat[x][y] = 2;
  51. k++;
  52. sol[0][k] = x;
  53. sol[1][k] = y;
  54. for(i = 0; i < 4; ++i)
  55. bkt(x + dx[i], y + dy[i]);
  56. mat[x][y] = 0;
  57. k--;
  58. }
  59. }
  60.  
  61. int main(int argc, char const *argv[]) {
  62.  
  63. read();
  64. border();
  65. //display();
  66. bkt(x,y);
  67. return 0;
  68. }
Success #stdin #stdout 0s 5300KB
stdin
4 6 3 1 1 6
1 0 0 0 0 0
0 0 1 1 0 1
0 0 0 0 0 1
1 1 1 1 1 1
stdout
(3,1)(2,1)(2,2)(1,2)(1,3)(1,4)(1,5)(1,6)
(3,1)(2,1)(2,2)(3,2)(3,3)(3,4)(3,5)(2,5)(1,5)(1,6)
(3,1)(3,2)(2,2)(1,2)(1,3)(1,4)(1,5)(1,6)
(3,1)(3,2)(3,3)(3,4)(3,5)(2,5)(1,5)(1,6)