fork download
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<math.h>
  4.  
  5. typedef struct tag_point_t
  6. {
  7. double x, y;
  8. }point_t;
  9.  
  10. #define POINT_NUM 30
  11.  
  12. double calc_distance(const point_t *p, const point_t *q)
  13. {
  14. double dx, dy;
  15.  
  16. dx=p->x-q->x;
  17. dy=p->y-q->y;
  18. return sqrt(dx*dx+dy*dy);
  19. }
  20.  
  21. int main(void)
  22. {
  23. point_t point[POINT_NUM];
  24. unsigned long connection[POINT_NUM], A_connection;
  25. int i, j, is_changed, A, B;
  26.  
  27. for(i=0;i<POINT_NUM;i++)
  28. {
  29. point[i].x=rand()/(RAND_MAX+1.0)*10.0;
  30. point[i].y=rand()/(RAND_MAX+1.0)*10.0;
  31. connection[i]=0;
  32. }
  33.  
  34. A=rand()%POINT_NUM;
  35. do
  36. {
  37. B=rand()%POINT_NUM;
  38. }while(A==B);
  39.  
  40. for(i=0;i<POINT_NUM;i++)
  41. {
  42. for(j=i;j<POINT_NUM;j++)
  43. {
  44. if(calc_distance(&point[i], &point[j])<=10.0)
  45. {
  46. connection[i]|=(1<<j);
  47. connection[j]|=(1<<i);
  48. }
  49. }
  50. }
  51.  
  52. A_connection=connection[A];
  53. do
  54. {
  55. is_changed=0;
  56. for(i=0;i<POINT_NUM;i++)
  57. {
  58. if(((A_connection>>i)&1) && ((A_connection&connection[i])!=connection[i]))
  59. {
  60. A_connection|=connection[i];
  61. is_changed=1;
  62. }
  63. }
  64. }while(is_changed);
  65.  
  66. for(i=0;i<POINT_NUM;i++)
  67. {
  68. printf("%2d : (%f,%f)\n", i, point[i].x, point[i].y);
  69. }
  70. printf("A=%d B=%d\n", A, B);
  71.  
  72. printf("移動%s\n", ((A_connection>>B)&1)?"可能":"不可能");
  73.  
  74. return 0;
  75. }
  76.  
Success #stdin #stdout 0.02s 1676KB
stdin
Standard input is empty
stdout
 0 : (8.401877,3.943829)
 1 : (7.830992,7.984400)
 2 : (9.116474,1.975514)
 3 : (3.352228,7.682296)
 4 : (2.777747,5.539700)
 5 : (4.773971,6.288709)
 6 : (3.647845,5.134009)
 7 : (9.522297,9.161951)
 8 : (6.357117,7.172969)
 9 : (1.416026,6.069689)
10 : (0.163006,2.428868)
11 : (1.372316,8.041768)
12 : (1.566791,4.009444)
13 : (1.297904,1.088088)
14 : (9.989245,2.182569)
15 : (5.129324,8.391122)
16 : (6.126398,2.960316)
17 : (6.375523,5.242872)
18 : (4.935830,9.727750)
19 : (2.925168,7.713577)
20 : (5.267450,7.699138)
21 : (4.002286,8.915295)
22 : (2.833147,3.524583)
23 : (8.077245,9.190265)
24 : (0.697553,9.493271)
25 : (5.259953,0.860558)
26 : (1.922138,6.632269)
27 : (8.902326,3.488929)
28 : (0.641713,0.200230)
29 : (4.577017,0.630958)
A=15  B=14
移動可能