Advertisement
Guest User

dominosolver

a guest
Jul 24th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 18.29 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Iterator;
  3. import java.util.List;
  4. import java.util.stream.Collectors;
  5.  
  6. public class Test
  7. {
  8.     public static void main(String[] args)
  9.     {
  10.         Chips chips = new Chips(chips());
  11.         Board board = new Board(cells());
  12.  
  13.         process(board, chips, new History());
  14.     }
  15.  
  16.     private static void process(Board board, Chips chips, History history)
  17.     {
  18.         if (!chips.isEmpty())
  19.         {
  20.             List<Hole> holes = board.holes();
  21.  
  22.             if (!holes.isEmpty())
  23.             {
  24.                 for (Hole hole : holes)
  25.                 {
  26.                     for (Chip chip : chips)
  27.                     {
  28.                         FitOrientation fitOrientation = board.fits(chip, hole);
  29.  
  30.                         if (fitOrientation != null)
  31.                         {
  32.                             Board newBoard = board.set(chip, hole, fitOrientation);
  33.                             Chips newChips = chips.remove(chip);
  34.  
  35.                             process(newBoard, newChips, history.add(chip));
  36.                         }
  37.                     }
  38.                 }
  39.             }
  40.             else
  41.             {
  42.                 System.out.print("ERROR");
  43.             }
  44.         }
  45.         else
  46.         {
  47.             board.print();
  48.             history.print();
  49.             System.exit(0);
  50.         }
  51.     }
  52.  
  53.     private static Cells cells()
  54.     {
  55.         Cell cell00 = new Cell(0, 0);
  56.         Cell cell01 = new Cell(0, 1);
  57.         Cell cell02 = new Cell(0, 2);
  58.         Cell cell03 = new Cell(0, 3);
  59.         Cell cell04 = new Cell(0, 4);
  60.         Cell cell05 = new Cell(0, 5);
  61.         Cell cell06 = new Cell(0, 6);
  62.         Cell cell10 = new Cell(1, 0);
  63.         Cell cell16 = new Cell(1, 6);
  64.         Cell cell17 = new Cell(1, 7);
  65.         Cell cell20 = new Cell(2, 0);
  66.         Cell cell21 = new Cell(2, 1);
  67.         Cell cell22 = new Cell(2, 2);
  68.         Cell cell23 = new Cell(2, 3);
  69.         Cell cell24 = new Cell(2, 4);
  70.         Cell cell25 = new Cell(2, 5);
  71.         Cell cell26 = new Cell(2, 6);
  72.         Cell cell30 = new Cell(3, 0);
  73.         Cell cell34 = new Cell(3, 4);
  74.         Cell cell36 = new Cell(3, 6);
  75.         Cell cell40 = new Cell(4, 0);
  76.         Cell cell44 = new Cell(4, 4);
  77.         Cell cell46 = new Cell(4, 6);
  78.         Cell cell50 = new Cell(5, 0);
  79.         Cell cell51 = new Cell(5, 1);
  80.         Cell cell52 = new Cell(5, 2);
  81.         Cell cell53 = new Cell(5, 3);
  82.         Cell cell54 = new Cell(5, 4);
  83.         Cell cell55 = new Cell(5, 5);
  84.         Cell cell56 = new Cell(5, 6);
  85.         Cell fakeCell = new Cell(1, 8, 6);
  86.  
  87.         /*cell00.down(cell10);
  88.         cell00.down(cell01);
  89.         cell01.left(cell00);
  90.         cell01.right(cell02);
  91.         cell02.left(cell01);
  92.         cell02.right(cell03);
  93.         cell03.left(cell02);
  94.         cell03.right(cell04);
  95.         cell04.left(cell03);
  96.         cell04.right(cell05);
  97.         cell05.left(cell04);
  98.         cell05.right(cell06);
  99.         cell06.left(cell05);
  100.         cell06.down(cell16);
  101.  
  102.         cell10.up(cell00);
  103.         cell10.down(cell20);
  104.         cell16.up(cell06);
  105.         cell16.down(cell26);
  106.         cell16.right(cell17);
  107.         cell17.left(cell16);
  108.         cell17.right(fakeCell);
  109.         fakeCell.left(cell17);
  110.  
  111.         cell20.up(cell10);
  112.         cell20.down(cell30);
  113.         cell20.right(cell21);
  114.         cell21.left(cell20);
  115.         cell21.right(cell22);
  116.         cell22.left(cell21);
  117.         cell22.right(cell23);
  118.         cell23.left(cell22);
  119.         cell23.right(cell24);
  120.         cell24.left(cell23);
  121.         cell24.right(cell25);
  122.         cell25.left(cell24);
  123.         cell25.right(cell26);
  124.         cell26.left(cell25);
  125.         cell26.up(cell16);
  126.         cell26.down(cell36);
  127.  
  128.         cell30.up(cell20);
  129.         cell30.down(cell40);
  130.         cell34.up(cell24);
  131.         cell34.down(cell44);
  132.         cell36.up(cell26);
  133.         cell36.down(cell46);
  134.  
  135.         cell40.up(cell30);
  136.         cell40.down(cell50);
  137.         cell44.up(cell34);
  138.         cell44.down(cell54);
  139.         cell46.up(cell36);
  140.         cell46.down(cell56);
  141.  
  142.         cell50.up(cell40);
  143.         cell50.right(cell51);
  144.         cell51.left(cell50);
  145.         cell51.right(cell52);
  146.         cell52.left(cell51);
  147.         cell52.right(cell53);
  148.         cell53.left(cell52);
  149.         cell53.right(cell54);
  150.         cell54.left(cell53);
  151.         cell54.right(cell55);
  152.         cell54.up(cell44);
  153.         cell55.left(cell54);
  154.         cell55.right(cell56);
  155.         cell56.left(cell55);
  156.         cell56.up(cell46);*/
  157.  
  158.         List<Cell> cells = new ArrayList<>();
  159.         cells.add(cell00);
  160.         cells.add(cell01);
  161.         cells.add(cell02);
  162.         cells.add(cell03);
  163.         cells.add(cell04);
  164.         cells.add(cell05);
  165.         cells.add(cell06);
  166.         cells.add(cell10);
  167.         cells.add(cell16);
  168.         cells.add(cell17);
  169.         cells.add(cell20);
  170.         cells.add(cell21);
  171.         cells.add(cell22);
  172.         cells.add(cell23);
  173.         cells.add(cell24);
  174.         cells.add(cell25);
  175.         cells.add(cell26);
  176.         cells.add(cell30);
  177.         cells.add(cell34);
  178.         cells.add(cell36);
  179.         cells.add(cell40);
  180.         cells.add(cell44);
  181.         cells.add(cell46);
  182.         cells.add(cell50);
  183.         cells.add(cell51);
  184.         cells.add(cell52);
  185.         cells.add(cell53);
  186.         cells.add(cell54);
  187.         cells.add(cell55);
  188.         cells.add(cell56);
  189.         cells.add(fakeCell);
  190.  
  191.         return new Cells(cells);
  192.     }
  193.  
  194.     private static List<Chip> chips()
  195.     {
  196.         List<Chip> chips = new ArrayList<>();
  197.  
  198.         chips.add(new Chip(2, 6, ChipType.HORIZONTAL));
  199.         chips.add(new Chip(4, 2, ChipType.HORIZONTAL));
  200.         chips.add(new Chip(4, 1, ChipType.HORIZONTAL));
  201.         chips.add(new Chip(3, 2, ChipType.HORIZONTAL));
  202.         chips.add(new Chip(6, 4, ChipType.HORIZONTAL));
  203.         chips.add(new Chip(1, 5, ChipType.HORIZONTAL));
  204.         chips.add(new Chip(1, 6, ChipType.HORIZONTAL));
  205.         chips.add(new Chip(4, 4, ChipType.HORIZONTAL));
  206.         chips.add(new Chip(3, 5, ChipType.HORIZONTAL));
  207.         chips.add(new Chip(5, 3, ChipType.HORIZONTAL));
  208.  
  209.         chips.add(new Chip(3, 1, ChipType.VERTICAL));
  210.         chips.add(new Chip(1, 4, ChipType.VERTICAL));
  211.         chips.add(new Chip(4, 1, ChipType.VERTICAL));
  212.         chips.add(new Chip(2, 4, ChipType.VERTICAL));
  213.         chips.add(new Chip(4, 5, ChipType.VERTICAL));
  214.  
  215.         return chips;
  216.     }
  217.  
  218.     private enum ChipType
  219.     {
  220.         HORIZONTAL,
  221.         VERTICAL
  222.     }
  223.  
  224.     private enum FitOrientation
  225.     {
  226.         UP,
  227.         DOWN,
  228.         LEFT,
  229.         RIGHT
  230.     }
  231.  
  232.     private static class Hole
  233.     {
  234.         private final Cell cell;
  235.         private final int value;
  236.  
  237.         private Hole(Cell cell, int value)
  238.         {
  239.             this.cell = cell;
  240.             this.value = value;
  241.         }
  242.  
  243.         @Override
  244.         public String toString()
  245.         {
  246.             return String.format("%s - Value=%d", cell.toString(), value);
  247.         }
  248.     }
  249.  
  250.     private static class Board
  251.     {
  252.         private final Cells cells;
  253.  
  254.         private Board(Cells cells)
  255.         {
  256.             this.cells = cells;
  257.         }
  258.  
  259.         public List<Hole> holes()
  260.         {
  261.             List<Hole> holes = new ArrayList<>();
  262.  
  263.             for (Cell cell : cells)
  264.             {
  265.                 holes.addAll(cell.holes(cells));
  266.             }
  267.  
  268.             return holes;
  269.         }
  270.  
  271.         public FitOrientation fits(Chip chip, Hole hole)
  272.         {
  273.             if (chip.type == ChipType.VERTICAL)
  274.             {
  275.                 Cell up = hole.cell.up(cells);
  276.                 Cell down = hole.cell.down(cells);
  277.  
  278.                 if (chip.valueB == hole.value)
  279.                 {
  280.                     if (fits(up, chip.valueA))
  281.                     {
  282.                         return FitOrientation.UP;
  283.                     }
  284.                     else
  285.                     {
  286.                         System.out.print("");
  287.                     }
  288.                 }
  289.                 else if (chip.valueA == hole.value)
  290.                 {
  291.                     if (fits(down, chip.valueB))
  292.                     {
  293.                         return FitOrientation.DOWN;
  294.                     }
  295.                     else
  296.                     {
  297.                         System.out.print("");
  298.                     }
  299.                 }
  300.             }
  301.             else if (chip.type == ChipType.HORIZONTAL)
  302.             {
  303.                 Cell left = hole.cell.left(cells);
  304.                 Cell right = hole.cell.right(cells);
  305.  
  306.                 if (chip.valueB == hole.value)
  307.                 {
  308.                     if (fits(left, chip.valueA))
  309.                     {
  310.                         return FitOrientation.LEFT;
  311.                     }
  312.                     else
  313.                     {
  314.                         System.out.print("");
  315.                     }
  316.                 }
  317.                 else if (chip.valueA == hole.value)
  318.                 {
  319.                     if (fits(right, chip.valueB))
  320.                     {
  321.                         return FitOrientation.RIGHT;
  322.                     }
  323.                     else
  324.                     {
  325.                         System.out.print("");
  326.                     }
  327.                 }
  328.             }
  329.  
  330.             return null;
  331.         }
  332.  
  333.         private boolean fits(Cell cell, int value)
  334.         {
  335.             if ((cell != null) && (cell.value == 0))
  336.             {
  337.                 Cell up = cell.up(cells);
  338.                 Cell down = cell.down(cells);
  339.                 Cell left = cell.left(cells);
  340.                 Cell right = cell.right(cells);
  341.  
  342.                 boolean upValid = (up == null) || (up.value == value) || (up.isEmpty());
  343.                 boolean downValid = (down == null) || (down.value == value) || (down.isEmpty());
  344.                 boolean leftValid = (left == null) || (left.value == value) || (left.isEmpty());
  345.                 boolean rightValid = (right == null) || (right.value == value) || (right.isEmpty());
  346.  
  347.                 return upValid && downValid && leftValid && rightValid;
  348.             }
  349.             else
  350.             {
  351.                 return false;
  352.             }
  353.         }
  354.  
  355.         public Board set(Chip chip, Hole hole, FitOrientation fitOrientation)
  356.         {
  357.             if (chip.type == ChipType.VERTICAL)
  358.             {
  359.                 if (fitOrientation == FitOrientation.UP)
  360.                 {
  361.                     Cell up = hole.cell.up(cells);
  362.                     Board newBoard = set(up.i, up.j, chip.valueA);
  363.  
  364.                     return newBoard.set(hole.cell.i, hole.cell.j, chip.valueB);
  365.                 }
  366.                 else if (fitOrientation == FitOrientation.DOWN)
  367.                 {
  368.                     Cell down = hole.cell.down(cells);
  369.                     Board newBoard = set(down.i, down.j, chip.valueB);
  370.  
  371.                     return newBoard.set(hole.cell.i, hole.cell.j, chip.valueA);
  372.                 }
  373.             }
  374.             else if (chip.type == ChipType.HORIZONTAL)
  375.             {
  376.                 if (fitOrientation == FitOrientation.LEFT)
  377.                 {
  378.                     Cell left = hole.cell.left(cells);
  379.                     Board newBoard = set(left.i, left.j, chip.valueA);
  380.  
  381.                     return newBoard.set(hole.cell.i, hole.cell.j, chip.valueB);
  382.                 }
  383.                 else if (fitOrientation == FitOrientation.RIGHT)
  384.                 {
  385.                     Cell right = hole.cell.right(cells);
  386.                     Board newBoard = set(right.i, right.j, chip.valueB);
  387.  
  388.                     return newBoard.set(hole.cell.i, hole.cell.j, chip.valueA);
  389.                 }
  390.             }
  391.  
  392.             throw new RuntimeException();
  393.         }
  394.  
  395.         public Board set(int i, int j, int value)
  396.         {
  397.             return new Board(cells.set(i, j, value));
  398.         }
  399.  
  400.         //        public Cell get(int i, int j)
  401.         //        {
  402.         //            return cells.cell(i, j);
  403.         //        }
  404.  
  405.         public void print()
  406.         {
  407.             StringBuilder builder = new StringBuilder();
  408.  
  409.             for (int i = 0; i < 6; i++)
  410.             {
  411.                 for (int j = 0; j < 8; j++)
  412.                 {
  413.                     Cell cell = cells.cell(i, j);
  414.  
  415.                     if (cell != null)
  416.                     {
  417.                         builder.append(cell.value).append(" ");
  418.                     }
  419.                     else
  420.                     {
  421.                         builder.append("x").append(" ");
  422.                     }
  423.                 }
  424.  
  425.                 builder.append("\n");
  426.             }
  427.  
  428.             builder.append("\n");
  429.  
  430.             System.out.print(builder.toString());
  431.         }
  432.     }
  433.  
  434.     private static class Cells implements Iterable<Cell>
  435.     {
  436.         private final List<Cell> cells;
  437.  
  438.         private Cells(List<Cell> cells)
  439.         {
  440.             this.cells = cells;
  441.         }
  442.  
  443.         public Cells set(int i, int j, int value)
  444.         {
  445.             List<Cell> newCells = new ArrayList<>();
  446.  
  447.             for (Cell cell : cells)
  448.             {
  449.                 if ((cell.i == i) && (cell.j == j))
  450.                 {
  451.                     newCells.add(cell.value(value));
  452.                 }
  453.                 else
  454.                 {
  455.                     newCells.add(cell);
  456.                 }
  457.             }
  458.  
  459.             return new Cells(newCells);
  460.         }
  461.  
  462.         public Cell cell(int i, int j)
  463.         {
  464.             for (Cell cell : cells)
  465.             {
  466.                 if ((cell.i == i) && (cell.j == j))
  467.                 {
  468.                     return cell;
  469.                 }
  470.             }
  471.  
  472.             return null;
  473.         }
  474.  
  475.         @Override
  476.         public Iterator<Cell> iterator()
  477.         {
  478.             return cells.iterator();
  479.         }
  480.     }
  481.  
  482.     private static class Cell
  483.     {
  484.         private final int i;
  485.         private final int j;
  486.         private final int value;
  487.  
  488.         private Cell(int i, int j, int value)
  489.         {
  490.             this.i = i;
  491.             this.j = j;
  492.             this.value = value;
  493.         }
  494.  
  495.         private Cell(int i, int j)
  496.         {
  497.             this(i, j, 0);
  498.         }
  499.  
  500.         public boolean isEmpty()
  501.         {
  502.             return (value == 0);
  503.         }
  504.  
  505.         public Cell value(int value)
  506.         {
  507.             return new Cell(i, j, value);
  508.         }
  509.  
  510.         public List<Hole> holes(Cells cells)
  511.         {
  512.             List<Hole> holes = new ArrayList<>();
  513.  
  514.             if (!isEmpty())
  515.             {
  516.                 Cell up = up(cells);
  517.                 if ((up != null) && (up.isEmpty()))
  518.                 {
  519.                     holes.add(new Hole(up, value));
  520.                 }
  521.  
  522.                 Cell down = down(cells);
  523.                 if ((down != null) && (down.isEmpty()))
  524.                 {
  525.                     holes.add(new Hole(down, value));
  526.                 }
  527.  
  528.                 Cell left = left(cells);
  529.                 if ((left != null) && (left.isEmpty()))
  530.                 {
  531.                     holes.add(new Hole(left, value));
  532.                 }
  533.  
  534.                 Cell right = right(cells);
  535.                 if ((right != null) && (right.isEmpty()))
  536.                 {
  537.                     holes.add(new Hole(right, value));
  538.                 }
  539.             }
  540.  
  541.             return holes;
  542.         }
  543.  
  544.         public Cell up(Cells cells)
  545.         {
  546.             return cells.cell(i - 1, j);
  547.         }
  548.  
  549.         public Cell down(Cells cells)
  550.         {
  551.             return cells.cell(i + 1, j);
  552.         }
  553.  
  554.         public Cell left(Cells cells)
  555.         {
  556.             return cells.cell(i, j - 1);
  557.         }
  558.  
  559.         public Cell right(Cells cells)
  560.         {
  561.             return cells.cell(i, j + 1);
  562.         }
  563.  
  564.         @Override
  565.         public String toString()
  566.         {
  567.             return String.format("Cell(%d,%d,%d)", i, j, value);
  568.         }
  569.     }
  570.  
  571.     private static class Chips implements Iterable<Chip>
  572.     {
  573.         private final List<Chip> chips;
  574.  
  575.         private Chips(List<Chip> chips)
  576.         {
  577.             this.chips = chips;
  578.         }
  579.  
  580.         public boolean isEmpty()
  581.         {
  582.             return chips.isEmpty();
  583.         }
  584.  
  585.         public Chips remove(Chip chip)
  586.         {
  587.             List<Chip> newChips = chips.stream().filter(c -> !c.equals(chip)).collect(Collectors.toList());
  588.  
  589.             return new Chips(newChips);
  590.         }
  591.  
  592.         @Override
  593.         public Iterator<Chip> iterator()
  594.         {
  595.             return chips.iterator();
  596.         }
  597.     }
  598.  
  599.     private static class Chip
  600.     {
  601.         // from left to right and up to bottom
  602.  
  603.         private final int valueA;
  604.         private final int valueB;
  605.         private final ChipType type;
  606.  
  607.         private Chip(int valueA, int valueB, ChipType type)
  608.         {
  609.             this.valueA = valueA;
  610.             this.valueB = valueB;
  611.             this.type = type;
  612.         }
  613.  
  614.         @Override
  615.         public boolean equals(Object o)
  616.         {
  617.             if (this == o)
  618.             {
  619.                 return true;
  620.             }
  621.             else if (o == null || getClass() != o.getClass())
  622.             {
  623.                 return false;
  624.             }
  625.  
  626.             Chip chip = (Chip) o;
  627.  
  628.             return (valueA == chip.valueA) && (valueB == chip.valueB) && (type == chip.type);
  629.  
  630.         }
  631.  
  632.         @Override
  633.         public int hashCode()
  634.         {
  635.             int result = valueA;
  636.             result = 31 * result + valueB;
  637.             result = 31 * result + type.hashCode();
  638.             return result;
  639.         }
  640.  
  641.         @Override
  642.         public String toString()
  643.         {
  644.             return String.format("%s(%d,%d)", type.toString(), valueA, valueB);
  645.         }
  646.     }
  647.  
  648.     private static class History
  649.     {
  650.         private final List<Chip> chips;
  651.  
  652.         private History(List<Chip> chips)
  653.         {
  654.             this.chips = chips;
  655.         }
  656.  
  657.         private History()
  658.         {
  659.             this.chips = new ArrayList<>();
  660.         }
  661.  
  662.         public History add(Chip newChip)
  663.         {
  664.             List<Chip> newChips = new ArrayList<>();
  665.             newChips.addAll(chips);
  666.             newChips.add(newChip);
  667.  
  668.             return new History(newChips);
  669.         }
  670.  
  671.         public void print()
  672.         {
  673.             for (Chip chip : chips)
  674.             {
  675.                 System.out.println(chip.toString());
  676.             }
  677.         }
  678.     }
  679. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement