Skip to content

Instantly share code, notes, and snippets.

@jiridanek
Created October 12, 2011 14:35
Show Gist options
  • Save jiridanek/1281376 to your computer and use it in GitHub Desktop.
Save jiridanek/1281376 to your computer and use it in GitHub Desktop.
Polynomina
[jirka@private src]$ git diff -w 9f08bdbc2c795f4d4cfdcb4b158de51b45b71c0f..73f156ece3769e9cc2157f898893e66b494222cf
diff --git a/Polynomium.java b/Polynomium.java
index e976786..38fb1a4 100644
--- a/Polynomium.java
+++ b/Polynomium.java
@@ -1,8 +1,9 @@
+import java.util.Arrays;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
-public class Polynomium {
+public class Polynominum {
public int a;
public int b;
/**
@@ -11,33 +12,36 @@ public class Polynomium {
* ************
* b
*/
+
+ /** [rádek][sloupec] */
public boolean data[][];
- public Polynomium(int na, int nb) {
+
+ public Polynominum(int na, int nb) {
a = na;
b = nb;
data = new boolean[a][b];
}
- public Polynomium setPixel(int x, int y, boolean value) {
- Polynomium newp;
+ public Polynominum setPixel(int x, int y, boolean value) {
+ Polynominum newp;
if (x < 0) {
- newp = new Polynomium(a+1, b);
+ newp = new Polynominum(a+1, b);
newp.insert(this, 1, 0);
newp.data[0][y] = value;
} else if (y < 0) {
- newp = new Polynomium(a, b+1);
+ newp = new Polynominum(a, b+1);
newp.insert(this, 0, 1);
newp.data[x][0] = value;
} else if (x >= a) {
- newp = new Polynomium(a+1, b);
+ newp = new Polynominum(a+1, b);
newp.insert(this, 0, 0);
newp.data[x][y] = value;
} else if (y >= b) {
- newp = new Polynomium(a, b+1);
+ newp = new Polynominum(a, b+1);
newp.insert(this, 0, 0);
newp.data[x][y] = value;
} else {
- newp = new Polynomium(a,b);
+ newp = new Polynominum(a,b);
newp.insert(this, 0, 0);
newp.data[x][y] = value;
}
@@ -51,32 +55,65 @@ public class Polynomium {
return data[i][j];
}
- public void flipX() {
+ public Polynominum flipX() {
+ Polynominum newP = new Polynominum(this.a, this.b);
+ for (int y=0; y < this.a; y++) {
+ for (int x=0; x < this.b; x++) {
+ newP.data[y][x] = (this.data[y][(this.b-1) - x]);
+ }
+ }
+ return newP;
+ }
+ public Polynominum flipY() {
+ Polynominum newP = new Polynominum(this.a, this.b);
+ for (int y=0; y < this.a; y++) {
+ for (int x=0; x < this.b; x++) {
+ newP.data[y][x] = this.data[(this.a-1)-y][x];
+ }
+ }
+ return newP;
}
- public void flipY() {
+ public Polynominum rotateCCW() {
+ Polynominum newP = new Polynominum(this.b, this.a);
+ for (int y=0; y < this.a; y++) {
+ for (int x=0; x < this.b; x++) {
+ newP.data[(this.b-1) - x][y] = this.data[y][x];
+ }
+ }
+ return newP;
}
public boolean equals(Object p2) {
- Polynomium p = (Polynomium) p2;
- if (a != p.a || b != p.b) {
+ /** asi je musím kopírovat */
+ Polynominum p = (Polynominum) p2;
+ if ((a != p.a || b != p.b) && (b != p.a || a != p.b)) {
return false;
}
- if (this.data == p2.flipX().data || this.data == p2.flipY().data || this.data == p2.flipX().flipY().data) {
+ // zapomneli jsme je porovnat _bez_ jakekoli flipovani
+ if (Arrays.deepEquals(this.data, p.data)
+ || Arrays.deepEquals(this.data, p.flipX().data)
+ || Arrays.deepEquals(this.data, p.flipY().data)
+ || Arrays.deepEquals(this.data, p.flipX().flipY().data)
+ || Arrays.deepEquals(this.data, p.rotateCCW().data)
+ || Arrays.deepEquals(this.data, p.rotateCCW().flipX().data)
+ || Arrays.deepEquals(this.data, p.rotateCCW().flipY().data)
+ || Arrays.deepEquals(this.data, p.rotateCCW().flipY().flipX().data)) {
return true;
}
return false;
}
- public boolean nonFlipEquals(Object p2)
- Polynomium p = (Polynomium) p2;
+// public boolean nonFlipEquals(Object p2) {
+// Polynominum p = (Polynominum) p2;
+// }
- public void insert(Polynomium p2, int x, int y) {
+ public void insert(Polynominum p2, int x, int y) {
for (int i = 0; i < p2.a; i++) {
for (int j = 0; j < p2.b; j++) {
this.data[i+x][j+y] = p2.data[i][j];
@@ -102,28 +139,28 @@ public class Polynomium {
- public static void main(String args[]) {
- Set<Polynomium> polyPole= new HashSet<Polynomium>();
+ public static void main(String[] args) {
+ Set<Polynominum> polyPole= new HashSet<Polynominum>();
Scanner s = new Scanner(System.in);
int n = s.nextInt();
- Polynomium a = new Polynomium(1,1);
+ Polynominum a = new Polynominum(1,1);
a = a.setPixel(0,0,true);
polyPole.add(a);
for (int level = 2; level <=n; level++) {
- Set<Polynomium> tempPolyPole= new HashSet<Polynomium>();
- for(Polynomium p : polyPole) {
+ Set<Polynominum> tempPolyPole= new HashSet<Polynominum>();
+ for(Polynominum p : polyPole) {
for (int i=0; i < p.a; i++) {
for (int j=0; j < p.b; j++) {
if(p.data[i][j] == true) {
- if(p.getPixel(i-1,j) == false && p.a+1 < p.b) {
+ if(p.getPixel(i-1,j) == false /*&& p.a+1 <= p.b*/) { // <=, < se rozbije na tetrominech
tempPolyPole.add(p.setPixel(i-1, j, true));
}
if(p.getPixel(i, j-1) == false) {
tempPolyPole.add(p.setPixel(i, j-1, true));
- }
- if(p.getPixel(i+1,j) == false && p.a+1 < p.b) {
- tempPolyPole.add(p.setPixel(i+1, j, true));
+ } // <=, < se rozbije na pentominech
+ if(p.getPixel(i+1,j) == false /*&& p.a+1 <= p.b*/) { // bohuzel to rozbiji
+ tempPolyPole.add(p.setPixel(i+1, j, true)); // ten uzasny napad kdy nepotrebujeme rotaci
}
if(p.getPixel(i, j+1) == false) {
tempPolyPole.add(p.setPixel(i, j+1, true));
@@ -137,8 +174,23 @@ public class Polynomium {
}
- for(Polynomium p : polyPole) {
+ for(Polynominum p : polyPole) {
System.out.println(p.toString());
}
+
+ System.out.println("nalezeno: " + polyPole.size());
+ }
+
+ /**
+ * nemůžu, protože my potřebujeme volat equals
+ * @return haš
+ */
+ @Override
+ public int hashCode() {
+ int result = a*b;
+ //int result = a;
+ //result = 31 * result + b;
+ //result = 31 * result + (data != null ? Arrays.deepHashCode(data) : 0);
+ return result;
}
}
import java.util.Arrays;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class Polynominum {
public int a;
public int b;
/**
* ************
* a * *
* ************
* b
*/
/** [rádek][sloupec] */
public boolean data[][];
public Polynominum(int na, int nb) {
a = na;
b = nb;
data = new boolean[a][b];
}
public Polynominum setPixel(int x, int y, boolean value) {
Polynominum newp;
if (x < 0) {
newp = new Polynominum(a+1, b);
newp.insert(this, 1, 0);
newp.data[0][y] = value;
} else if (y < 0) {
newp = new Polynominum(a, b+1);
newp.insert(this, 0, 1);
newp.data[x][0] = value;
} else if (x >= a) {
newp = new Polynominum(a+1, b);
newp.insert(this, 0, 0);
newp.data[x][y] = value;
} else if (y >= b) {
newp = new Polynominum(a, b+1);
newp.insert(this, 0, 0);
newp.data[x][y] = value;
} else {
newp = new Polynominum(a,b);
newp.insert(this, 0, 0);
newp.data[x][y] = value;
}
return newp;
}
public boolean getPixel(int i, int j) {
if (i < 0 || j < 0 || i >= a | j >= b) {
return false;
}
return data[i][j];
}
public Polynominum flipX() {
Polynominum newP = new Polynominum(this.a, this.b);
for (int y=0; y < this.a; y++) {
for (int x=0; x < this.b; x++) {
newP.data[y][x] = (this.data[y][(this.b-1) - x]);
}
}
return newP;
}
public Polynominum flipY() {
Polynominum newP = new Polynominum(this.a, this.b);
for (int y=0; y < this.a; y++) {
for (int x=0; x < this.b; x++) {
newP.data[y][x] = this.data[(this.a-1)-y][x];
}
}
return newP;
}
public Polynominum rotateCCW() {
Polynominum newP = new Polynominum(this.b, this.a);
for (int y=0; y < this.a; y++) {
for (int x=0; x < this.b; x++) {
newP.data[(this.b-1) - x][y] = this.data[y][x];
}
}
return newP;
}
public boolean equals(Object p2) {
/** asi je musím kopírovat */
Polynominum p = (Polynominum) p2;
if ((a != p.a || b != p.b) && (b != p.a || a != p.b)) {
return false;
}
// zapomneli jsme je porovnat _bez_ jakekoli flipovani
if (Arrays.deepEquals(this.data, p.data)
|| Arrays.deepEquals(this.data, p.flipX().data)
|| Arrays.deepEquals(this.data, p.flipY().data)
|| Arrays.deepEquals(this.data, p.flipX().flipY().data)
|| Arrays.deepEquals(this.data, p.rotateCCW().data)
|| Arrays.deepEquals(this.data, p.rotateCCW().flipX().data)
|| Arrays.deepEquals(this.data, p.rotateCCW().flipY().data)
|| Arrays.deepEquals(this.data, p.rotateCCW().flipY().flipX().data)) {
return true;
}
return false;
}
// public boolean nonFlipEquals(Object p2) {
// Polynominum p = (Polynominum) p2;
// }
public void insert(Polynominum p2, int x, int y) {
for (int i = 0; i < p2.a; i++) {
for (int j = 0; j < p2.b; j++) {
this.data[i+x][j+y] = p2.data[i][j];
}
}
}
public String toString(){
String out = a + ":" + b + "\n\n";
for (int y = 0; y < a; y ++) {
for (int x = 0; x < b; x++) {
if (data[y][x] == false) {
out +=".";
}
else {
out +="+";
}
}
out += "\n";
}
return out;
}
public static void main(String[] args) {
Set<Polynominum> polyPole= new HashSet<Polynominum>();
Scanner s = new Scanner(System.in);
int n = s.nextInt();
Polynominum a = new Polynominum(1,1);
a = a.setPixel(0,0,true);
polyPole.add(a);
for (int level = 2; level <=n; level++) {
Set<Polynominum> tempPolyPole= new HashSet<Polynominum>();
for(Polynominum p : polyPole) {
for (int i=0; i < p.a; i++) {
for (int j=0; j < p.b; j++) {
if(p.data[i][j] == true) {
if(p.getPixel(i-1,j) == false /*&& p.a+1 <= p.b*/) { // <=, < se rozbije na tetrominech
tempPolyPole.add(p.setPixel(i-1, j, true));
}
if(p.getPixel(i, j-1) == false) {
tempPolyPole.add(p.setPixel(i, j-1, true));
} // <=, < se rozbije na pentominech
if(p.getPixel(i+1,j) == false /*&& p.a+1 <= p.b*/) { // bohuzel to rozbiji
tempPolyPole.add(p.setPixel(i+1, j, true)); // ten uzasny napad kdy nepotrebujeme rotaci
} // NAKONEC teda zadne tyhle meze, napad jde do kytek uplne
if(p.getPixel(i, j+1) == false) { //protoze se to rozbilo na 7 - davalo to o dve mene
tempPolyPole.add(p.setPixel(i, j+1, true));
}
}
}
}
}
polyPole = tempPolyPole;
}
for(Polynominum p : polyPole) {
System.out.println(p.toString());
}
System.out.println("nalezeno: " + polyPole.size());
}
/**
* nemůžu, protože my potřebujeme volat equals
* @return haš
*/
@Override
public int hashCode() {
int result = (31+a)*b;
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment