fork download
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4. const int NUM_HOUSES = 8;
  5. void print_houses(int houses[], int cur_pos, int night)
  6. {
  7. // Task 3:
  8. // print the state of each house in current night.
  9. // '*': thief's position, '1, 2, 3, ...': stolen houses, '0': valid houses.
  10. // example: // Night 1: // | 0 | 0 | * | 0 | 0 | 0 | 0 | 0 |
  11.  
  12. cout << "Night " << night << ": " << endl;
  13. cout << "|";
  14. for (int idx = 0; idx < NUM_HOUSES; idx++)
  15. {
  16. if (idx == cur_pos)
  17. {
  18. // In cur_pos we need to print * which is the thief's position
  19. cout << " * |";
  20. }
  21. else
  22. {
  23. // otherwise we can print the array content
  24. cout << " " << houses[idx] << " |";
  25. }
  26. }
  27. cout << endl;
  28. }
  29. int steal_house(int houses[], int cur_pos, int alert_dis, int night)
  30. {
  31. // Task 2:
  32. // output the next target. When there is no valid house to steal, return -1.
  33.  
  34. int target = -1;
  35.  
  36. // First of all set the value of the house that thief stoled to night - 1
  37. houses[cur_pos] = night - 1;
  38.  
  39. // Normally next_target would be [cur_pos + alert_dis]
  40. // But as it is a circular array so we need to bound within the limits,
  41. // e.g in this case index can't go out of bound i.e. idx<=7
  42. // So we are taking the reminder after dividing it by 8
  43. int next_pos = (cur_pos + alert_dis) % NUM_HOUSES;
  44.  
  45. // To select the suitable next house we are incrementing and checking each possibility
  46. while (houses[next_pos] != 0)
  47. {
  48. next_pos = (next_pos + 1) % NUM_HOUSES;
  49.  
  50. // abs() : This function returns absolute value of a number,
  51. // e.g. abs(2) will return 2 , abs(-5) will return 5
  52.  
  53. // So we are just checking if next_position is in a safe distance from cur_pos
  54. if (abs(next_pos - cur_pos) < alert_dis)
  55. return target; // If there are no suitable house return -1, here target is having -1 value
  56. }
  57. target = next_pos;
  58. return target;
  59. }
  60. int main()
  61. {
  62. int houses[NUM_HOUSES] = {0};
  63. int first_target;
  64. int alert_dis;
  65. cout << endl
  66. << "The number of houses in this community is " << NUM_HOUSES << "." << endl;
  67. // input the first target house ID of the thief (current position) and
  68. // the alert distance (min interval) of the security system.
  69. cout << "Please input the first target house ID of the thief: " << endl;
  70. cin >> first_target;
  71. while ((first_target >= NUM_HOUSES) || (first_target < 0))
  72. {
  73. cout << "The target of the thief should less than " << NUM_HOUSES << endl;
  74. cout << "Please re-input the first target ID: " << endl;
  75. cin >> first_target;
  76. }
  77. houses[first_target] = 1; // Set first target to 1
  78. cout << "Please input the alert distance of the community's security system: " << endl;
  79. cin >> alert_dis;
  80. while ((alert_dis >= NUM_HOUSES) || (alert_dis < 0))
  81. {
  82. cout << "The alert distance of the community's security system should less than " << NUM_HOUSES << endl;
  83. cout << "Please re-input the alert distance: " << endl;
  84. cin >> alert_dis;
  85. }
  86. cout << endl;
  87. int cur_pos = first_target;
  88. int night = 1;
  89. print_houses(houses, cur_pos, night);
  90. while (true)
  91. {
  92. // Task 1:
  93. // - update 'night'
  94. // - call 'steal_house( )' function to get the thief's next target and update 'cur_pos'
  95. // - call 'print_houses( )' function to print the states of the houses
  96. // - When the next target is invalid (steal_house( ) return '-1'),
  97. // break out the while loop.
  98.  
  99. // First of all incrementing the value of night by 1, night++ ==> night = night+ 1
  100. night++;
  101.  
  102. // extracting the next_target from steal_house()
  103. int next_target = steal_house(houses, cur_pos, alert_dis, night);
  104.  
  105. // if function returns -1, that means it's invalid. So break the loop
  106. if (next_target == -1)
  107. break;
  108. else
  109. {
  110. // otherwise update the curr position of theif with next_target which will be the new position
  111. cur_pos = next_target;
  112. print_houses(houses, cur_pos, night);
  113. }
  114. }
  115. cout << endl
  116. << "No house can be stolen." << endl
  117. << "Finished." << endl;
  118. return 0;
  119. }
  120.  
Success #stdin #stdout 0.01s 5536KB
stdin
2
2
stdout
The number of houses in this community is 8.
Please input the first target house ID of the thief: 
Please input the alert distance of the community's security system: 

Night 1: 
| 0 | 0 | * | 0 | 0 | 0 | 0 | 0 |
Night 2: 
| 0 | 0 | 1 | 0 | * | 0 | 0 | 0 |
Night 3: 
| 0 | 0 | 1 | 0 | 2 | 0 | * | 0 |
Night 4: 
| * | 0 | 1 | 0 | 2 | 0 | 3 | 0 |
Night 5: 
| 4 | 0 | 1 | * | 2 | 0 | 3 | 0 |
Night 6: 
| 4 | 0 | 1 | 5 | 2 | * | 3 | 0 |
Night 7: 
| 4 | 0 | 1 | 5 | 2 | 6 | 3 | * |
Night 8: 
| 4 | * | 1 | 5 | 2 | 6 | 3 | 7 |

No house can be stolen.
Finished.