fork(2) download
  1. #include <iostream>
  2. #include <iomanip>
  3.  
  4. using namespace std;
  5.  
  6. const int Tgt = 110;
  7. const int I = Tgt;
  8.  
  9. int m[7][7]={
  10. {3,4,5,4,3,9,7},
  11. {1,I,6,I,5,I,8},
  12. {2,3,7,6,9,4,1},
  13. {7,I,8,I,1,I,3},
  14. {5,5,9,8,7,2,6},
  15. {1,I,4,I,6,I,3},
  16. {3,4,2,5,5,4,3}
  17. };
  18.  
  19. bool path[7][7] = { false };
  20.  
  21. bool step(int x, int y, int sum)
  22. {
  23. sum += m[x][y];
  24. if (sum > Tgt) return false;
  25. if (x == 6 && y == 6)
  26. {
  27. if (sum != Tgt) return false;
  28. path[x][y] = true;
  29. for(int i = 0; i < 7; ++i)
  30. {
  31. for(int j = 0; j < 7; ++j)
  32. {
  33. cout << (path[i][j] ? '*' : ' ');
  34. }
  35. cout << endl;
  36. }
  37. return true;
  38. }
  39.  
  40. path[x][y] = true;
  41.  
  42. // Up
  43. if (y > 0 && !path[x][y-1])
  44. {
  45. if (step(x,y-1,sum)) return true;
  46. }
  47. // Dn
  48. if (y < 6 && !path[x][y+1])
  49. {
  50. if (step(x,y+1,sum)) return true;
  51. }
  52. // Lt
  53. if (x > 0 && !path[x-1][y])
  54. {
  55. if (step(x-1,y,sum)) return true;
  56. }
  57. // Rt
  58. if (x < 6 && !path[x+1][y])
  59. {
  60. if (step(x+1,y,sum)) return true;
  61. }
  62. path[x][y] = false;
  63. return false;
  64. }
  65.  
  66. int main(int argc, const char * argv[])
  67. {
  68. step(0,0,0);
  69. }
  70.  
  71.  
Success #stdin #stdout 0s 3468KB
stdin
Standard input is empty
stdout
*******
      *
  *****
  *    
  *    
  *    
  *****