Advertisement
yeeteeee

Untitled

May 20th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. package classwork;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. public class PuZzLe {
  6. public static void main(String[] args) {
  7. String p = "12, 22, 32, 23, 43, 31, 44, 33, 443, 11";
  8. ArrayList<String> pieces = sortPieces(p.replaceAll(" ", ""));
  9. System.out.println(pieces);
  10. System.out.println(run(4, 6, pieces));
  11. }
  12.  
  13. static ArrayList<String> sortPieces(String a) {
  14. ArrayList<String> ps = new ArrayList<String>();
  15. ps.add("00");
  16. String[] as = a.split(",");
  17. boolean ch;
  18. int c;
  19.  
  20. for (int i = 0; i < 10; i++) {
  21. ch = false;
  22. c = 0;
  23. while (ch == false)
  24. if (are(as[i]) < are(ps.get(c))) {
  25. ps.add(c, as[i]);
  26. ch = true;
  27. } else if (are(as[i]) == are(ps.get(c))) {
  28. if ((Character.getNumericValue(as[i].charAt(0))) < (Character
  29. .getNumericValue(ps.get(c).charAt(0)))) {
  30. ps.add(c, as[i]);
  31. ch = true;
  32. }
  33. } else if (c + 1 == ps.size()) {
  34. ps.add(c, as[i]);
  35. ch = true;
  36. } else
  37. c += 1;
  38.  
  39. }
  40. ps.remove(ps.size() - 1);
  41. return ps;
  42. }
  43.  
  44. static int are(String s) {
  45. int ma = 0;
  46.  
  47. if (s.length() == 2)
  48. ma = (Character.getNumericValue(s.charAt(0))) * (Character.getNumericValue(s.charAt(1)));
  49. else if (s.length() == 3)
  50. ma = ((Character.getNumericValue(s.charAt(0)))
  51. - ((Character.getNumericValue(s.charAt(0))) - (Character.getNumericValue(s.charAt(2)))))
  52. * (Character.getNumericValue(s.charAt(1))
  53. + ((Character.getNumericValue(s.charAt(1))) - (Character.getNumericValue(s.charAt(2)))));
  54.  
  55. return ma;
  56. }
  57.  
  58. static int w(String s) {
  59. int width = 0;
  60. width = Character.getNumericValue(s.charAt(1));
  61. return width;
  62. }
  63.  
  64. static int wa(String s, int cc) {
  65. int width = 0;
  66. if (s.length() == 3) {
  67. if (cc != Character.getNumericValue(s.charAt(1)))
  68. width = Character.getNumericValue(s.charAt(2));
  69. else
  70. width = Character.getNumericValue(s.charAt(2));
  71.  
  72. } else if (s.length() == 2) {
  73. width = w(s);
  74. }
  75. return width;
  76. }
  77.  
  78. static int h(String s) {
  79. int height = 0;
  80. height = Character.getNumericValue(s.charAt(0));
  81. return height;
  82. }
  83.  
  84. static void pg(int[][] grid, int gh, int gw) {
  85. for (int i = 0; i < gh; i++) {
  86. for (int j = 0; j < gw; j++) {
  87. System.out.print(grid[i][j]);
  88. }
  89. System.out.println("");
  90. }
  91. }
  92.  
  93. static String run(int gw, int gh, ArrayList<String> pcs) {
  94. String ms = "";
  95. int x = 0, y = 0;
  96. boolean found;
  97. boolean next = true;
  98. int[][] grid = new int[gh][gw];
  99.  
  100. for (int i = 0; i < pcs.size(); i++) {
  101. found = false;
  102.  
  103. if (next)
  104. for (int j = 0; j < gh; j++) {
  105. for (int k = 0; k < gw; k++) {
  106. if (!found && grid[gh - j][k] == 0) {
  107. x = j;
  108. y = k;
  109. found = true;
  110. next = false;
  111. }
  112. }
  113. }
  114.  
  115. if (chck(gh, gw, x, y, pcs.get(pcs.size() - i))) {
  116. for (int j = 0; j < h(pcs.get(pcs.size() - j)); j++) {
  117. for (int k = 0; k < wa(pcs.get(pcs.size() - k), h(pcs.get(pcs.size() - j))); k++) {
  118. grid[j+y][k+x]=1;
  119. }
  120. }
  121. next = true;
  122. }
  123. }
  124. return ms;
  125. }
  126.  
  127. static boolean chck(int gh, int gw, int x, int y, String tbc) {
  128. if (w(tbc) + x > gw)
  129. return false;
  130. else if (h(tbc) + y > gh)
  131. return false;
  132. else
  133. return true;
  134. }
  135.  
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement