Advertisement
Guest User

Untitled

a guest
Feb 11th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.91 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. class Werhouse{
  4. public int x;
  5. public int y;
  6. public int[] storage;
  7.  
  8. public Werhouse(int xx, int yy){
  9. x= xx;
  10. y=yy;
  11. storage = new int[Main.PRODUCT_TYPES];
  12. }
  13.  
  14. public int items(){
  15. int sum = 0;
  16. for(int i:storage){
  17. sum+=i;
  18. }
  19. return sum;
  20. }
  21.  
  22. }
  23.  
  24. class Order{
  25. public int x;
  26. public int y;
  27. HashMap<Integer, Integer> products;
  28.  
  29. public Order(int xx, int yy, HashMap<Integer, Integer> hash){
  30. x = xx;
  31. y = yy;
  32. products = hash;
  33. }
  34. }
  35.  
  36. class Drone{
  37. public int currentWerhouse;
  38. public int currentLoad;
  39. public int wastedTurns;
  40. LinkedList<Integer> items;
  41. LinkedList<Integer> locations;
  42.  
  43. public Drone(){
  44. currentWerhouse = 0;
  45. wastedTurns = 0;
  46. currentLoad = 0;
  47. items = new LinkedList<>();
  48. locations = new LinkedList<>();
  49. }
  50.  
  51. // class DistanceComparator implements Comparator<Werhouse>{
  52. // int x;
  53. // int y;
  54. //
  55. // public DistanceComparator(int x2, int y2) {
  56. // x = x2;
  57. // y = y2;
  58. // }
  59. //
  60. // @Override
  61. // public int compare(Werhouse first, Werhouse second) {
  62. // int dist = Main.distance(x, y, first.x, first.y);
  63. // int dist2 = Main.distance(x, y, second.x, second.y);
  64. // if(first.items()==0) dist = Integer.MAX_VALUE;
  65. // if(second.items()==0) dist2 = Integer.MAX_VALUE;
  66. //
  67. // return dist - dist2;
  68. // }
  69. // }
  70.  
  71. // public Werhouse findClosestWerhouse(int x, int y) {
  72. // TreeSet<Werhouse> houses= new TreeSet<Werhouse>(new DistanceComparator(x, y));
  73. //
  74. // for(int i = 0; i<Werhouse werhouse : Main.werhouses){
  75. // houses.add(werhouse);
  76. // }
  77. //
  78. // return houses.first();
  79. //
  80. // }
  81.  
  82. public void deliver(int indeks) {
  83. int currentX = Main.werhouses[currentWerhouse].x;
  84. int currentY = Main.werhouses[currentWerhouse].y;
  85. while (!locations.isEmpty()) {
  86. int tmp = locations.removeFirst();
  87. int newX = Main.orders[tmp].x;
  88. int newY = Main.orders[tmp].y;
  89.  
  90. wastedTurns += Main.distance(currentX, currentY, newX, newY);
  91. currentX = newX;
  92. currentY = newY;
  93. Main.log.append(String.format("%d D %d 1 %d\n", indeks, tmp, items.removeFirst()));
  94. Main.naredbi++;
  95. }
  96. //nov werhouse
  97. //TODO: print naredba
  98. Random r = new Random();
  99. currentWerhouse = r.nextInt(Main.werhouses.length);
  100. wastedTurns += Main.distance(currentX, currentY, Main.werhouses[currentWerhouse].x, Main.werhouses[currentWerhouse].y);
  101. }
  102. }
  103.  
  104. public class Main {
  105. public static int PRODUCT_TYPES;
  106. public static Werhouse[] werhouses;
  107. public static Order[] orders;
  108. public static StringBuilder log;
  109. public static int naredbi;
  110.  
  111. public static int distance(int x, int y, int xx, int yy) {
  112. xx-=x;
  113. xx = Math.abs(xx);
  114. xx*=xx;
  115. yy-=y;
  116. yy = Math.abs(yy);
  117. yy *=yy;
  118. return (int) Math.ceil(Math.sqrt(xx + yy)) ;
  119. }
  120.  
  121. public static void main(String[] args){
  122. Scanner sc = new Scanner(System.in);
  123. int H, W, NUM_DRONES, TURNS, PAYLOAD, NUM_WERHOUSE;
  124. H = sc.nextInt();
  125. W = sc.nextInt();
  126. NUM_DRONES = sc.nextInt();
  127. Drone[] drones = new Drone[NUM_DRONES];
  128. for(int i = 0; i<NUM_DRONES; i++){
  129. drones[i] = new Drone();
  130. }
  131. TURNS = sc.nextInt();
  132. PAYLOAD = sc.nextInt();
  133. PRODUCT_TYPES = sc.nextInt();
  134.  
  135. int[] weights = new int[PRODUCT_TYPES];
  136. for(int i = 0; i<PRODUCT_TYPES; i++){
  137. weights[i] = sc.nextInt();
  138. }
  139.  
  140. NUM_WERHOUSE= sc.nextInt();
  141. werhouses = new Werhouse[NUM_WERHOUSE];
  142. for(int i = 0; i<NUM_WERHOUSE; i++){
  143. int x, y;
  144. x = sc.nextInt();
  145. y = sc.nextInt();
  146. werhouses[i] = new Werhouse(x, y);
  147. for(int j = 0; j<PRODUCT_TYPES; j++){
  148. werhouses[i].storage[j] = sc.nextInt();
  149. }
  150. }
  151.  
  152. int NUM_ORDERS;
  153. NUM_ORDERS = sc.nextInt();
  154. orders = new Order[NUM_ORDERS];
  155. for(int i = 0; i<NUM_ORDERS; i++){
  156. int x = sc.nextInt();
  157. int y = sc.nextInt();
  158. int n = sc.nextInt();
  159. HashMap<Integer, Integer> hash = new HashMap<>();
  160. for(int j = 0; j<n; j++){
  161. int tmp = sc.nextInt();
  162. if(hash.containsKey(tmp)){
  163. Integer it = hash.remove(tmp);
  164. hash.put(tmp, it + 1);
  165. }
  166. else{
  167. hash.put(tmp, 1);
  168. }
  169. }
  170. orders[i] = new Order(x, y, hash);
  171. }
  172.  
  173. //algoritam
  174.  
  175. log = new StringBuilder();
  176. naredbi = 0;
  177.  
  178. for(int i = 0; i < NUM_DRONES; i++){ //za seokoj dron
  179. Drone currentDrone = drones[i];
  180. while(currentDrone.wastedTurns < TURNS){ //dodeka ima benzin
  181. Werhouse currentWerhouse = werhouses[currentDrone.currentWerhouse];
  182. for(int j = 0; j < PRODUCT_TYPES; j++){ //za sekoj item
  183. if(currentWerhouse.storage[j] > 0){ // ako go ima vo currentWehouse
  184.  
  185. for(int k = 0; k<NUM_ORDERS; k++){
  186. Order currentOrder = orders[k];
  187. if(currentOrder.products.containsKey(j) && currentOrder.products.get(j)>0 && PAYLOAD - currentDrone.currentLoad >= weights[i]){
  188. //stavi go itemot vo dronot
  189. currentDrone.currentLoad += weights[i]; //upgrade na tezinata na dronot
  190. // if(currentDrone.items.isEmpty() || currentDrone.items.getLast() != j){
  191. // currentDrone.wastedTurns++;
  192. // }
  193. // else{
  194. // k--;
  195. // }
  196.  
  197. currentDrone.items.addLast(j);
  198. currentDrone.locations.addLast(k);
  199. Integer tmp = currentOrder.products.remove(j);
  200. currentOrder.products.put(j, tmp-1);
  201. currentWerhouse.storage[j]--;
  202.  
  203. log.append(String.format("%d L 1 %d %d\n", i, j, currentDrone.currentWerhouse));
  204. naredbi++;
  205.  
  206. }
  207.  
  208. }
  209. }
  210. }
  211. currentDrone.deliver(i);
  212. }
  213. }
  214. System.out.println(naredbi);
  215. System.out.print(log);
  216. }
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement