Source code for submission s1394

Cockchaf.java

  1. import java.util.HashSet;
  2. import java.util.LinkedList;
  3. import java.util.List;
  4. import java.util.PriorityQueue;
  5. import java.util.Queue;
  6. import java.io.BufferedReader;
  7. import java.io.IOException;
  8. import java.io.InputStreamReader;
  9. import java.util.HashMap;
  10.  
  11. import javax.management.Query;
  12. import javax.print.attribute.HashDocAttributeSet;
  13.  
  14.  
  15. public class Cockchaf {
  16. private static double countTime(Bod start, Bod cil, int speed, int anglespeed, int ax, int ay, int az){
  17. int xdiff = start.x - cil.x;
  18. int ydiff = start.y - cil.y;
  19. int zdiff = start.z - cil.z;
  20. double distance = Math.sqrt(xdiff*xdiff + ydiff*ydiff + zdiff*zdiff);
  21. double alfa = 180 - Math.toDegrees(Math.acos( (ax*cil.x + ay*cil.y + az*cil.z) / (Math.sqrt(ax*ax + ay*ay + az*az)*Math.sqrt(cil.x*cil.x + cil.y*cil.y + cil.z*cil.z)) ));
  22.  
  23. System.out.println((distance / speed) + (alfa / anglespeed));
  24. return (distance / speed) + (alfa / anglespeed);
  25.  
  26. }
  27.  
  28. public static void main(String[] args) throws Exception {
  29. String str;
  30. String[] radek;
  31. int n,s,t;
  32. int[] start = new int[3];
  33. int[] konec = new int[3];
  34. int[] spoje;
  35. HashMap<Bod,List<Bod>> map = new HashMap<Bod,List<Bod>>();
  36. while((str=br.readLine()) != null){
  37. radek = str.split(" ");
  38. n = Integer.parseInt(radek[0]);
  39. s = Integer.parseInt(radek[1]);
  40. t = Integer.parseInt(radek[2]);
  41. spoje = new int[6];
  42.  
  43. str = br.readLine();
  44. radek = str.split(" ");
  45. start[0] = Integer.parseInt(radek[0]);
  46. start[1] = Integer.parseInt(radek[1]);
  47. start[2] = Integer.parseInt(radek[2]);
  48.  
  49. konec[0] = Integer.parseInt(radek[3]);
  50. konec[1] = Integer.parseInt(radek[4]);
  51. konec[2] = Integer.parseInt(radek[5]);
  52. Bod startpoint = new Bod (start[0], start[1], start[2]);
  53. Bod endpoint = new Bod( konec[0], konec[1], konec[2]);
  54.  
  55.  
  56. PriorityQueue<Bod> q = new PriorityQueue<Bod>();
  57. q.add(startpoint);
  58.  
  59. for(int i=0; i<n; i++){
  60. str = br.readLine();
  61. radek = str.split(" ");
  62. spoje[0] = Integer.parseInt(radek[0]);
  63. spoje[1] = Integer.parseInt(radek[1]);
  64. spoje[2] = Integer.parseInt(radek[2]);
  65. spoje[3] = Integer.parseInt(radek[3]);
  66. spoje[4] = Integer.parseInt(radek[4]);
  67. spoje[5] = Integer.parseInt(radek[5]);
  68.  
  69. Bod st = new Bod(spoje[0],spoje[1],spoje[2]);
  70. Bod f = new Bod(spoje[3],spoje[4],spoje[5]);
  71.  
  72. if(map.containsKey(s)){
  73. map.get(st).add(f);
  74. }
  75. else{
  76. LinkedList<Bod> l = new LinkedList<Bod>();
  77. l.add(f);
  78.  
  79. if(map.containsKey(f)){
  80.  
  81. }
  82. map.put(st,l);
  83. }
  84.  
  85. if(map.containsKey(f)){
  86. map.get(f).add(st);
  87. }else{
  88. LinkedList<Bod> l = new LinkedList<Bod>();
  89. l.add(st);
  90. map.put(f,l);
  91. }
  92. }
  93.  
  94. boolean firstiter = true;
  95. while(!q.isEmpty()){
  96. Bod bod = q.remove();
  97.  
  98. for(Bod bods: map.get(bod)){
  99. if(firstiter){
  100. bod.ix = bods.x - bod.x;
  101. bod.iy = bods.y - bod.y;
  102. bod.iz = bods.z - bod.z;
  103. firstiter = false;
  104. }
  105.  
  106. double time = countTime(bod, bods, s, t, bod.ix, bod.iy, bod.iz );
  107. bods.time = time < bods.time ? time : bods.time;
  108. q.add(bods);
  109.  
  110. System.out.println(bods);
  111.  
  112. if(bods.equals(endpoint)){
  113. endpoint = bods;
  114. q.clear();
  115. break;
  116. }
  117. }
  118. }
  119.  
  120. System.out.println(endpoint.time);
  121. }
  122.  
  123. br.close();
  124. }
  125.  
  126.  
  127. }
  128.  
  129. class Bod implements Comparable<Bod>{
  130. int x,y,z;
  131. int ix, iy, iz;
  132. double time;
  133. public Bod(int x,int y, int z){
  134. this.x = x;
  135. this.y = y;
  136. this.z = z;
  137. }
  138. @Override
  139. public int hashCode() {
  140. final int prime = 31;
  141. int result = 1;
  142. result = prime * result + x;
  143. result = prime * result + y;
  144. result = prime * result + z;
  145. return result;
  146. }
  147.  
  148. @Override
  149. public int compareTo(Bod b){
  150. return (int)(this.time - b.time);
  151. }
  152.  
  153. @Override
  154. public boolean equals(Object obj) {
  155. if (this == obj)
  156. return true;
  157. if (obj == null)
  158. return false;
  159. if (getClass() != obj.getClass())
  160. return false;
  161. Bod other = (Bod) obj;
  162. if (x != other.x)
  163. return false;
  164. if (y != other.y)
  165. return false;
  166. if (z != other.z)
  167. return false;
  168. return true;
  169. }
  170. @Override
  171. public String toString() {
  172. return "Bod [x=" + x + ", y=" + y + ", z=" + z
  173. +" ix: "+ix+" iy "+iy+" iz "+iz +", time=" + time + "]";
  174. }
  175.  
  176.  
  177. }