Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
70
Task/N-queens-problem/Processing/n-queens-problem.processing
Normal file
70
Task/N-queens-problem/Processing/n-queens-problem.processing
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
int n = 8;
|
||||
int[] b = new int[n];
|
||||
int s = 0;
|
||||
int y = 0;
|
||||
|
||||
void setup() {
|
||||
size(400, 400);
|
||||
textAlign(CENTER, CENTER);
|
||||
textFont(createFont("DejaVu Sans", 44));
|
||||
b[0] = -1;
|
||||
}
|
||||
|
||||
void draw() {
|
||||
if (y >= 0) {
|
||||
do {
|
||||
b[y]++;
|
||||
} while ((b[y] < n) && unsafe(y));
|
||||
if (b[y] < n) {
|
||||
if (y < (n-1)) {
|
||||
b[++y] = -1;
|
||||
} else {
|
||||
drawBoard();
|
||||
}
|
||||
} else {
|
||||
y--;
|
||||
}
|
||||
} else {
|
||||
textSize(18);
|
||||
text("Press any key to restart", width / 2, height - 20);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
boolean unsafe(int y) {
|
||||
int x = b[y];
|
||||
for (int i = 1; i <= y; i++) {
|
||||
int t = b[y - i];
|
||||
if (t == x ||
|
||||
t == x - i ||
|
||||
t == x + i) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void drawBoard() {
|
||||
float w = width / n;
|
||||
for (int y = 0; y < n; y++) {
|
||||
for (int x = 0; x < n; x++) {
|
||||
fill(255 * ((x + y) % 2));
|
||||
square(x * w, y * w, w);
|
||||
if (b[y] == x) {
|
||||
fill(255 - 255 * ((x + y) % 2));
|
||||
textSize(42);
|
||||
text("♕", w / 2 + x *w, w /2 + y * w);
|
||||
}
|
||||
}
|
||||
}
|
||||
fill(255, 0, 0);
|
||||
textSize(18);
|
||||
text("Solution " + (++s), width / 2, height / 90);
|
||||
}
|
||||
|
||||
void keyPressed() {
|
||||
b = new int[n];
|
||||
s = 0;
|
||||
y = 0;
|
||||
b[0] = -1;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue