Advertisement
Guest User

Untitled

a guest
Jan 17th, 2016
387
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 3.89 KB | None | 0 0
  1. let node_exists map x =
  2.   List.exists (fun (x1, _, _) -> x = x1) map
  3.  
  4. let get_map ln gwn =
  5.   let rec f1 cnt acc =
  6.     if cnt <= 0 then acc
  7.     else
  8.       let x, y = Scanf.sscanf (input_line stdin) "%d %d" (fun n1 n2 -> (n1, n2)) in
  9.       let acc = if (node_exists acc x)
  10.                   then
  11.                     let acc = List.map (fun (x1, links, gw) -> if x = x1 then x1, (y :: links), gw else (x1, links, gw)) acc in
  12.                     if (node_exists acc y)
  13.                       then List.map (fun (x1, links, gw) -> if y = x1 then x1, (x :: links), gw else (x1, links, gw)) acc
  14.                       else ((y, [x], false) :: acc)
  15.                   else if (node_exists acc y)
  16.                     then List.map (fun (x1, links, gw) -> if y = x1 then x1, (x:: links), gw else (x1, links, gw)) acc
  17.                     else ((y, [x], false) :: (x, [y], false) :: acc) in
  18.       f1 (cnt - 1) acc in
  19.   let map = f1 ln [] in
  20.   let rec f2 cnt acc =
  21.     if cnt <= 0 then acc
  22.     else
  23.       let v = int_of_string (input_line stdin) in
  24.       f2 (cnt - 1) (v :: acc) in
  25.   let gateways = f2 gwn [] in
  26.   let into_gateways (x, links, _) =
  27.     let is_gw = List.mem x gateways in
  28.     (x, links, is_gw) in
  29.   List.map into_gateways map
  30. ;;
  31.  
  32. let l, e = Scanf.sscanf (input_line stdin) "%d %d %d" (fun _ l e -> (l, e)) in
  33. (* l: the number of links *)
  34. (* e: the number of exit gateways *)
  35.  
  36. let map = get_map l e in
  37.  
  38.  
  39. (* game loop *)
  40. while true do
  41.     let si = int_of_string (input_line stdin) in (* The index of the node on which the Skynet agent is positioned this turn *)
  42.  
  43.     (* Write an action using print_endline *)
  44.     (* To debug: prerr_endline "Debug message"; *)
  45.  
  46.     print_endline "1 2"; (* Example: 0 1 are the indices of the nodes you wish to sever the link between *)
  47.     ();
  48. done;
  49.  
  50. (*
  51.  Your virus has caused a backdoor to open on the Skynet network enabling you to send new instructions in real time.
  52.  
  53. You decide to take action by stopping Skynet from communicating on its own internal network.
  54.  
  55. Skynet's network is divided into several smaller networks, in each sub-network is a Skynet agent tasked with transferring information by moving from node to node along links and accessing gateways leading to other sub-networks.
  56.  
  57. Your mission is to reprogram the virus so it will sever links in such a way that the Skynet Agent is unable to access another sub-network thus preventing information concerning the presence of our virus to reach Skynet's central hub.
  58.   Rules
  59. For each test you are given:
  60.  
  61.     A map of the network.
  62.     The position of the exit gateways.
  63.     The starting position of the Skynet agent.
  64.  
  65. >>> Nodes can only be connected to up to a single gateway. <<<
  66.  
  67. Each game turn:
  68.  
  69.     First off, you sever one of the given links in the network.
  70.     Then the Skynet agent moves from one Node to another accessible Node.
  71.  
  72. Skynet agent
  73.    
  74. Gateway
  75. Victory Conditions
  76. The Skynet agent cannot reach an exit gateway
  77. Lose Conditions
  78. The Skynet agent has reached a gateway
  79.   Example
  80.  
  81. 4 4 1
  82. 0 1
  83. 0 2
  84. 1 3
  85. 2 3
  86. 3
  87.  
  88. Initialization input
  89.  
  90. Text representation of the network used in this example. There are 4 nodes, 4 links and 1 gateway. The next 4 lines describe the links. The last integer is the index of the exit node.
  91. Turn 1
  92. The Skynet agent starts at node 0 (SI = 0). Our virus cut the link between the nodes 1 and 3.
  93. Turn 2
  94. The Skynet agent moves to node 2 (SI = 2). Our virus cut the link between the nodes 2 and 3.
  95. Turn 3
  96. The Skynet agent has been cut off from the exit, you have won !
  97.  
  98. Initialization input
  99.  
  100. Line 1: 3 integers N L E
  101. - N, the total number of nodes in the level, including the gateways.
  102. - L, the number of links in the level.
  103. - E, the number of exit gateways in the level.
  104.  
  105. Next L lines: 2 integers per line (N1, N2), indicating a link between the nodes indexed N1 and N2 in the network.
  106.  
  107. Next E lines: 1 integer EI representing the index of a gateway node.
  108.  
  109. *)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement