Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package classwork;
- import java.util.ArrayList;
- public class PuZzLe {
- public static void main(String[] args) {
- String p = "12, 22, 32, 23, 43, 31, 44, 33, 443, 11";
- ArrayList<String> pieces = sortPieces(p.replaceAll(" ", ""));
- System.out.println(pieces);
- System.out.println(run(4, 6, pieces));
- }
- static ArrayList<String> sortPieces(String a) {
- ArrayList<String> ps = new ArrayList<String>();
- ps.add("00");
- String[] as = a.split(",");
- boolean ch;
- int c;
- for (int i = 0; i < 10; i++) {
- ch = false;
- c = 0;
- while (ch == false)
- if (are(as[i]) < are(ps.get(c))) {
- ps.add(c, as[i]);
- ch = true;
- } else if (are(as[i]) == are(ps.get(c))) {
- if ((Character.getNumericValue(as[i].charAt(0))) < (Character
- .getNumericValue(ps.get(c).charAt(0)))) {
- ps.add(c, as[i]);
- ch = true;
- }
- } else if (c + 1 == ps.size()) {
- ps.add(c, as[i]);
- ch = true;
- } else
- c += 1;
- }
- ps.remove(ps.size() - 1);
- return ps;
- }
- static int are(String s) {
- int ma = 0;
- if (s.length() == 2)
- ma = (Character.getNumericValue(s.charAt(0))) * (Character.getNumericValue(s.charAt(1)));
- else if (s.length() == 3)
- ma = ((Character.getNumericValue(s.charAt(0)))
- - ((Character.getNumericValue(s.charAt(0))) - (Character.getNumericValue(s.charAt(2)))))
- * (Character.getNumericValue(s.charAt(1))
- + ((Character.getNumericValue(s.charAt(1))) - (Character.getNumericValue(s.charAt(2)))));
- return ma;
- }
- static int w(String s) {
- int width = 0;
- width = Character.getNumericValue(s.charAt(1));
- return width;
- }
- static int wa(String s, int cc) {
- int width = 0;
- if (s.length() == 3) {
- if (cc != Character.getNumericValue(s.charAt(1)))
- width = Character.getNumericValue(s.charAt(2));
- else
- width = Character.getNumericValue(s.charAt(2));
- } else if (s.length() == 2) {
- width = w(s);
- }
- return width;
- }
- static int h(String s) {
- int height = 0;
- height = Character.getNumericValue(s.charAt(0));
- return height;
- }
- static void pg(int[][] grid, int gh, int gw) {
- for (int i = 0; i < gh; i++) {
- for (int j = 0; j < gw; j++) {
- System.out.print(grid[i][j]);
- }
- System.out.println("");
- }
- }
- static String run(int gw, int gh, ArrayList<String> pcs) {
- String ms = "";
- int x = 0, y = 0;
- boolean found;
- boolean next = true;
- int[][] grid = new int[gh][gw];
- for (int i = 0; i < pcs.size(); i++) {
- found = false;
- if (next)
- for (int j = 0; j < gh; j++) {
- for (int k = 0; k < gw; k++) {
- if (!found && grid[gh - j][k] == 0) {
- x = j;
- y = k;
- found = true;
- next = false;
- }
- }
- }
- if (chck(gh, gw, x, y, pcs.get(pcs.size() - i))) {
- for (int j = 0; j < h(pcs.get(pcs.size() - j)); j++) {
- for (int k = 0; k < wa(pcs.get(pcs.size() - k), h(pcs.get(pcs.size() - j))); k++) {
- grid[j+y][k+x]=1;
- }
- }
- next = true;
- }
- }
- return ms;
- }
- static boolean chck(int gh, int gw, int x, int y, String tbc) {
- if (w(tbc) + x > gw)
- return false;
- else if (h(tbc) + y > gh)
- return false;
- else
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement