Pentomino solving algorithm using Algorithm X for exact cover problems











up vote
3
down vote

favorite












i am working on this for several days now and finally decided to post my problem here. I will explain you in detail what i am doing and how.
Before continuing. I went through Wikipedia and another 20 sites which explain this problem but it didn't help me.



Dancing Links - Wikipedia



Knuth's algorithm x.



One of the more useful sites:(Kanoodle) but when it comes to my problem the explanation is very poor.



Problem: How many ways are there to cover a rectangle of 5xn with W-, I- and L-Pentominos. You are allowed to flip and rotate them. What are Pentominos you may ask? Pentominos consist of 5 squares wich together build a figure. For example a W-Pentomino.



   | A | B | C
---------------
a | 1 | 0 | 0 |
-------------
b | 1 | 1 | 0 | Imagine all the 1s together they build a big "W".
------------- If you look at my picture it will be clearer.
c | 0 | 1 | 1 |


Instead of implementing W-, L- and I-Pentominos in a 5xn field i cut my task and started to think about all possible ways to fill a "W" in a 5x3 field.
Like this.
enter image description here
My next step was to think about a Matrix which looked similar to the DLX-Algorithm of Knuth and i came up with this. The green line between yellow and orange means that you could put both together to have another valid solution. My next text describes this green line.
enter image description here



I noticed that if i take a row and check if any other row under or above this row hasn't a 1 in the same column i had a valid solution. But i didn't know how to implement it. After researching hours and hours i found another way to describe my problem. I took a subset (my W-Pentomino) and defined it like this.
---picture.



Yet again i had trouble implementing this.



So here is my question: How would you implement this problem in JAVA. Is my approach good? Could you work with my ideas? If yes, how would you go on, if no, would you tell me where are the fails in my thoughts?



Here is the code i wrote sofar.



package data;

public class PentominoWILDLX
{
static Node h; // header element

static int cnt; // count solutions

public PentominoWILDLX()
{
h = new Node(); // init header element
cnt = 0; // init counter
}


public static void search (int k) // finds & counts solutions
{
if(h.R == h) // if empty: count & done
{
cnt++; return; // choose next column c
}
Node c = h.R; // remove c from clumns
cover(c); // choose next colun c
for (Node r=c.D; r!=c; r=r.D) // forall rows with 1 in c
{
for(Node j=r.R; j!=r; j=j.R) // forall 1-elements in row
{
cover(j.C); // remove clumn
}
search(k+1); // recursion with k+1 << hier überpfüen ob ich ändern muss
for (Node j=r.L; j!=r; j=j.L) // forall 1-elemnts in row
{
uncover(j.C); // backtrack . unremove? << googlen
}
uncover(c); // unremove f c to coulmns
}
}



public static void cover (Node c) // remove clumn c
{
c.R.L = c.L; // remove header
c.L.R = c.R; // from row list
for (Node i=c.D; i!=c; i=i.D) // forall rows with 1
{
for (Node j=i.R; i!=j; j=j.R) // forall elem in row
{
j.D.U = j.U; // rmove row elemnt
j.U.D = j.D; // from column list
}
}
}

public static void uncover (Node c) // undo remove col c
{
for (Node i=c.U; i!=c; i=i.U) // forall rows with 1
{
for (Node j=i.L; i!=j; j=j.L) // for all elem in row
{
j.D.U = j; // unremove row ele,
j.U.D = j; // to lumn list
}
c.R.L = c; // unremove header
c.L.R = c; // to row list
}
} // end of class

class Node // represents 1 element or header
{
Node C; // reference to column-header << h?,
Node L; // left
Node R; // right
Node U; // reference up
Node D; // down reference down

Node()
{
C=L=R=U=D=this; // 2*double-linked circular list
}
} // end of class

public static void main(String args)
{

}

}









share|improve this question
























  • I am not sure if I understand completely. Are you trying to find all the possible ways a shape can fit in a rectangle or find how many can fit on the same grid? I think I may be able to devise an answer for both.
    – Forseth11
    May 9 '15 at 22:10










  • I try to find all the possible ways a shape can fit in a rectangle of 5xn but to reach this i first try to solve how many can fit in a fixed grid of 5x3. So i would like to hear both answers =) as i can learn more. I am looking forward hearing from you.
    – Matthis Kohli
    May 10 '15 at 0:23










  • I posted an answer before seeing this comment for finding how many fit. To find all the possibilities you can modify step 2 in the answer to loop through the possibilities and give you coordinates.
    – Forseth11
    May 10 '15 at 1:26










  • Thank you very much i start to go through this as soon as i can. Really appreciate it.
    – Matthis Kohli
    May 10 '15 at 11:38










  • War das zufällig an der HHN beim Heinz als Übung dran?
    – Cappuccino90
    Nov 21 '15 at 15:11















up vote
3
down vote

favorite












i am working on this for several days now and finally decided to post my problem here. I will explain you in detail what i am doing and how.
Before continuing. I went through Wikipedia and another 20 sites which explain this problem but it didn't help me.



Dancing Links - Wikipedia



Knuth's algorithm x.



One of the more useful sites:(Kanoodle) but when it comes to my problem the explanation is very poor.



Problem: How many ways are there to cover a rectangle of 5xn with W-, I- and L-Pentominos. You are allowed to flip and rotate them. What are Pentominos you may ask? Pentominos consist of 5 squares wich together build a figure. For example a W-Pentomino.



   | A | B | C
---------------
a | 1 | 0 | 0 |
-------------
b | 1 | 1 | 0 | Imagine all the 1s together they build a big "W".
------------- If you look at my picture it will be clearer.
c | 0 | 1 | 1 |


Instead of implementing W-, L- and I-Pentominos in a 5xn field i cut my task and started to think about all possible ways to fill a "W" in a 5x3 field.
Like this.
enter image description here
My next step was to think about a Matrix which looked similar to the DLX-Algorithm of Knuth and i came up with this. The green line between yellow and orange means that you could put both together to have another valid solution. My next text describes this green line.
enter image description here



I noticed that if i take a row and check if any other row under or above this row hasn't a 1 in the same column i had a valid solution. But i didn't know how to implement it. After researching hours and hours i found another way to describe my problem. I took a subset (my W-Pentomino) and defined it like this.
---picture.



Yet again i had trouble implementing this.



So here is my question: How would you implement this problem in JAVA. Is my approach good? Could you work with my ideas? If yes, how would you go on, if no, would you tell me where are the fails in my thoughts?



Here is the code i wrote sofar.



package data;

public class PentominoWILDLX
{
static Node h; // header element

static int cnt; // count solutions

public PentominoWILDLX()
{
h = new Node(); // init header element
cnt = 0; // init counter
}


public static void search (int k) // finds & counts solutions
{
if(h.R == h) // if empty: count & done
{
cnt++; return; // choose next column c
}
Node c = h.R; // remove c from clumns
cover(c); // choose next colun c
for (Node r=c.D; r!=c; r=r.D) // forall rows with 1 in c
{
for(Node j=r.R; j!=r; j=j.R) // forall 1-elements in row
{
cover(j.C); // remove clumn
}
search(k+1); // recursion with k+1 << hier überpfüen ob ich ändern muss
for (Node j=r.L; j!=r; j=j.L) // forall 1-elemnts in row
{
uncover(j.C); // backtrack . unremove? << googlen
}
uncover(c); // unremove f c to coulmns
}
}



public static void cover (Node c) // remove clumn c
{
c.R.L = c.L; // remove header
c.L.R = c.R; // from row list
for (Node i=c.D; i!=c; i=i.D) // forall rows with 1
{
for (Node j=i.R; i!=j; j=j.R) // forall elem in row
{
j.D.U = j.U; // rmove row elemnt
j.U.D = j.D; // from column list
}
}
}

public static void uncover (Node c) // undo remove col c
{
for (Node i=c.U; i!=c; i=i.U) // forall rows with 1
{
for (Node j=i.L; i!=j; j=j.L) // for all elem in row
{
j.D.U = j; // unremove row ele,
j.U.D = j; // to lumn list
}
c.R.L = c; // unremove header
c.L.R = c; // to row list
}
} // end of class

class Node // represents 1 element or header
{
Node C; // reference to column-header << h?,
Node L; // left
Node R; // right
Node U; // reference up
Node D; // down reference down

Node()
{
C=L=R=U=D=this; // 2*double-linked circular list
}
} // end of class

public static void main(String args)
{

}

}









share|improve this question
























  • I am not sure if I understand completely. Are you trying to find all the possible ways a shape can fit in a rectangle or find how many can fit on the same grid? I think I may be able to devise an answer for both.
    – Forseth11
    May 9 '15 at 22:10










  • I try to find all the possible ways a shape can fit in a rectangle of 5xn but to reach this i first try to solve how many can fit in a fixed grid of 5x3. So i would like to hear both answers =) as i can learn more. I am looking forward hearing from you.
    – Matthis Kohli
    May 10 '15 at 0:23










  • I posted an answer before seeing this comment for finding how many fit. To find all the possibilities you can modify step 2 in the answer to loop through the possibilities and give you coordinates.
    – Forseth11
    May 10 '15 at 1:26










  • Thank you very much i start to go through this as soon as i can. Really appreciate it.
    – Matthis Kohli
    May 10 '15 at 11:38










  • War das zufällig an der HHN beim Heinz als Übung dran?
    – Cappuccino90
    Nov 21 '15 at 15:11













up vote
3
down vote

favorite









up vote
3
down vote

favorite











i am working on this for several days now and finally decided to post my problem here. I will explain you in detail what i am doing and how.
Before continuing. I went through Wikipedia and another 20 sites which explain this problem but it didn't help me.



Dancing Links - Wikipedia



Knuth's algorithm x.



One of the more useful sites:(Kanoodle) but when it comes to my problem the explanation is very poor.



Problem: How many ways are there to cover a rectangle of 5xn with W-, I- and L-Pentominos. You are allowed to flip and rotate them. What are Pentominos you may ask? Pentominos consist of 5 squares wich together build a figure. For example a W-Pentomino.



   | A | B | C
---------------
a | 1 | 0 | 0 |
-------------
b | 1 | 1 | 0 | Imagine all the 1s together they build a big "W".
------------- If you look at my picture it will be clearer.
c | 0 | 1 | 1 |


Instead of implementing W-, L- and I-Pentominos in a 5xn field i cut my task and started to think about all possible ways to fill a "W" in a 5x3 field.
Like this.
enter image description here
My next step was to think about a Matrix which looked similar to the DLX-Algorithm of Knuth and i came up with this. The green line between yellow and orange means that you could put both together to have another valid solution. My next text describes this green line.
enter image description here



I noticed that if i take a row and check if any other row under or above this row hasn't a 1 in the same column i had a valid solution. But i didn't know how to implement it. After researching hours and hours i found another way to describe my problem. I took a subset (my W-Pentomino) and defined it like this.
---picture.



Yet again i had trouble implementing this.



So here is my question: How would you implement this problem in JAVA. Is my approach good? Could you work with my ideas? If yes, how would you go on, if no, would you tell me where are the fails in my thoughts?



Here is the code i wrote sofar.



package data;

public class PentominoWILDLX
{
static Node h; // header element

static int cnt; // count solutions

public PentominoWILDLX()
{
h = new Node(); // init header element
cnt = 0; // init counter
}


public static void search (int k) // finds & counts solutions
{
if(h.R == h) // if empty: count & done
{
cnt++; return; // choose next column c
}
Node c = h.R; // remove c from clumns
cover(c); // choose next colun c
for (Node r=c.D; r!=c; r=r.D) // forall rows with 1 in c
{
for(Node j=r.R; j!=r; j=j.R) // forall 1-elements in row
{
cover(j.C); // remove clumn
}
search(k+1); // recursion with k+1 << hier überpfüen ob ich ändern muss
for (Node j=r.L; j!=r; j=j.L) // forall 1-elemnts in row
{
uncover(j.C); // backtrack . unremove? << googlen
}
uncover(c); // unremove f c to coulmns
}
}



public static void cover (Node c) // remove clumn c
{
c.R.L = c.L; // remove header
c.L.R = c.R; // from row list
for (Node i=c.D; i!=c; i=i.D) // forall rows with 1
{
for (Node j=i.R; i!=j; j=j.R) // forall elem in row
{
j.D.U = j.U; // rmove row elemnt
j.U.D = j.D; // from column list
}
}
}

public static void uncover (Node c) // undo remove col c
{
for (Node i=c.U; i!=c; i=i.U) // forall rows with 1
{
for (Node j=i.L; i!=j; j=j.L) // for all elem in row
{
j.D.U = j; // unremove row ele,
j.U.D = j; // to lumn list
}
c.R.L = c; // unremove header
c.L.R = c; // to row list
}
} // end of class

class Node // represents 1 element or header
{
Node C; // reference to column-header << h?,
Node L; // left
Node R; // right
Node U; // reference up
Node D; // down reference down

Node()
{
C=L=R=U=D=this; // 2*double-linked circular list
}
} // end of class

public static void main(String args)
{

}

}









share|improve this question















i am working on this for several days now and finally decided to post my problem here. I will explain you in detail what i am doing and how.
Before continuing. I went through Wikipedia and another 20 sites which explain this problem but it didn't help me.



Dancing Links - Wikipedia



Knuth's algorithm x.



One of the more useful sites:(Kanoodle) but when it comes to my problem the explanation is very poor.



Problem: How many ways are there to cover a rectangle of 5xn with W-, I- and L-Pentominos. You are allowed to flip and rotate them. What are Pentominos you may ask? Pentominos consist of 5 squares wich together build a figure. For example a W-Pentomino.



   | A | B | C
---------------
a | 1 | 0 | 0 |
-------------
b | 1 | 1 | 0 | Imagine all the 1s together they build a big "W".
------------- If you look at my picture it will be clearer.
c | 0 | 1 | 1 |


Instead of implementing W-, L- and I-Pentominos in a 5xn field i cut my task and started to think about all possible ways to fill a "W" in a 5x3 field.
Like this.
enter image description here
My next step was to think about a Matrix which looked similar to the DLX-Algorithm of Knuth and i came up with this. The green line between yellow and orange means that you could put both together to have another valid solution. My next text describes this green line.
enter image description here



I noticed that if i take a row and check if any other row under or above this row hasn't a 1 in the same column i had a valid solution. But i didn't know how to implement it. After researching hours and hours i found another way to describe my problem. I took a subset (my W-Pentomino) and defined it like this.
---picture.



Yet again i had trouble implementing this.



So here is my question: How would you implement this problem in JAVA. Is my approach good? Could you work with my ideas? If yes, how would you go on, if no, would you tell me where are the fails in my thoughts?



Here is the code i wrote sofar.



package data;

public class PentominoWILDLX
{
static Node h; // header element

static int cnt; // count solutions

public PentominoWILDLX()
{
h = new Node(); // init header element
cnt = 0; // init counter
}


public static void search (int k) // finds & counts solutions
{
if(h.R == h) // if empty: count & done
{
cnt++; return; // choose next column c
}
Node c = h.R; // remove c from clumns
cover(c); // choose next colun c
for (Node r=c.D; r!=c; r=r.D) // forall rows with 1 in c
{
for(Node j=r.R; j!=r; j=j.R) // forall 1-elements in row
{
cover(j.C); // remove clumn
}
search(k+1); // recursion with k+1 << hier überpfüen ob ich ändern muss
for (Node j=r.L; j!=r; j=j.L) // forall 1-elemnts in row
{
uncover(j.C); // backtrack . unremove? << googlen
}
uncover(c); // unremove f c to coulmns
}
}



public static void cover (Node c) // remove clumn c
{
c.R.L = c.L; // remove header
c.L.R = c.R; // from row list
for (Node i=c.D; i!=c; i=i.D) // forall rows with 1
{
for (Node j=i.R; i!=j; j=j.R) // forall elem in row
{
j.D.U = j.U; // rmove row elemnt
j.U.D = j.D; // from column list
}
}
}

public static void uncover (Node c) // undo remove col c
{
for (Node i=c.U; i!=c; i=i.U) // forall rows with 1
{
for (Node j=i.L; i!=j; j=j.L) // for all elem in row
{
j.D.U = j; // unremove row ele,
j.U.D = j; // to lumn list
}
c.R.L = c; // unremove header
c.L.R = c; // to row list
}
} // end of class

class Node // represents 1 element or header
{
Node C; // reference to column-header << h?,
Node L; // left
Node R; // right
Node U; // reference up
Node D; // down reference down

Node()
{
C=L=R=U=D=this; // 2*double-linked circular list
}
} // end of class

public static void main(String args)
{

}

}






java algorithm






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '15 at 15:54









SeeuD1

1,41211942




1,41211942










asked May 9 '15 at 21:53









Matthis Kohli

536618




536618












  • I am not sure if I understand completely. Are you trying to find all the possible ways a shape can fit in a rectangle or find how many can fit on the same grid? I think I may be able to devise an answer for both.
    – Forseth11
    May 9 '15 at 22:10










  • I try to find all the possible ways a shape can fit in a rectangle of 5xn but to reach this i first try to solve how many can fit in a fixed grid of 5x3. So i would like to hear both answers =) as i can learn more. I am looking forward hearing from you.
    – Matthis Kohli
    May 10 '15 at 0:23










  • I posted an answer before seeing this comment for finding how many fit. To find all the possibilities you can modify step 2 in the answer to loop through the possibilities and give you coordinates.
    – Forseth11
    May 10 '15 at 1:26










  • Thank you very much i start to go through this as soon as i can. Really appreciate it.
    – Matthis Kohli
    May 10 '15 at 11:38










  • War das zufällig an der HHN beim Heinz als Übung dran?
    – Cappuccino90
    Nov 21 '15 at 15:11


















  • I am not sure if I understand completely. Are you trying to find all the possible ways a shape can fit in a rectangle or find how many can fit on the same grid? I think I may be able to devise an answer for both.
    – Forseth11
    May 9 '15 at 22:10










  • I try to find all the possible ways a shape can fit in a rectangle of 5xn but to reach this i first try to solve how many can fit in a fixed grid of 5x3. So i would like to hear both answers =) as i can learn more. I am looking forward hearing from you.
    – Matthis Kohli
    May 10 '15 at 0:23










  • I posted an answer before seeing this comment for finding how many fit. To find all the possibilities you can modify step 2 in the answer to loop through the possibilities and give you coordinates.
    – Forseth11
    May 10 '15 at 1:26










  • Thank you very much i start to go through this as soon as i can. Really appreciate it.
    – Matthis Kohli
    May 10 '15 at 11:38










  • War das zufällig an der HHN beim Heinz als Übung dran?
    – Cappuccino90
    Nov 21 '15 at 15:11
















I am not sure if I understand completely. Are you trying to find all the possible ways a shape can fit in a rectangle or find how many can fit on the same grid? I think I may be able to devise an answer for both.
– Forseth11
May 9 '15 at 22:10




I am not sure if I understand completely. Are you trying to find all the possible ways a shape can fit in a rectangle or find how many can fit on the same grid? I think I may be able to devise an answer for both.
– Forseth11
May 9 '15 at 22:10












I try to find all the possible ways a shape can fit in a rectangle of 5xn but to reach this i first try to solve how many can fit in a fixed grid of 5x3. So i would like to hear both answers =) as i can learn more. I am looking forward hearing from you.
– Matthis Kohli
May 10 '15 at 0:23




I try to find all the possible ways a shape can fit in a rectangle of 5xn but to reach this i first try to solve how many can fit in a fixed grid of 5x3. So i would like to hear both answers =) as i can learn more. I am looking forward hearing from you.
– Matthis Kohli
May 10 '15 at 0:23












I posted an answer before seeing this comment for finding how many fit. To find all the possibilities you can modify step 2 in the answer to loop through the possibilities and give you coordinates.
– Forseth11
May 10 '15 at 1:26




I posted an answer before seeing this comment for finding how many fit. To find all the possibilities you can modify step 2 in the answer to loop through the possibilities and give you coordinates.
– Forseth11
May 10 '15 at 1:26












Thank you very much i start to go through this as soon as i can. Really appreciate it.
– Matthis Kohli
May 10 '15 at 11:38




Thank you very much i start to go through this as soon as i can. Really appreciate it.
– Matthis Kohli
May 10 '15 at 11:38












War das zufällig an der HHN beim Heinz als Übung dran?
– Cappuccino90
Nov 21 '15 at 15:11




War das zufällig an der HHN beim Heinz als Übung dran?
– Cappuccino90
Nov 21 '15 at 15:11












1 Answer
1






active

oldest

votes

















up vote
2
down vote













To find all the possible ways a Pentomino can be put inside a rectangle you can do the following steps:




  1. Find the length and with of the pentomino as if you were to draw a rectangle to its size.

  2. Now find how many times that rectangle will fit in the bigger rectangle. For example, you have a 3x5 rectangle and the smaller rectangle is for the W shape and it is 3x3. 5-3+1 = 3. There are 3 possible ways to fit the W shape in a 3x5 square (Without flipping or rotating). If the rectangle was 5x5 and we are still using the same pentomino then there would be 9 possible different ways to put the shape without flipping or rotating. Use the formula: x = a - b + 1; y = c - d + 1; xy = z (Possible ways with no rotating). a = length of big rectangle; b = length of small rectangle; c = width of big rectangle; d = width of small rectangle.

  3. Since we are dealing with rectangles you will need to flip the shape and rotate it and each time you do so do the same calculation in step 3 and add it to the total.

  4. Step 3 brought up another problem. What if the shape flipped or rotated is the same as a previous shape. To calculate this take the shape in the format of x and y so the w shape would be: {(0,0), (1,0), (1,1), (2,1), (2,2)} if the origin was in the top left. Take these coordinates and rotate them. (0, 0) -> (0, 3) for the first point rotating 90 degrees counter clockwise. To rotate any points the first 90 degrees do: (y, w - x - 1) w = width of rectangle. To rotate the next 90 degrees do: (y, h - x - 1) h = height of rectangle. To rotate the final time repeat the (y, w - x - 1). Remember that every time you rotate the width and height must be swapped because it is a rectangle and not always a square. If any of these rotated figures have the same coordinates (They don't have to be in the same order), then that specific rotation will not be checked with step 2.

  5. Finally you must take care of reflecting / flipping. This is easier than rotating. To flip horizontally simply do (w - x - 1, y); w = width of shape.




Here are some pictures of work I used to figure this out and test it:



Finding possibilities:



Find possibilities



enter image description here



enter image description here



Rotating (Squares and rectangles):
enter image description here



enter image description here



Flipping:
enter image description here






share|improve this answer





















  • Still thinking ... i dont understand the Dancing-Links here. I filled a 5xn field with nodes now but noticed its not Dancing-Links. =(
    – Matthis Kohli
    May 10 '15 at 22:04










  • Sorry... I don't know what a 5xn field is or what Pentominos are. I am just using what you are asking and shown and logic to answer your question.
    – Forseth11
    May 10 '15 at 22:08










  • what is the length and what is the width in your formula. In a 3x5 rectangle and the "W" is a 3x3 rectangle. Which one is lenght. The column or the row?
    – Matthis Kohli
    May 10 '15 at 22:09










  • The width is how many units left and right and the height is how many units up and down. So width is the column. In a 3x5 rectangle the 3 is the width and the 5 is the height.
    – Forseth11
    May 10 '15 at 22:20











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f30145404%2fpentomino-solving-algorithm-using-algorithm-x-for-exact-cover-problems%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote













To find all the possible ways a Pentomino can be put inside a rectangle you can do the following steps:




  1. Find the length and with of the pentomino as if you were to draw a rectangle to its size.

  2. Now find how many times that rectangle will fit in the bigger rectangle. For example, you have a 3x5 rectangle and the smaller rectangle is for the W shape and it is 3x3. 5-3+1 = 3. There are 3 possible ways to fit the W shape in a 3x5 square (Without flipping or rotating). If the rectangle was 5x5 and we are still using the same pentomino then there would be 9 possible different ways to put the shape without flipping or rotating. Use the formula: x = a - b + 1; y = c - d + 1; xy = z (Possible ways with no rotating). a = length of big rectangle; b = length of small rectangle; c = width of big rectangle; d = width of small rectangle.

  3. Since we are dealing with rectangles you will need to flip the shape and rotate it and each time you do so do the same calculation in step 3 and add it to the total.

  4. Step 3 brought up another problem. What if the shape flipped or rotated is the same as a previous shape. To calculate this take the shape in the format of x and y so the w shape would be: {(0,0), (1,0), (1,1), (2,1), (2,2)} if the origin was in the top left. Take these coordinates and rotate them. (0, 0) -> (0, 3) for the first point rotating 90 degrees counter clockwise. To rotate any points the first 90 degrees do: (y, w - x - 1) w = width of rectangle. To rotate the next 90 degrees do: (y, h - x - 1) h = height of rectangle. To rotate the final time repeat the (y, w - x - 1). Remember that every time you rotate the width and height must be swapped because it is a rectangle and not always a square. If any of these rotated figures have the same coordinates (They don't have to be in the same order), then that specific rotation will not be checked with step 2.

  5. Finally you must take care of reflecting / flipping. This is easier than rotating. To flip horizontally simply do (w - x - 1, y); w = width of shape.




Here are some pictures of work I used to figure this out and test it:



Finding possibilities:



Find possibilities



enter image description here



enter image description here



Rotating (Squares and rectangles):
enter image description here



enter image description here



Flipping:
enter image description here






share|improve this answer





















  • Still thinking ... i dont understand the Dancing-Links here. I filled a 5xn field with nodes now but noticed its not Dancing-Links. =(
    – Matthis Kohli
    May 10 '15 at 22:04










  • Sorry... I don't know what a 5xn field is or what Pentominos are. I am just using what you are asking and shown and logic to answer your question.
    – Forseth11
    May 10 '15 at 22:08










  • what is the length and what is the width in your formula. In a 3x5 rectangle and the "W" is a 3x3 rectangle. Which one is lenght. The column or the row?
    – Matthis Kohli
    May 10 '15 at 22:09










  • The width is how many units left and right and the height is how many units up and down. So width is the column. In a 3x5 rectangle the 3 is the width and the 5 is the height.
    – Forseth11
    May 10 '15 at 22:20















up vote
2
down vote













To find all the possible ways a Pentomino can be put inside a rectangle you can do the following steps:




  1. Find the length and with of the pentomino as if you were to draw a rectangle to its size.

  2. Now find how many times that rectangle will fit in the bigger rectangle. For example, you have a 3x5 rectangle and the smaller rectangle is for the W shape and it is 3x3. 5-3+1 = 3. There are 3 possible ways to fit the W shape in a 3x5 square (Without flipping or rotating). If the rectangle was 5x5 and we are still using the same pentomino then there would be 9 possible different ways to put the shape without flipping or rotating. Use the formula: x = a - b + 1; y = c - d + 1; xy = z (Possible ways with no rotating). a = length of big rectangle; b = length of small rectangle; c = width of big rectangle; d = width of small rectangle.

  3. Since we are dealing with rectangles you will need to flip the shape and rotate it and each time you do so do the same calculation in step 3 and add it to the total.

  4. Step 3 brought up another problem. What if the shape flipped or rotated is the same as a previous shape. To calculate this take the shape in the format of x and y so the w shape would be: {(0,0), (1,0), (1,1), (2,1), (2,2)} if the origin was in the top left. Take these coordinates and rotate them. (0, 0) -> (0, 3) for the first point rotating 90 degrees counter clockwise. To rotate any points the first 90 degrees do: (y, w - x - 1) w = width of rectangle. To rotate the next 90 degrees do: (y, h - x - 1) h = height of rectangle. To rotate the final time repeat the (y, w - x - 1). Remember that every time you rotate the width and height must be swapped because it is a rectangle and not always a square. If any of these rotated figures have the same coordinates (They don't have to be in the same order), then that specific rotation will not be checked with step 2.

  5. Finally you must take care of reflecting / flipping. This is easier than rotating. To flip horizontally simply do (w - x - 1, y); w = width of shape.




Here are some pictures of work I used to figure this out and test it:



Finding possibilities:



Find possibilities



enter image description here



enter image description here



Rotating (Squares and rectangles):
enter image description here



enter image description here



Flipping:
enter image description here






share|improve this answer





















  • Still thinking ... i dont understand the Dancing-Links here. I filled a 5xn field with nodes now but noticed its not Dancing-Links. =(
    – Matthis Kohli
    May 10 '15 at 22:04










  • Sorry... I don't know what a 5xn field is or what Pentominos are. I am just using what you are asking and shown and logic to answer your question.
    – Forseth11
    May 10 '15 at 22:08










  • what is the length and what is the width in your formula. In a 3x5 rectangle and the "W" is a 3x3 rectangle. Which one is lenght. The column or the row?
    – Matthis Kohli
    May 10 '15 at 22:09










  • The width is how many units left and right and the height is how many units up and down. So width is the column. In a 3x5 rectangle the 3 is the width and the 5 is the height.
    – Forseth11
    May 10 '15 at 22:20













up vote
2
down vote










up vote
2
down vote









To find all the possible ways a Pentomino can be put inside a rectangle you can do the following steps:




  1. Find the length and with of the pentomino as if you were to draw a rectangle to its size.

  2. Now find how many times that rectangle will fit in the bigger rectangle. For example, you have a 3x5 rectangle and the smaller rectangle is for the W shape and it is 3x3. 5-3+1 = 3. There are 3 possible ways to fit the W shape in a 3x5 square (Without flipping or rotating). If the rectangle was 5x5 and we are still using the same pentomino then there would be 9 possible different ways to put the shape without flipping or rotating. Use the formula: x = a - b + 1; y = c - d + 1; xy = z (Possible ways with no rotating). a = length of big rectangle; b = length of small rectangle; c = width of big rectangle; d = width of small rectangle.

  3. Since we are dealing with rectangles you will need to flip the shape and rotate it and each time you do so do the same calculation in step 3 and add it to the total.

  4. Step 3 brought up another problem. What if the shape flipped or rotated is the same as a previous shape. To calculate this take the shape in the format of x and y so the w shape would be: {(0,0), (1,0), (1,1), (2,1), (2,2)} if the origin was in the top left. Take these coordinates and rotate them. (0, 0) -> (0, 3) for the first point rotating 90 degrees counter clockwise. To rotate any points the first 90 degrees do: (y, w - x - 1) w = width of rectangle. To rotate the next 90 degrees do: (y, h - x - 1) h = height of rectangle. To rotate the final time repeat the (y, w - x - 1). Remember that every time you rotate the width and height must be swapped because it is a rectangle and not always a square. If any of these rotated figures have the same coordinates (They don't have to be in the same order), then that specific rotation will not be checked with step 2.

  5. Finally you must take care of reflecting / flipping. This is easier than rotating. To flip horizontally simply do (w - x - 1, y); w = width of shape.




Here are some pictures of work I used to figure this out and test it:



Finding possibilities:



Find possibilities



enter image description here



enter image description here



Rotating (Squares and rectangles):
enter image description here



enter image description here



Flipping:
enter image description here






share|improve this answer












To find all the possible ways a Pentomino can be put inside a rectangle you can do the following steps:




  1. Find the length and with of the pentomino as if you were to draw a rectangle to its size.

  2. Now find how many times that rectangle will fit in the bigger rectangle. For example, you have a 3x5 rectangle and the smaller rectangle is for the W shape and it is 3x3. 5-3+1 = 3. There are 3 possible ways to fit the W shape in a 3x5 square (Without flipping or rotating). If the rectangle was 5x5 and we are still using the same pentomino then there would be 9 possible different ways to put the shape without flipping or rotating. Use the formula: x = a - b + 1; y = c - d + 1; xy = z (Possible ways with no rotating). a = length of big rectangle; b = length of small rectangle; c = width of big rectangle; d = width of small rectangle.

  3. Since we are dealing with rectangles you will need to flip the shape and rotate it and each time you do so do the same calculation in step 3 and add it to the total.

  4. Step 3 brought up another problem. What if the shape flipped or rotated is the same as a previous shape. To calculate this take the shape in the format of x and y so the w shape would be: {(0,0), (1,0), (1,1), (2,1), (2,2)} if the origin was in the top left. Take these coordinates and rotate them. (0, 0) -> (0, 3) for the first point rotating 90 degrees counter clockwise. To rotate any points the first 90 degrees do: (y, w - x - 1) w = width of rectangle. To rotate the next 90 degrees do: (y, h - x - 1) h = height of rectangle. To rotate the final time repeat the (y, w - x - 1). Remember that every time you rotate the width and height must be swapped because it is a rectangle and not always a square. If any of these rotated figures have the same coordinates (They don't have to be in the same order), then that specific rotation will not be checked with step 2.

  5. Finally you must take care of reflecting / flipping. This is easier than rotating. To flip horizontally simply do (w - x - 1, y); w = width of shape.




Here are some pictures of work I used to figure this out and test it:



Finding possibilities:



Find possibilities



enter image description here



enter image description here



Rotating (Squares and rectangles):
enter image description here



enter image description here



Flipping:
enter image description here







share|improve this answer












share|improve this answer



share|improve this answer










answered May 10 '15 at 1:23









Forseth11

1,2771617




1,2771617












  • Still thinking ... i dont understand the Dancing-Links here. I filled a 5xn field with nodes now but noticed its not Dancing-Links. =(
    – Matthis Kohli
    May 10 '15 at 22:04










  • Sorry... I don't know what a 5xn field is or what Pentominos are. I am just using what you are asking and shown and logic to answer your question.
    – Forseth11
    May 10 '15 at 22:08










  • what is the length and what is the width in your formula. In a 3x5 rectangle and the "W" is a 3x3 rectangle. Which one is lenght. The column or the row?
    – Matthis Kohli
    May 10 '15 at 22:09










  • The width is how many units left and right and the height is how many units up and down. So width is the column. In a 3x5 rectangle the 3 is the width and the 5 is the height.
    – Forseth11
    May 10 '15 at 22:20


















  • Still thinking ... i dont understand the Dancing-Links here. I filled a 5xn field with nodes now but noticed its not Dancing-Links. =(
    – Matthis Kohli
    May 10 '15 at 22:04










  • Sorry... I don't know what a 5xn field is or what Pentominos are. I am just using what you are asking and shown and logic to answer your question.
    – Forseth11
    May 10 '15 at 22:08










  • what is the length and what is the width in your formula. In a 3x5 rectangle and the "W" is a 3x3 rectangle. Which one is lenght. The column or the row?
    – Matthis Kohli
    May 10 '15 at 22:09










  • The width is how many units left and right and the height is how many units up and down. So width is the column. In a 3x5 rectangle the 3 is the width and the 5 is the height.
    – Forseth11
    May 10 '15 at 22:20
















Still thinking ... i dont understand the Dancing-Links here. I filled a 5xn field with nodes now but noticed its not Dancing-Links. =(
– Matthis Kohli
May 10 '15 at 22:04




Still thinking ... i dont understand the Dancing-Links here. I filled a 5xn field with nodes now but noticed its not Dancing-Links. =(
– Matthis Kohli
May 10 '15 at 22:04












Sorry... I don't know what a 5xn field is or what Pentominos are. I am just using what you are asking and shown and logic to answer your question.
– Forseth11
May 10 '15 at 22:08




Sorry... I don't know what a 5xn field is or what Pentominos are. I am just using what you are asking and shown and logic to answer your question.
– Forseth11
May 10 '15 at 22:08












what is the length and what is the width in your formula. In a 3x5 rectangle and the "W" is a 3x3 rectangle. Which one is lenght. The column or the row?
– Matthis Kohli
May 10 '15 at 22:09




what is the length and what is the width in your formula. In a 3x5 rectangle and the "W" is a 3x3 rectangle. Which one is lenght. The column or the row?
– Matthis Kohli
May 10 '15 at 22:09












The width is how many units left and right and the height is how many units up and down. So width is the column. In a 3x5 rectangle the 3 is the width and the 5 is the height.
– Forseth11
May 10 '15 at 22:20




The width is how many units left and right and the height is how many units up and down. So width is the column. In a 3x5 rectangle the 3 is the width and the 5 is the height.
– Forseth11
May 10 '15 at 22:20


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f30145404%2fpentomino-solving-algorithm-using-algorithm-x-for-exact-cover-problems%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to pass form data using jquery Ajax to insert data in database?

National Museum of Racing and Hall of Fame

Guess what letter conforming each word