-
Notifications
You must be signed in to change notification settings - Fork 0
/
Prof_22July_main.cpp
206 lines (174 loc) · 4.24 KB
/
Prof_22July_main.cpp
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <stdio.h>
#include <stdlib.h>
#define MAX_ID 100
#define MAX_STR_LENGTH 3000
#define MAX_IMAGE_HEIGHT 50
#define MAX_IMAGE_WIDTH 50
#define MAX_SCREEN_HEIGHT 1000
#define MAX_SCREEN_WIDTH 1000
#define THRESHOLD 0x07FFFFFF
enum Command {
INIT = 0,
CREATE,
ADD,
SHOW,
};
enum Type {
VWIDGET = 0,
HWIDGET,
TEXT,
IMAGE,
};
typedef struct {
int type;
union {
struct { int width; } vwidget;
struct { int height; } hwidget;
struct { char *strPtr; } text;
struct { int height, width; char *imagePtr; } image;
};
} Element;
extern void init();
extern int create(Element* elementPtr);
extern void add(int parentId, int childId);
extern void show(int elementId, char screen[MAX_SCREEN_HEIGHT][MAX_SCREEN_WIDTH]);
static int id[MAX_ID + 1];
static int point, expected;
static char baseStr[MAX_STR_LENGTH];
static char baseImage[MAX_IMAGE_HEIGHT][MAX_IMAGE_WIDTH];
static char bufStr[MAX_STR_LENGTH];
static char bufImage[MAX_IMAGE_HEIGHT * MAX_IMAGE_WIDTH];
static char screen[MAX_SCREEN_HEIGHT][MAX_SCREEN_WIDTH];
static void loadBase() {
int N, H, W;
scanf("%d", &N);
int idx = 0;
while (idx < N) {
scanf("%c", &baseStr[idx]);
if (baseStr[idx] != '\n' && baseStr[idx] != '\r') ++idx;
}
scanf("%d %d", &H, &W);
for (int y = 0; y < H; ++y) {
int x = 0;
while (x < W) {
scanf("%c", &baseImage[y][x]);
if (baseImage[y][x] != '\n' && baseImage[y][x] != '\r') ++x;
}
}
}
static void readElement(Element *elementPtr) {
int x1, y1, x2, y2, idx;
switch (elementPtr->type) {
case VWIDGET:
scanf("%d", &(elementPtr->vwidget.width));
break;
case HWIDGET:
scanf("%d", &(elementPtr->hwidget.height));
break;
case TEXT:
scanf("%d %d", &x1, &x2);
idx = 0;
for (int x = x1; x <= x2; ++x)
bufStr[idx++] = baseStr[x];
bufStr[idx] = '\0';
elementPtr->text.strPtr = bufStr;
break;
case IMAGE:
scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
idx = 0;
for (int y = y1; y <= y2; ++y)
for (int x = x1; x <= x2; ++x)
bufImage[idx++] = baseImage[y][x];
elementPtr->image.height = y2 - y1 + 1;
elementPtr->image.width = x2 - x1 + 1;
elementPtr->image.imagePtr = bufImage;
break;
default:
break; // never executed
}
}
static int calcHash(char screen[MAX_SCREEN_HEIGHT][MAX_SCREEN_WIDTH], int height, int width) {
int ret = 0, count = 1;
for (int y = 0; y < height; ++y)
for (int x = 0; x < width; ++x) {
ret += screen[y][x] * count++;
ret &= THRESHOLD;
}
return ret;
}
static void init_m() {
for (int idx = 0; idx <= MAX_ID; ++idx)
id[idx] = -1;
point = 0;
}
static void run() {
int lineN;
scanf("%d", &lineN);
for (int line = 0; line < lineN; ++line) {
int command;
int parentId, childId, elementId;
int height, width;
int hash, resultHash;
Element element;
scanf("%d", &command);
switch (command) {
case INIT:
scanf("%d", &expected);
init();
break;
case CREATE:
scanf("%d %d", &elementId, &(element.type));
readElement(&element);
id[elementId] = create(&element);
break;
case ADD:
scanf("%d %d", &parentId, &childId);
add(id[parentId], id[childId]);
break;
case SHOW:
scanf("%d %d %d %d", &elementId, &height, &width, &hash);
show(id[elementId], screen);
resultHash = calcHash(screen, height, width);
#if 0
printf ("Expected W %d H %d\n", width, height);
for (int y = 0; y < height; ++y)
{
for (int x = 0; x < width; ++x)
printf("%c", screen[y][x]);
printf("\n");
}
#endif
if (resultHash == hash)
{
point++;
// printf ("%d \n", point);
}
else
{
printf("FAILED %d %d\n", hash, resultHash);
exit (0);
return;
}
break;
}
}
}
int main() {
int T, total_score;
setbuf(stdout, NULL);
//freopen("sample_input.txt", "r", stdin);
scanf("%d", &T);
loadBase();
total_score = 0;
for (int testcase = 1; testcase <= T; ++testcase) {
init_m();
run();
printf("#%d %d\n", testcase, point);
if (point == expected) total_score++;
}
printf("total score = %d\n", total_score * 100 / T);
return 0;
}