/* CODE */
#include<stdio.h>
#include<stdlib.h>
#define DEBUG 0
#define MAX_TIME 1440
typedef struct time
{
int start_time;
int end_time;
struct time *next;
}TIME;
typedef enum EVENT_TYPE
{
EVENT_TYPE_A,
EVENT_TYPE_B,
EVENT_TYPE_C,
EVENT_TYPE_D,
EVENT_TYPE_E,
EVENT_TYPE_F
}EVENT_TYPE;
typedef enum FLAG_TYPE
{
FALSE,
TRUE
}FLAG_TYPE;
void print_free_slots(TIME *root, int duration);
void adjust_time_link(TIME *root,int start_time, int end_time);
void print_link (TIME * root);
EVENT_TYPE check_event_type(int old_start,int old_end,int new_start,int new_end);
main()
{
int k,duration,input_time,start_time,end_time;
TIME *root;
root = (TIME *) malloc(sizeof(TIME));
root->next = NULL;
root->start_time = -1;
root->end_time = -1;
scanf("%d%d",&k,&duration);
while(k)
{
k--;
scanf("%d",&input_time);
start_time = input_time*60;
scanf("%d",&input_time);
start_time = (start_time+input_time)%MAX_TIME;
scanf("%d",&input_time);
end_time = input_time*60;
scanf("%d",&input_time);
end_time =(end_time+input_time)%MAX_TIME;
if(start_time < end_time)
{
adjust_time_link(root, start_time, end_time);
}
else if(start_time > end_time)
{
if(end_time != 0 )
{
adjust_time_link(root, 0, end_time);
}
adjust_time_link(root, start_time, MAX_TIME);
}
#if DEBUG
print_link ( root);
#endif
}
print_free_slots(root->next,duration);
#if DEBUG
getchar();getchar();
#endif
return 0;
}
void adjust_time_link(TIME *root,int start_time, int end_time)
{
TIME *temp,*prv_temp,*new_temp;
EVENT_TYPE event_type;
FLAG_TYPE flag = TRUE;
if(root->next == NULL)
{
new_temp = (TIME *) malloc(sizeof(TIME));
new_temp->next = NULL;
new_temp->start_time = start_time;
new_temp->end_time = end_time;
root->next = new_temp;
}
else
{
temp = root->next;
prv_temp = root;
while(flag == TRUE)
{
event_type = check_event_type(temp->start_time,temp->end_time,start_time,end_time);
flag = FALSE;
switch(event_type)
{
case EVENT_TYPE_A:
new_temp = (TIME *) malloc(sizeof(TIME));
new_temp->start_time = start_time;
new_temp->end_time = end_time;
prv_temp->next = new_temp;
new_temp->next = temp;
break;
case EVENT_TYPE_B:
temp->start_time = start_time;
break;
case EVENT_TYPE_C:
if(temp->next == NULL)
{
temp->start_time = start_time;
temp->end_time = end_time;
}
else
{
temp = temp->next;
free(prv_temp->next);
prv_temp->next = temp;
flag = TRUE;
}
break;
case EVENT_TYPE_D:
if(temp->next == NULL)
{
temp->end_time = end_time;
}
else
{
start_time = temp->end_time;
prv_temp = temp;
temp = temp->next;
flag = TRUE;
}
break;
case EVENT_TYPE_E:
/*No change */
break;
case EVENT_TYPE_F:
if(temp->next == NULL)
{
new_temp = (TIME *) malloc(sizeof(TIME));
new_temp->start_time = start_time;
new_temp->end_time = end_time;
temp->next = new_temp;
new_temp->next = NULL;
}
else
{
prv_temp = temp;
temp = temp->next;
flag =TRUE;
}
break;
default:
break;
}
}
}
return;
}
EVENT_TYPE check_event_type(int old_start,int old_end,int new_start,int new_end)
{
EVENT_TYPE event_type;
if(new_start < old_start)
{
if(new_end < old_start)
{
event_type = EVENT_TYPE_A;
}
else if((new_end >= old_start) && (new_end < old_end))
{
event_type = EVENT_TYPE_B;
}
else
{
event_type = EVENT_TYPE_C;
}
}
else if((new_start >= old_start) && (new_start < old_end))
{
if(new_end >= old_end)
{
event_type = EVENT_TYPE_D;
}
else
{
event_type = EVENT_TYPE_E;
}
}
else
{
event_type = EVENT_TYPE_F;
}
return event_type;
}
void print_free_slots(TIME *root,int duration)
{
int start_time = 0;
while(root!=NULL)
{
if((root->start_time - start_time)>=duration)
{
printf("%02d %02d %02d %02d\n",start_time/60,start_time%60,(root->start_time/60)%24,root->start_time%60);
}
start_time = root->end_time;
root = root->next;
}
if(start_time != MAX_TIME)
{
if((MAX_TIME - start_time)>=duration)
{
printf("%02d %02d %02d %02d\n",start_time/60,start_time%60,(MAX_TIME/60)%24,MAX_TIME%60);
}
}
return;
}
#if DEBUG
void print_link (TIME * root)
{
while(root !=NULL)
{
printf("%02d %02d %02d %02d\n",root->start_time/60,root->start_time%60,root->end_time/60,root->end_time%60);
root= root->next;
}
printf("\n");
}
#endif
No comments:
Post a Comment