Guest User

Untitled

a guest
Apr 26th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.87 KB | None | 0 0
  1. /*
  2. ID: vxsj.fu1
  3. LANG: JAVA
  4. PROG: castle
  5. */
  6.  
  7. import java.io.*;
  8. import java.util.*;
  9.  
  10. public class castle {
  11.  
  12. int[][] map;
  13. ArrayList<Integer>[] graph;
  14. ArrayList<Integer>[] walls;
  15. int compCount;
  16. int[] comps;
  17. int xVal;
  18. int locMax;
  19.  
  20. void floodFill(int index) {
  21. int numVisited = 41;
  22. while (numVisited != 0) {
  23. numVisited = 0;
  24. for (int i = 0; i < graph.length; i++) {
  25. if (comps[i] == -2) {
  26. numVisited++;
  27. comps[i] = index;
  28. for (Integer j : graph[i]) {
  29. if (comps[j] == 0) {
  30. comps[j] = -2;
  31. }
  32. }
  33. i = 0;
  34. }
  35. }
  36. if (numVisited > locMax) {
  37. locMax = numVisited;
  38. }
  39. }
  40. }
  41.  
  42. void findComponent() {
  43. compCount = 0;
  44. locMax = 0;
  45. for (int i = 0; i < graph.length; i++) {
  46. comps[i] = 0;
  47. }
  48. for (int i = 0; i < graph.length; i++) {
  49. if (comps[i] == 0) {
  50. compCount++;
  51. comps[i] = -2;
  52. floodFill(compCount);
  53. }
  54. }
  55. }
  56.  
  57. void getARoom(int wall)
  58. {
  59. locMax = 0;
  60. for (int i = 0; i < graph.length; i++) {
  61. comps[i] = 0;
  62. }
  63. comps[wall] = -2;
  64. floodFill(1);
  65. }
  66.  
  67. int b2(int i) {
  68. int bit = 0;
  69. for (int j = 3; j >= 0; j--) {
  70. int delta = (i / (int) Math.pow(2, j)) * (int) Math.pow(10, j);
  71. if (delta == 0) {
  72. continue;
  73. }
  74. bit += delta;
  75. i -= (int) Math.pow(2, j);
  76. }
  77. return bit;
  78. }
  79.  
  80. boolean checkEast(int a, int b) {
  81. if ((a % 1000) / 100 == 0 && b % 1 == 0) {
  82. return true;
  83. }
  84. return false;
  85. }
  86.  
  87. boolean checkSouth(int a, int b) {
  88. if (a / 1000 == 0 && (b % 100) / 10 == 0) {
  89. return true;
  90. }
  91. return false;
  92. }
  93.  
  94. @SuppressWarnings("unchecked")
  95. void run() throws IOException {
  96. BufferedReader f = new BufferedReader(new FileReader("castle.in"));
  97. PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(
  98. "castle.out")));
  99. StringTokenizer st = new StringTokenizer(f.readLine());
  100. int x = Integer.parseInt(st.nextToken());
  101. int y = Integer.parseInt(st.nextToken());
  102. xVal = x;
  103. map = new int[x][y];
  104. boolean fun = true;
  105. for (int j = 0; j < y; j++) {
  106. st = new StringTokenizer(f.readLine());
  107. for (int i = 0; i < x; i++) {
  108. map[i][j] = b2(Integer.parseInt(st.nextToken()));
  109. if (map[i][j] != 1111)
  110. {
  111. fun = false;
  112. }
  113. }
  114. }
  115. graph = new ArrayList[x * y];
  116. walls = new ArrayList[x * y];
  117. for (int i = 0; i < x * y; i++) {
  118. graph[i] = new ArrayList<Integer>();
  119. walls[i] = new ArrayList<Integer>();
  120. }
  121. comps = new int[x * y];
  122. for (int j = 0; j < y; j++) {
  123. for (int i = 0; i < x; i++) {
  124. if (i + 1 != x && checkEast(map[i][j], map[i + 1][j])) {
  125. graph[x * j + i].add(x * j + i + 1);
  126. graph[x * j + i + 1].add(x * j + i);
  127. } else if (i + 1 != x && !checkEast(map[i][j], map[i + 1][j])) {
  128. walls[x * j + i].add(x * j + i + 1);
  129. walls[x * j + i + 1].add(x * j + i);
  130. }
  131. if (j + 1 != y && checkSouth(map[i][j], map[i][j + 1])) {
  132. graph[x * j + i].add(x * j + x + i);
  133. graph[x * j + x + i].add(x * j + i);
  134. } else if (j + 1 != y && !checkSouth(map[i][j], map[i][j + 1])) {
  135. walls[x * j + i].add(x * j + x + i);
  136. walls[x * j + x + i].add(x * j + i);
  137. }
  138. }
  139. }
  140. findComponent();
  141. System.out.println(compCount);
  142. System.out.println(locMax);
  143. int maxMax = 0;
  144. int maxX = 0;
  145. int minY = 0;
  146. char letter = 'N';
  147. if (fun)
  148. {
  149. out.println(locMax + 1);
  150. if (y == maxX + 1)
  151. {
  152. letter = 'E';
  153. }
  154. out.println(y + " " + (maxX + 1) + " " + letter);
  155. out.close();
  156. System.exit(0);
  157. }
  158. for (int i = 0; i < x * y; i++) {
  159. for (int j = 0; j < walls[i].size(); j++) {
  160. graph[i].add(walls[i].get(j));
  161. graph[walls[i].get(j)].add(i);
  162. getARoom(i);
  163. if (maxMax == locMax) {
  164. if (minY == i % x + 1) {
  165. if (maxX == i / x + 1) {
  166. letter = 'N';
  167. } else if (maxX < i / x + 1) {
  168. maxX = i / x + 1;
  169. if (Math.abs(i - walls[i].get(j)) == x) {
  170. letter = 'N';
  171. }
  172. if (Math.abs(i - walls[i].get(j)) == 1) {
  173. letter = 'E';
  174. }
  175. }
  176. } else if (minY > i % x + 1) {
  177. maxX = i / x + 1;
  178. minY = i % x + 1;
  179. if (Math.abs(i - walls[i].get(j)) == x) {
  180. letter = 'N';
  181. }
  182. if (Math.abs(i - walls[i].get(j)) == 1) {
  183. letter = 'E';
  184. }
  185. }
  186. } else if (maxMax < locMax) {
  187. maxMax = locMax;
  188. maxX = i / x + 1;
  189. minY = i % x + 1;
  190. if (Math.abs(i - walls[i].get(j)) == x) {
  191. letter = 'N';
  192. }
  193. if (Math.abs(i - walls[i].get(j)) == 1) {
  194. letter = 'E';
  195. }
  196. }
  197. graph[i].remove(graph[i].size()-1);
  198. graph[walls[i].get(j)].remove(graph[walls[i].get(j)].size()-1);
  199. }
  200. }
  201. System.out.println(maxMax);
  202. System.out.println(maxX + " " + minY + " " + letter);
  203. out.close();
  204. System.exit(0);
  205. }
  206.  
  207. public static void main(String[] args) throws IOException {
  208. castle prog = new castle();
  209. prog.run();
  210. }
  211. }
Add Comment
Please, Sign In to add comment