#include <iostream>
#include <string>
using namespace std;

//  A constant for declaring an empty hour for a classroom
const int  EMPTY = -1;

//  How many hours exist in a day
const int  nhours = 3;

inline bool
solve_rec (int course_room[], int course_prof[], int course_hour[],
            int ncourse, bool room_unavail[][nhours],
            bool prof_unavail[][nhours]);

int  main (void)
{
  //  The following names could be used for printing
  //  the timetable on the screen.
  string  room_name[] = {"C1", "C2"};
  string  prof_name[] = {"Einstein", "Godel", "Eco"};
  string  course_name[] = {"Physics", "Physics", "Maths",
       "Maths", "Philosophy", "Philosophy", "Literature"};

  //  Availabilities of our resources
  bool  room_unavail[][nhours] = { {false, false, false},
                                   {false, false, false} };
  bool  prof_unavail[][nhours] = { {false, false, false},
                                   {false, false, false},
                                   {false, false, false} };

  //  Indexes of the corresponding classroom and professor
  //  for each course, and the hour it is scheduled at
  //  (currently EMPTY).
  int  course_room[] = {0, 1, 0, 1, 0, 1, 0};
  int  course_prof[] = {0, 0, 1, 1, 2, 2, 2};
  int  course_hour[] = {EMPTY, EMPTY, EMPTY, EMPTY,
                        EMPTY, EMPTY, EMPTY};

  //  If we change ncourse to 7, the scheduler
  //  will not find any solution.
  int  ncourse = 6;

  if (solve_rec(course_room,course_prof,course_hour,ncourse,
                           room_unavail,prof_unavail) == true)
     cout << "Found a solution\n";
  else
     cout << "Couldn't find a solution\n";
}


bool
solve_rec_body (int course_room[], int course_prof[],
  int course_hour[], int curr_course, int ncourse,
  bool room_unavail[][nhours], bool prof_unavail[][nhours])
{
  //  "Renaming" (for better readability)
  int  l = curr_course;

  //  Check if all the courses have been scheduled
  if (l == ncourse)
    return  true;

  for (int h=0;  h<nhours;  ++h)  {
    if ( !room_unavail[course_room[l]][h]
          &&  !prof_unavail[course_prof[l]][h] )  {

       //  Assign resources and hour
       room_unavail[course_room[l]][h] = true;
       prof_unavail[course_prof[l]][h] = true;
       course_hour[l] = h;

       //  Continue with scheduling the next course
       if (solve_rec_body(course_room,course_prof,course_hour,
                l+1,ncourse,room_unavail,prof_unavail) == true)
                return  true;

       //  Free resources
       room_unavail[course_room[l]][h] = false;
       prof_unavail[course_prof[l]][h] = false;
       course_hour[l] = EMPTY;
    }
  }

  //  Couldn't find an hour, for course `l'
  return  false;
}


inline bool
solve_rec (int course_room[], int course_prof[], int course_hour[],
            int ncourse, bool room_unavail[][nhours],
            bool prof_unavail[][nhours])
{
  return  solve_rec_body(course_room,course_prof,course_hour,
                          0,ncourse,room_unavail,prof_unavail);
}

