Volver a la lista de problemas
Mouse Clicks
142.c
/* 142 - Mouse Clicks */ #include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct { int x; int y; int visible; } icon; typedef struct { int x1,y1; int x2,y2; } region; int nic = 0; icon *ic = NULL; int nre = 0; region *re = NULL; unsigned int dist(int x1, int y1, int x2, int y2) { return (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2); } int inside(int x, int y, int r) { if (x>=re[r].x1 && x<=re[r].x2 && y>=re[r].y1 && y<=re[r].y2) { return 1; } else { return 0; } } int deep_inside(int x, int y, int r) { if (x>re[r].x1 && x<re[r].x2 && y>re[r].y1 && y<re[r].y2) { return 1; } else { return 0; } } void read_icon(void) { int x,y; scanf("%d %d", &x, &y); ic = realloc(ic, (nic+1)*sizeof(icon)); if (!ic) abort(); ic[nic].visible = 1; ic[nic].x = x; ic[nic].y = y; nic++; } void read_region(void) { int x1,y1,x2,y2; scanf("%d %d %d %d", &x1, &y1, &x2, &y2); re = realloc(re, (nre+1)*sizeof(region)); if (!re) abort(); re[nre].x1 = x1; re[nre].y1 = y1; re[nre].x2 = x2; re[nre].y2 = y2; nre++; } void process_mouse(void) { int x,y; int i; unsigned int min = (unsigned int)-1; scanf("%d %d", &x, &y); for (i=nre-1; i>=0; i--) { if (inside(x, y, i)) { printf("%c\n", 'A'+i); return; } } for (i=0; i<nic; i++) { unsigned int d; if (!ic[i].visible) { continue; } d = dist(x,y,ic[i].x,ic[i].y); if (d < min) { min = d; } } for (i=0; i<nic; i++) { if (!ic[i].visible) { continue; } if (min == dist(x,y,ic[i].x,ic[i].y)) { printf("%3d", i+1); } } printf("\n"); } int main(void) { char c; int i,r; int done = 0; while (scanf(" %c", &c)==1 && c!='#') { if (c=='I') { if (done) { abort(); } read_icon(); } else if (c=='R') { if (done) { abort(); } read_region(); } else if (c=='M') { if (!done) { done = 1; for (i=0; i<nic; i++) { for (r=0; r<nre; r++) { if (inside(ic[i].x, ic[i].y, r)) { ic[i].visible = 0; } } } } process_mouse(); } else { abort(); } } return 0; }