Commit fb17c000 authored by bbguimaraes's avatar bbguimaraes
Browse files

nqueens

parent 7df9317f
......@@ -3,4 +3,5 @@ for details/demos/screenshots.
- asm: simple assembly program that slides a three-digit number across the
terminal and an external seven-segment display.
- nqueens: n-queens solver and graphical display.
- tablut: ANSI C / ncurses implementation of the game Tablut.
all: nqueens/NQueens.class
.PHONY: clean
clean:
rm -f nqueens/*.class
%.class: %.java
javac $<
nqueens/NQueens.class: nqueens/NQueens.java
nqueens/NQueens.java: \
nqueens/Solver.java nqueens/Solver.class \
nqueens/Window.java nqueens/Window.class
nqueens/Solver.java: nqueens/Window.java nqueens/Window.class
package nqueens;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
public class NQueens {
public static void main(String[] args) {
String n_str = JOptionPane.showInputDialog("n:");
if(n_str == null)
return;
int n = Integer.parseInt(n_str);
Solver s = new Solver(n);
List<char[][]> l = new ArrayList<char[][]>();
for(int i = 0; i < n; i++)
s.nqueen(0, i, l);
Window w = new Window();
w.set_solutions(l);
w.revalidate();
w.repaint();
w.pack();
}
}
package nqueens;
import java.util.List;
public class Solver {
int l, c;
char[][] mat;
public Solver(int n) {
this.l = 0;
this.c = 0;
this.mat = this.create_matrix(n);
}
public int nqueen(int l, int c, List<char[][]> r) {
int n = this.mat.length;
if(this.possible(this.mat, l, c)) {
this.mat[l][c] = 'q';
if(l == n - 1) {
r.add(this.dup_mat(this.mat));
this.mat[l][c] = 0;
return 1;
} else {
int np = 0;
for(int i = 0; i < n; i++) {
np += nqueen(l + 1, i, r);
}
this.mat[l][c] = 0;
return np;
}
}
return 0;
}
public static char[][] create_matrix(int n) {
char[][] m = new char[n][n];
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
m[i][j] = 0;
return m;
}
private static char[][] dup_mat(char[][] m) {
int n = m.length;
char[][] mat_dup = new char[n][n];
for(int i = 0; i < n; ++i)
for(int j = 0; j < n; ++j)
mat_dup[i][j] = m[i][j];
return mat_dup;
}
private static boolean possible(char[][] m, int l, int c) {
if(l == 0)
return true;
int i, j;
for(i = 0; i < l; i++)
if(m[i][c] != 0)
return false;
i = l - 1;
j = c - 1;
while(i >= 0 && j >= 0) {
if(m[i][j] != 0)
return false;
i--;
j--;
}
i = l - 1;
j = c + 1;
while(i >= 0 && j < m.length) {
if(m[i][j] != 0)
return false;
i--;
j++;
}
return true;
}
}
package nqueens;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Graphics;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
public class Window extends JFrame {
private static final long serialVersionUID = 1L;
private final int TAM = 50;
public Window() {
super();
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(new Dimension(800, 600));
setVisible(true);
}
public void set_solutions(List<char[][]> r) {
final int n = (int)Math.floor(Math.sqrt(r.size()));
JPanel p = new JPanel(new GridLayout(n, n));
for(char[][] m : r) {
JPanel new_panel = new JPanel();
new_panel.add(new Display(m));
p.add(new_panel);
}
setContentPane(new JScrollPane(p));
}
private class Display extends JPanel {
private static final long serialVersionUID = 1L;
char[][] mat;
public Display(char[][] mat) {
super();
this.mat = mat;
this.setBackground(Color.WHITE);
Dimension d = new Dimension(
mat.length * TAM + 1, mat.length * TAM + 1);
this.setSize(d);
this.setPreferredSize(d);
this.setMinimumSize(d);
this.setMaximumSize(d);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
int n = this.mat.length;
for(int i = 0; i <= n; i++) {
g.drawLine(0, i * TAM, n * TAM, i * TAM);
g.drawLine(i * TAM, 0, i * TAM, n * TAM);
}
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
if(this.mat[i][j] == 'q')
g.drawString(
"q",
(j + 1) * TAM - TAM / 2,
(i + 1) * TAM - TAM / 2);
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment