-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBridge.cpp
More file actions
154 lines (120 loc) · 2.41 KB
/
Copy pathBridge.cpp
File metadata and controls
154 lines (120 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
Programmers solution using Queue.
Source: https://programmers.co.kr/learn/courses/30/lessons/42583?language=cpp
*/
#define _CRT_SECURE_NO_WARNINGS
#define max_size (100)
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include <stdio.h>
typedef int element;
typedef struct AQueue {
element top;
element rear;
element *contents;
}AQueue;
int is_full(AQueue *q){
return (q->top == (q->rear + 1) % max_size);
}
AQueue create() {
AQueue *queue;
queue = (AQueue*)malloc(sizeof(AQueue));
queue->contents= (element*)malloc(sizeof(element)*max_size);
queue->top = 0;
queue->rear = 0;
return *queue;
}
void push(AQueue *q,element i) {
if (is_full(q)) {
printf("Error: already Full");
}
else {
q->rear++;
q->contents[q->rear%max_size] = i;
}
}
element pop(AQueue *q) {
element tmp;
tmp = q->contents[(q->top+1 )% max_size];
q->top++;
return tmp;
}
element peek(AQueue *q) {
element tmp;
tmp = q->contents[(q->top + 1) % max_size];
return tmp;
}
void printQ(AQueue *q) {
for (int i = q->top + 1; i <= q->rear; i++) {
printf("%d ", q->contents[i%max_size]);
}
printf("\n");
}
element sumTruck(AQueue *q) {
element sum = 0;
for (int i = q->top + 1; i <= q->rear; i++) {
sum += q->contents[i];
}
return sum;
}
void plustime(AQueue *q) {
for (int i = q->top + 1; i <= q->rear; i++) {
q->contents[i]++;
}
}
int main()
{
element max_weight;
element numTruck;
element *arr;
element sec = 0;
element through;
printf("number of truck: ");
scanf("%d", &numTruck);
printf("bridge length: ");
scanf("%d", &through);
printf("max weight: ");
scanf("%d", &max_weight);
arr = (element*)malloc(sizeof(element)*numTruck);
AQueue onbridge, waiting, timearr;
onbridge = create();
waiting = create();
timearr = create();
printf("Insert trucks' weight: ");
for (int i = 0; i < numTruck; i++) {
scanf("%d", &arr[i]);
push(&waiting, arr[i]);
}
push(&onbridge, pop(&waiting));
push(&timearr, 1);
sec++;
while (onbridge.rear != (onbridge.top) )
{
if (peek(&timearr) == through) {
pop(&timearr);
pop(&onbridge);
}
if (waiting.top == (waiting.rear))
{
plustime(&timearr);
sec++;
}
else {
if (sumTruck(&onbridge) + peek(&waiting) <= max_weight)
{
plustime(&timearr);
push(&onbridge, pop(&waiting));
push(&timearr, 1);
sec++;
}
else
{
plustime(&timearr);
sec++;
}
}
}
printf("\n%d\n", sec);
return 0;
}