一 原题
IV Balkan Olympiad
You've just be assigned the project of implemented a windowing interface. This windowing interface is fairly simple, and fortunately, you don't have to display the actual windows. There are 5 basic operations:
- Create a window
- Bring a window to the top
- Put a window to the bottom
- Destroy a window
- Output what percentage of a window is visible (i.e., isn't covered by windows above it).
In the input, the operations appear in the following format:
- Create window: w(I,x,y,X,Y)
- Bring window to top: t(I)
- Put window on bottom: b(I)
- Destroy window: d(I)
- Output percentage visible: s(I)
(x,y) and (X,Y) are opposite corners of the window. When a window is created, it is put `on top'. You can't create a window with an identifier that is already in use, but you can destroy a window and then create a new one with the identifier of the destroyed window. Coordinates will be positive integers, and all windows will be of non-zero area (x != X and y != Y). The x and y coordinates are between 1 and 32767 inclusive.
PROGRAM NAME: window
INPUT FORMAT
The input file consists of a sequence of commands to your interpreter. They will be listed one per line. Terminate the program when no more input is available
SAMPLE INPUT (file window.in)
w(a,10,132,20,12) w(b,8,76,124,15) s(a)
OUTPUT FORMAT
Output lines only for the s() commands. Of course, there might be several s() commands (but no more than 500) so the output should be a sequence of percentages, one per line, stating the percentage of the windows that are visible. The percentages should be rounded to 3 decimal places.
SAMPLE OUTPUT (file window.out)
49.167
二 分析
三 代码
USER: Qi Shen [maxkibb3] TASK: window LANG: C++ Compiling... Compile: OK Executing... Test 1: TEST OK [0.000 secs, 4188 KB] Test 2: TEST OK [0.000 secs, 4188 KB] Test 3: TEST OK [0.000 secs, 4188 KB] Test 4: TEST OK [0.000 secs, 4188 KB] Test 5: TEST OK [0.000 secs, 4188 KB] Test 6: TEST OK [0.000 secs, 4188 KB] Test 7: TEST OK [0.000 secs, 4188 KB] Test 8: TEST OK [0.000 secs, 4188 KB] Test 9: TEST OK [0.000 secs, 4188 KB] Test 10: TEST OK [0.000 secs, 4188 KB] Test 11: TEST OK [0.000 secs, 4188 KB] All tests OK.
Your program ('window') produced all correct answers! This is your submission #3 for this problem. Congratulations!
/*
ID:maxkibb3
LANG:C++
PROB:window
*/
#include<cstdio>
#include<vector>
#include<cstring>
#include<cstdlib>
#include<cmath>
using namespace std;
struct Window {
char id;
int x1, y1, x2, y2;
double area() {
return (double)abs((x2 - x1) * (y2 - y1));
}
};
Window mk_window(char _id, int _x1,
int _y1, int _x2, int _y2) {
Window ret;
ret.id = _id;
ret.x1 = _x1;
ret.y1 = _y1;
ret.x2 = _x2;
ret.y2 = _y2;
return ret;
}
vector<Window> windows;
int find_window(char _id) {
for(int i = 0; i < windows.size(); i++) {
if(_id == windows[i].id) return i;
}
printf("Shall not see this!\n");
return -1;
}
void create(char *_s) {
int pos[4];
int cnt = 0;
int len = strlen(_s);
for(int i = 2; i < len; i++) {
if(_s[i] == ',') {
pos[cnt++] = i;
_s[i] = '\0';
}
if(_s[i] == ')')
_s[i] = '\0';
}
char id = _s[2];
int x1 = atoi(_s + pos[0] + 1);
int y1 = atoi(_s + pos[1] + 1);
int x2 = atoi(_s + pos[2] + 1);
int y2 = atoi(_s + pos[3] + 1);
if(x1 > x2) swap(x1, x2);
if(y1 < y2) swap(y1, y2);
windows.push_back(mk_window(id, x1, y1, x2, y2));
}
void top(char *_s) {
char id = _s[2];
int idx = find_window(id);
Window w = windows[idx];
windows.erase(windows.begin() + idx);
windows.push_back(w);
}
void bottom(char *_s) {
char id = _s[2];
int idx = find_window(id);
Window w = windows[idx];
windows.erase(windows.begin() + idx);
windows.insert(windows.begin(), w);
}
void destroy(char *_s) {
char id = _s[2];
int idx = find_window(id);
windows.erase(windows.begin() + idx);
}
double up(Window _w, int _idx) {
if(_w.x1 > _w.x2 || _w.y1 < _w.y2) return 0;
if(_idx == windows.size()) return _w.area();
double ret = 0;
Window w = windows[_idx];
ret += up(mk_window('_', _w.x1, _w.y1,
min(_w.x2, w.x1), _w.y2), _idx + 1);
ret += up(mk_window('_', max(_w.x1, w.x2),
_w.y1, _w.x2, _w.y2), _idx + 1);
ret += up(mk_window('_', max(_w.x1, w.x1), _w.y1,
min(w.x2, _w.x2), max(_w.y2, w.y1)), _idx + 1);
ret += up(mk_window('_', max(_w.x1, w.x1),
min(_w.y1, w.y2), min(_w.x2, w.x2), _w.y2), _idx + 1);
return ret;
}
void calc(char *_s) {
char id = _s[2];
int idx = find_window(id);
double seen = up(windows[idx], idx + 1);
printf("%.3f\n", seen / windows[idx].area() * 100);
}
void solve() {
char s[50];
while(scanf("%s", s) != -1) {
if(s[0] == 'w')
create(s);
else if(s[0] == 't')
top(s);
else if(s[0] == 'b')
bottom(s);
else if(s[0] == 'd')
destroy(s);
else
calc(s);
}
}
int main() {
freopen("window.in", "r", stdin);
freopen("window.out", "w", stdout);
solve();
return 0;
}