Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. package com.uned;
  2.  
  3.  
  4. import java.util.Arrays;
  5. import java.util.Random;
  6.  
  7. /**
  8. * Se dispone de n cubos indentificados del 1 a n. Cada cubo tiene en una de sus caras una letra distinta. Se india
  9. * una palabra de n letras.
  10. * Colocar los n cubos uno a continuacion del otro de forma que con esa disposicion se forme la palabra dada.
  11. * En diferentes cubos puede haber letras repetidas, la solucion puede ser no unica o no existir.
  12. */
  13. public class Cube {
  14.  
  15. public static void main(String[] args) {
  16. String wordToSearch = args[0];
  17. int n = wordToSearch.length();
  18.  
  19. char[][] matrixCube = new char[n][n];
  20. fillCubesWithRandomLetters(matrixCube);
  21.  
  22. int solution[][] = new int[n][2];
  23. for (int[] aSolution : solution) {
  24. Arrays.fill(aSolution, -1);
  25. }
  26.  
  27. System.out.println("Word to search is: " + wordToSearch);
  28.  
  29. for (char[] aMatrixCube : matrixCube) {
  30. for (char anAMatrixCube : aMatrixCube) {
  31. System.out.print(anAMatrixCube);
  32. }
  33. System.out.println();
  34. }
  35.  
  36. search(wordToSearch, matrixCube, solution, 0);
  37. }
  38.  
  39. private static boolean search(String wordToSearch, char[][] matrixCube, int[][] solution, int searchLetterPosition) {
  40. if (solution[solution.length - 1][0] != -1) {
  41. printSolution(solution);
  42. return true;
  43. }
  44. char letterToFound = wordToSearch.charAt(searchLetterPosition);
  45. for (int cube = 0; cube < matrixCube.length; cube++) {
  46. if (cubeIsFree(solution, cube) && !isSolved(solution)) {
  47. for (int faceInCube = 0; faceInCube < matrixCube[cube].length; faceInCube++) {
  48. boolean letterFounded = matrixCube[cube][faceInCube] == letterToFound;
  49. boolean nextLetterFounded;
  50. if (letterFounded) {
  51. solution[searchLetterPosition][0] = cube;
  52. solution[searchLetterPosition][1] = faceInCube;
  53. nextLetterFounded = search(wordToSearch, matrixCube, solution, searchLetterPosition + 1);
  54. if (!nextLetterFounded && cube == matrixCube.length - 1 && !isSolved(solution)) {
  55. solution[Math.max(searchLetterPosition - 1, 0)][0] = -1;
  56. solution[Math.max(searchLetterPosition - 1, 0)][1] = -1;
  57. }
  58. }
  59. }
  60. }
  61. }
  62. return solution[searchLetterPosition][0] != -1;
  63. }
  64.  
  65. private static boolean isSolved(int[][] solution) {
  66. for (int[] aSolution : solution) {
  67. if (aSolution[0] == -1) {
  68. return false;
  69. }
  70. }
  71. return true;
  72. }
  73.  
  74. private static void printSolution(int[][] solution) {
  75. System.out.println("Solution is:");
  76. for (int[] cubeAndFace : solution) {
  77. System.out.printf("%s %d%n", cubeAndFace[0], cubeAndFace[1]);
  78. }
  79. }
  80.  
  81. private static boolean cubeIsFree(int[][] solution, int cube) {
  82. for (int[] aSolution : solution) {
  83. if (aSolution[0] == cube) {
  84. return false;
  85. }
  86. }
  87. return true;
  88. }
  89.  
  90. private static void fillCubesWithRandomLetters(char[][] matrix) {
  91. for (int i = 0; i < matrix.length; i++) {
  92. for (int j = 0; j < matrix[i].length; j++) {
  93. matrix[i][j] = (char) (new Random().nextInt(8) + 97);
  94. }
  95. }
  96. }
  97.  
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement