Herowaree

2046

Nov 25th, 2017
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main() {
  5. long int i, j, m, n, num_hs, num_reqs, min_dis, id_hs;
  6.  
  7. long int k = 1;
  8.  
  9. scanf("%ld %ld", &m, &n);
  10.  
  11. while(m != 0 && n != 0){
  12.  
  13. if(k > 1) printf("\n");
  14.  
  15. scanf("%ld", &num_hs);
  16.  
  17. long int hs_x[num_hs], hs_y[num_hs];
  18.  
  19. for(i = 0; i < num_hs; i++){
  20. scanf("%ld %ld", &hs_x[i], &hs_y[i]);
  21. }
  22.  
  23. scanf("%ld", &num_reqs);
  24.  
  25. long int req_x[num_reqs], req_y[num_reqs];
  26.  
  27. for(i = 0; i < num_reqs; i++){
  28. scanf("%ld %ld", &req_x[i], &req_y[i]);
  29. }
  30.  
  31. printf("Instancia %ld\n", k);
  32. for(i = 0; i < num_reqs; i++){
  33. min_dis = abs(req_x[i] - hs_x[0]) + abs(req_y[i] - hs_y[0]);
  34. id_hs = 0;
  35. for(j = 1; j < num_hs; j++){
  36. if(abs(req_x[i] - hs_x[j]) + abs(req_y[i] - hs_y[j]) < min_dis){
  37. min_dis = abs(req_x[i] - hs_x[j]) + abs(req_y[i] - hs_y[j]);
  38. id_hs = j;
  39. }else if(abs(req_x[i] - hs_x[j]) + abs(req_y[i] - hs_y[j]) == min_dis){
  40. if(hs_x[j] < hs_x[id_hs]){
  41. id_hs = j;
  42. }else if(hs_x[j] == hs_x[id_hs]) {
  43. if(hs_y[j] < hs_y[id_hs]){
  44. id_hs = j;
  45. }
  46. }
  47. }
  48. }
  49.  
  50. printf("%ld %ld\n", hs_x[id_hs], hs_y[id_hs]);
  51. }
  52.  
  53. scanf("%ld %ld", &m, &n);
  54. k++;
  55. }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment