University stuff.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. import java.io.File;
  2. import java.util.Scanner;
  3. import java.util.ArrayList;
  4. class Brett {
  5. class Rute {
  6. int verdi;
  7. Rad rad;
  8. Kolonne kolonne;
  9. Boks boks;
  10. Brett brett;
  11. Rute(int verdi, Brett brett) {
  12. this.verdi = verdi;
  13. this.brett = brett;
  14. }
  15. public boolean harVerdi() {
  16. return verdi != -1;
  17. }
  18. public int[] finnAlleMuligeTall() {
  19. ArrayList<Integer> ugyldige = new ArrayList<>();
  20. for (Rute r: rad.ruter) {
  21. if (r.harVerdi()) {
  22. ugyldige.add(r.verdi);
  23. }
  24. }
  25. for (Rute r: kolonne.ruter) {
  26. if (r.harVerdi()) {
  27. ugyldige.add(r.verdi);
  28. }
  29. }
  30. for (Rute r: boks.ruter) {
  31. if (r.harVerdi()) {
  32. ugyldige.add(r.verdi);
  33. }
  34. }
  35. ArrayList<Integer> gyldige = new ArrayList<>();
  36. for (int i = 1; i <= brett.storrelse; ++i) {
  37. if (!ugyldige.contains(i)) {
  38. gyldige.add(i);
  39. }
  40. }
  41. int[] res = new int[gyldige.size()];
  42. for (int i = 0; i < res.length; ++i) {
  43. res[i] = gyldige.get(i);
  44. }
  45. return (int[])res;
  46. }
  47. }
  48. class Kolonne {
  49. private int index = 0;
  50. Rute[] ruter;
  51. Kolonne(int storrelse) {
  52. ruter = new Rute[storrelse];
  53. }
  54. public void settInn(Rute r) {
  55. ruter[index++] = r;
  56. }
  57. }
  58. class Rad {
  59. private int index = 0;
  60. Rute[] ruter;
  61. Rad(int storrelse) {
  62. ruter = new Rute[storrelse];
  63. }
  64. public void settInn(Rute r) {
  65. ruter[index++] = r;
  66. }
  67. }
  68. class Boks {
  69. private int index = 0;
  70. Rute[] ruter;
  71. int bredde;
  72. int hoyde;
  73. Boks(int bredde, int hoyde) {
  74. this.bredde = bredde;
  75. this.hoyde = hoyde;
  76. ruter = new Rute[bredde * hoyde];
  77. }
  78. public void settInn(Rute r) {
  79. ruter[index++] = r;
  80. }
  81. }
  82. File fil;
  83. int raderPerBoks;
  84. int kolonnerPerBoks;
  85. int antallBokserX;
  86. int antallBokserY;
  87. int storrelse;
  88. Rute[] ruter;
  89. Rad[] rader;
  90. Kolonne[] kolonner;
  91. Boks[] bokser;
  92. Brett(File fil) {
  93. this.fil = fil;
  94. try {
  95. read();
  96. } catch (Exception ex) {
  97. System.out.println(ex.toString());
  98. System.exit(1);
  99. }
  100. }
  101. public void print() throws Exception {
  102. int x = 0;
  103. int y = 1;
  104. int modX = storrelse / antallBokserX;
  105. int modY = storrelse / antallBokserY;
  106. for (Rute r: ruter) {
  107. if (x % modX == 0 && x != 0) {
  108. System.out.print("|");
  109. }
  110. if (r.harVerdi()) {
  111. System.out.print(toChar(r.verdi));
  112. } else {
  113. System.out.print(" ");
  114. }
  115. x += 1;
  116. if (x >= storrelse) {
  117. x = 0;
  118. if (y % modY == 0 && y < storrelse) {
  119. System.out.println("");
  120. for (int i = 0; i < storrelse; ++i) {
  121. if (i % modX == 0 && i != 0) {
  122. System.out.print("+");
  123. }
  124. System.out.print("-");
  125. }
  126. }
  127. System.out.println("");
  128. y += 1;
  129. }
  130. }
  131. }
  132. public int toInt(char c) throws Exception {
  133. if (c == '.') {
  134. return -1;
  135. } else if (c >= '1' && c <= '9') {
  136. return (int)c - 49 + 1;
  137. } else if (c >= 'A' && c <= 'Z') {
  138. return (int)c - 65 + 10;
  139. } else if (c == '@') {
  140. return 36;
  141. } else if (c == '#') {
  142. return 37;
  143. } else if (c == '&') {
  144. return 38;
  145. } else if (c >= 'a' && c <= 'z') {
  146. return c - 97 + 39;
  147. } else {
  148. throw new IllegalArgumentException();
  149. }
  150. }
  151. public char toChar(int c) throws Exception {
  152. if (c == -1) {
  153. return '.';
  154. } else if (c >= 1 && c <= 9) {
  155. return (char)(c + 49 -1);
  156. } else if (c >= 10 && c <= 35) {
  157. return (char)(c + 65 - 10);
  158. } else if (c == 36) {
  159. return '@';
  160. } else if (c == 37) {
  161. return '#';
  162. } else if (c == 38) {
  163. return '&';
  164. } else if (c >= 39 && c <= 64) {
  165. return (char)(c + 97 - 39);
  166. } else {
  167. throw new IllegalArgumentException();
  168. }
  169. }
  170. private void read() throws Exception {
  171. Scanner s = new Scanner(fil);
  172. raderPerBoks = Integer.parseInt(s.nextLine());
  173. kolonnerPerBoks = Integer.parseInt(s.nextLine());
  174. storrelse = raderPerBoks * kolonnerPerBoks;
  175. antallBokserX = storrelse / kolonnerPerBoks;
  176. antallBokserY = storrelse / raderPerBoks;
  177. if (storrelse > 64) {
  178. throw new Exception("For stort brett!");
  179. }
  180. ruter = new Rute[storrelse * storrelse];
  181. // Les inn alle ruter
  182. int i = 0;
  183. for (int y = 0; y < storrelse; ++y) {
  184. if (!s.hasNextLine()) {
  185. throw new Exception("For faa rader!");
  186. }
  187. char[] rad = s.nextLine().toCharArray();
  188. if (rad.length > storrelse) {
  189. throw new Exception("For mange kolonner i rad "+(y+1)+"!");
  190. }
  191. for (int x = 0; x < storrelse; ++x) {
  192. char c;
  193. try {
  194. c = rad[x];
  195. } catch (ArrayIndexOutOfBoundsException ex) {
  196. throw new Exception("For faa kolonner i rad "+(y+1)+"!");
  197. }
  198. ruter[i++] = new Rute(toInt(c), this);
  199. }
  200. }
  201. if (s.hasNextLine()) {
  202. throw new Exception("For mange rader!");
  203. }
  204. // Opprett rader, kolonner, og bokser
  205. // (obligens `void opprettDatastruktur()`, jeg mener det ikke gir mening
  206. // aa ha det som en egen metode, da det vil skje hvis og bare hvis vi
  207. // leser inn ny informasjon)
  208. rader = new Rad[storrelse];
  209. kolonner = new Kolonne[storrelse];
  210. bokser = new Boks[storrelse];
  211. for (i = 0; i < storrelse; ++i) {
  212. rader[i] = new Rad(storrelse);
  213. kolonner[i] = new Kolonne(storrelse);
  214. bokser[i] = new Boks(kolonnerPerBoks, raderPerBoks);
  215. }
  216. int x = 0;
  217. int y = 0;
  218. for (Rute r: ruter) {
  219. r.rad = rader[y];
  220. rader[y].settInn(r);
  221. r.kolonne = kolonner[x];
  222. kolonner[x].settInn(r);
  223. int boxx = (x / kolonnerPerBoks) % antallBokserX;
  224. int boxy = (y / raderPerBoks) % antallBokserY;
  225. int boxi = boxx + (boxy * antallBokserX);
  226. bokser[boxi].settInn(r);
  227. r.boks = bokser[boxi];
  228. x += 1;
  229. if (x >= storrelse) {
  230. x = 0;
  231. y += 1;
  232. }
  233. }
  234. }
  235. }