Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 KB | None | 0 0
  1. import java.awt.Point;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.io.PrintStream;
  5. import java.util.ArrayList;
  6. import java.util.Arrays;
  7. import java.util.HashMap;
  8. import java.util.Scanner;
  9.  
  10.  
  11. public class E {
  12. Scanner in;
  13. boolean map[][];
  14. HashMap<Character, Char> chars;
  15.  
  16. public static void main(String args[]) throws IOException {
  17. new E().doIt();
  18. }
  19.  
  20. public void doIt() throws IOException {
  21. in = new Scanner(new File("E.in"));
  22. PrintStream out = new PrintStream("E.out");
  23.  
  24. int tc = in.nextInt();
  25. int n;
  26. for(int c=1; c<=tc; c++) {
  27. n = in.nextInt();
  28. chars = new HashMap<Character, Char>();
  29.  
  30. map = new boolean[in.nextInt()][in.nextInt()];
  31. for(boolean[] mr: map) {
  32. Arrays.fill(mr,true);
  33. }
  34.  
  35. String line = in.nextLine();
  36. // System.out.println(line + "\t" + map.length + "\t" + map.length);
  37. for(int i=0; i<map.length; i++) {
  38. line = in.nextLine();
  39. // System.out.println(line);
  40. for(int j=0; j<map[0].length; j++) {
  41. if(line.charAt(j) == '#') {
  42. map[i][j] = false;
  43. continue;
  44. }
  45. if(line.charAt(j) != '.') {
  46. chars.put(line.charAt(j), new Char(j, i));
  47. }
  48. }
  49. }
  50.  
  51. String s;
  52. char character;
  53. for(int i=0; i<n; i++) {
  54. s = in.next();
  55. character = s.charAt(0);
  56. // System.out.println(s + character);
  57. chars.get(character).read();
  58. }
  59.  
  60. int time = in.nextInt();
  61.  
  62. for(Character ch : chars.keySet()) {
  63. chars.get(ch).move(time);
  64. }
  65.  
  66. char[][] fin = new char[map.length][map[0].length];
  67. for(char[] ch : fin) {
  68. Arrays.fill(ch, '.');
  69. }
  70. for(int i=0; i<map.length; i++) {
  71. for(int j=0; j<map[0].length; j++) {
  72. if(!map[i][j]) fin[i][j] = '#';
  73. }
  74. }
  75.  
  76. for(Character ch : chars.keySet()) {
  77. System.out.println(ch);
  78. System.out.println(chars.get(ch).pos);
  79. fin[chars.get(ch).pos.y][chars.get(ch).pos.x] = ch;
  80. }
  81.  
  82. out.println("DATA SET #" + c);
  83. for(int i=0; i<map.length; i++) {
  84. for(int j=0; j<map[0].length; j++) {
  85. out.print(fin[i][j]);
  86. }
  87. out.println();
  88. }
  89. }
  90. }
  91.  
  92. boolean valid(Point p) {
  93. if(p.x < 0 || p.y < 0 || p.y >= map.length || p.x >= map[0].length) {
  94. return false;
  95. }
  96. return map[p.y][p.x];
  97. }
  98.  
  99. class Char {
  100. ArrayList<String> moves;
  101. Point pos;
  102.  
  103. Char(int x, int y) {
  104. moves = new ArrayList<String>();
  105. pos = new Point(x, y);
  106. }
  107.  
  108. void read() {
  109. int l = in.nextInt();
  110. String t;
  111. for(int i=0; i<l; i++) {
  112. t = in.next();
  113. for(int j=in.nextInt(); j>=0; j--) {
  114. moves.add(t);
  115. }
  116. }
  117.  
  118. Point np = new Point(pos.x, pos.y);
  119. Point nnp = new Point(pos.x, pos.y);
  120.  
  121. for(int i=0; i<moves.size(); i++) {
  122. t = moves.get(i);
  123.  
  124. if(t.equals("EAST"))
  125. nnp.x++;
  126. else if(t.equals("WEST"))
  127. nnp.x--;
  128. else if(t.equals("NORTH"))
  129. nnp.y--;
  130. else if(t.equals("SOUTH"))
  131. nnp.y++;
  132.  
  133. if(!valid(nnp)) {
  134. nnp.x = np.x;
  135. nnp.y = np.y;
  136. moves.set(i, "PAUSE");
  137. } else {
  138. np.x = nnp.x;
  139. np.y = nnp.y;
  140. }
  141. }
  142.  
  143. if(!pos.equals(np)) {
  144. for(int i=moves.size()-1; i >= 0; i--) {
  145. moves.add(moves.get(i));
  146. }
  147. }
  148. }
  149.  
  150. void move(int time) {
  151. time %= moves.size();
  152.  
  153. String t;
  154. for(int i=0; i< time; i++) {
  155. t = moves.get(i);
  156. if(t.equals("EAST"))
  157. pos.x++;
  158. else if(t.equals("WEST"))
  159. pos.x--;
  160. else if(t.equals("NORTH"))
  161. pos.y--;
  162. else if(t.equals("SOUTH"))
  163. pos.y++;
  164. }
  165. }
  166. }
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement