#include <iostream>
#include <cmath>
using namespace std;
const int NUM_HOUSES = 8;
void print_houses(int houses[], int cur_pos, int night)
{
// Task 3:
// print the state of each house in current night.
// '*': thief's position, '1, 2, 3, ...': stolen houses, '0': valid houses.
// example: // Night 1: // | 0 | 0 | * | 0 | 0 | 0 | 0 | 0 |
cout << "Night " << night << ": " << endl;
cout << "|";
for (int idx = 0; idx < NUM_HOUSES; idx++)
{
if (idx == cur_pos)
{
// In cur_pos we need to print * which is the thief's position
cout << " * |";
}
else
{
// otherwise we can print the array content
cout << " " << houses[idx] << " |";
}
}
cout << endl;
}
int steal_house(int houses[], int cur_pos, int alert_dis, int night)
{
// Task 2:
// output the next target. When there is no valid house to steal, return -1.
int target = -1;
// First of all set the value of the house that thief stoled to night - 1
houses[cur_pos] = night - 1;
// Normally next_target would be [cur_pos + alert_dis]
// But as it is a circular array so we need to bound within the limits,
// e.g in this case index can't go out of bound i.e. idx<=7
// So we are taking the reminder after dividing it by 8
int next_pos = (cur_pos + alert_dis) % NUM_HOUSES;
// To select the suitable next house we are incrementing and checking each possibility
while (houses[next_pos] != 0)
{
next_pos = (next_pos + 1) % NUM_HOUSES;
// abs() : This function returns absolute value of a number,
// e.g. abs(2) will return 2 , abs(-5) will return 5
// So we are just checking if next_position is in a safe distance from cur_pos
if (abs(next_pos - cur_pos) < alert_dis)
return target; // If there are no suitable house return -1, here target is having -1 value
}
target = next_pos;
return target;
}
int main()
{
int houses[NUM_HOUSES] = {0};
int first_target;
int alert_dis;
cout << endl
<< "The number of houses in this community is " << NUM_HOUSES << "." << endl;
// input the first target house ID of the thief (current position) and
// the alert distance (min interval) of the security system.
cout << "Please input the first target house ID of the thief: " << endl;
cin >> first_target;
while ((first_target >= NUM_HOUSES) || (first_target < 0))
{
cout << "The target of the thief should less than " << NUM_HOUSES << endl;
cout << "Please re-input the first target ID: " << endl;
cin >> first_target;
}
houses[first_target] = 1; // Set first target to 1
cout << "Please input the alert distance of the community's security system: " << endl;
cin >> alert_dis;
while ((alert_dis >= NUM_HOUSES) || (alert_dis < 0))
{
cout << "The alert distance of the community's security system should less than " << NUM_HOUSES << endl;
cout << "Please re-input the alert distance: " << endl;
cin >> alert_dis;
}
cout << endl;
int cur_pos = first_target;
int night = 1;
print_houses(houses, cur_pos, night);
while (true)
{
// Task 1:
// - update 'night'
// - call 'steal_house( )' function to get the thief's next target and update 'cur_pos'
// - call 'print_houses( )' function to print the states of the houses
// - When the next target is invalid (steal_house( ) return '-1'),
// break out the while loop.
// First of all incrementing the value of night by 1, night++ ==> night = night+ 1
night++;
// extracting the next_target from steal_house()
int next_target = steal_house(houses, cur_pos, alert_dis, night);
// if function returns -1, that means it's invalid. So break the loop
if (next_target == -1)
break;
else
{
// otherwise update the curr position of theif with next_target which will be the new position
cur_pos = next_target;
print_houses(houses, cur_pos, night);
}
}
cout << endl
<< "No house can be stolen." << endl
<< "Finished." << endl;
return 0;
}