initial import
This commit is contained in:
commit
ea08c61688
18 changed files with 394 additions and 0 deletions
18
.classpath
Normal file
18
.classpath
Normal file
|
@ -0,0 +1,18 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.web.container"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.module.container"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jst.server.core.container/org.eclipse.jst.server.tomcat.runtimeTarget/Apache Tomcat v6.0">
|
||||
<attributes>
|
||||
<attribute name="owner.project.facets" value="jst.web"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/java-6-sun-1.6.0.15">
|
||||
<attributes>
|
||||
<attribute name="owner.project.facets" value="jst.java"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="lib" path="/usr/share/java/servlet-api-2.4.jar"/>
|
||||
<classpathentry kind="output" path="build/classes"/>
|
||||
</classpath>
|
36
.project
Normal file
36
.project
Normal file
|
@ -0,0 +1,36 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>NDameJava</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.wst.jsdt.core.javascriptValidator</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.wst.common.project.facet.core.builder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.wst.validation.validationbuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jem.workbench.JavaEMFNature</nature>
|
||||
<nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
|
||||
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
<nature>org.eclipse.wst.jsdt.core.jsNature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
11
.settings/.jsdtscope
Normal file
11
.settings/.jsdtscope
Normal file
|
@ -0,0 +1,11 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.JRE_CONTAINER"/>
|
||||
<classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.WebProject">
|
||||
<attributes>
|
||||
<attribute name="hide" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.baseBrowserLibrary"/>
|
||||
<classpathentry kind="output" path=""/>
|
||||
</classpath>
|
12
.settings/org.eclipse.jdt.core.prefs
Normal file
12
.settings/org.eclipse.jdt.core.prefs
Normal file
|
@ -0,0 +1,12 @@
|
|||
#Sat Feb 20 21:14:00 CET 2010
|
||||
eclipse.preferences.version=1
|
||||
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
|
||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
|
||||
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
|
||||
org.eclipse.jdt.core.compiler.compliance=1.6
|
||||
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
|
||||
org.eclipse.jdt.core.compiler.debug.localVariable=generate
|
||||
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
|
||||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.source=1.6
|
3
.settings/org.eclipse.ltk.core.refactoring.prefs
Normal file
3
.settings/org.eclipse.ltk.core.refactoring.prefs
Normal file
|
@ -0,0 +1,3 @@
|
|||
#Sat Feb 20 21:14:00 CET 2010
|
||||
eclipse.preferences.version=1
|
||||
org.eclipse.ltk.core.refactoring.enable.project.refactoring.history=false
|
9
.settings/org.eclipse.wst.common.component
Normal file
9
.settings/org.eclipse.wst.common.component
Normal file
|
@ -0,0 +1,9 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project-modules id="moduleCoreId" project-version="1.5.0">
|
||||
<wb-module deploy-name="test">
|
||||
<wb-resource deploy-path="/" source-path="/WebContent"/>
|
||||
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src"/>
|
||||
<property name="context-root" value="test"/>
|
||||
<property name="java-output-path"/>
|
||||
</wb-module>
|
||||
</project-modules>
|
8
.settings/org.eclipse.wst.common.project.facet.core.xml
Normal file
8
.settings/org.eclipse.wst.common.project.facet.core.xml
Normal file
|
@ -0,0 +1,8 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<faceted-project>
|
||||
<runtime name="Apache Tomcat v6.0"/>
|
||||
<fixed facet="jst.web"/>
|
||||
<fixed facet="jst.java"/>
|
||||
<installed facet="jst.java" version="6.0"/>
|
||||
<installed facet="jst.web" version="2.4"/>
|
||||
</faceted-project>
|
1
.settings/org.eclipse.wst.jsdt.ui.superType.container
Normal file
1
.settings/org.eclipse.wst.jsdt.ui.superType.container
Normal file
|
@ -0,0 +1 @@
|
|||
org.eclipse.wst.jsdt.launching.baseBrowserLibrary
|
1
.settings/org.eclipse.wst.jsdt.ui.superType.name
Normal file
1
.settings/org.eclipse.wst.jsdt.ui.superType.name
Normal file
|
@ -0,0 +1 @@
|
|||
Window
|
3
.settings/org.eclipse.wst.ws.service.policy.prefs
Normal file
3
.settings/org.eclipse.wst.ws.service.policy.prefs
Normal file
|
@ -0,0 +1,3 @@
|
|||
#Sat Feb 20 21:14:00 CET 2010
|
||||
eclipse.preferences.version=1
|
||||
org.eclipse.wst.ws.service.policy.projectEnabled=false
|
3
WebContent/META-INF/MANIFEST.MF
Normal file
3
WebContent/META-INF/MANIFEST.MF
Normal file
|
@ -0,0 +1,3 @@
|
|||
Manifest-Version: 1.0
|
||||
Class-Path:
|
||||
|
12
WebContent/WEB-INF/web.xml
Normal file
12
WebContent/WEB-INF/web.xml
Normal file
|
@ -0,0 +1,12 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
|
||||
<display-name>test</display-name>
|
||||
<welcome-file-list>
|
||||
<welcome-file>index.html</welcome-file>
|
||||
<welcome-file>index.htm</welcome-file>
|
||||
<welcome-file>index.jsp</welcome-file>
|
||||
<welcome-file>default.html</welcome-file>
|
||||
<welcome-file>default.htm</welcome-file>
|
||||
<welcome-file>default.jsp</welcome-file>
|
||||
</welcome-file-list>
|
||||
</web-app>
|
BIN
WebContent/dame.gif
Normal file
BIN
WebContent/dame.gif
Normal file
Binary file not shown.
After Width: | Height: | Size: 923 B |
72
WebContent/index.jsp
Normal file
72
WebContent/index.jsp
Normal file
|
@ -0,0 +1,72 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
|
||||
|
||||
<%@page import="java.util.ArrayList"%><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
||||
<head>
|
||||
<meta http-equiv="content-type" content="text/html; charset=utf-8"></meta>
|
||||
<title>n-Damen Problem - Backtracking</title>
|
||||
<link rel="stylesheet" type="text/css" href="schach.css" />
|
||||
<script src="schach.js" type="text/javascript"></script>
|
||||
</head>
|
||||
<body onload="schach(document.getElementById('sols').childNodes[1].value)">
|
||||
<div id="wrapper">
|
||||
<div id="header">n-Damen Problem</div>
|
||||
|
||||
<%@ page import="de.steffenvogel.ndame.DameArr" %>
|
||||
|
||||
<%
|
||||
int n = (request.getParameter("n") == null) ? 5 : Integer.parseInt(request.getParameter("n"));
|
||||
if (n > 16) {
|
||||
out.println("n too large!");
|
||||
}
|
||||
else {
|
||||
long start = System.currentTimeMillis();
|
||||
|
||||
DameArr dame = new DameArr(n);
|
||||
dame.run();
|
||||
|
||||
|
||||
long diff = (System.currentTimeMillis() - start);
|
||||
|
||||
%>
|
||||
|
||||
Für das n-Damen-Problem mit Schachbrettgröße <%= n %> wurden <%= dame.solcount %> Lösungen gefunden.<br /><br />
|
||||
|
||||
<%
|
||||
if (n < 14) {
|
||||
%>
|
||||
<select id="sols" onchange="schach(this.value)" size="2">
|
||||
<%
|
||||
for (int[] solution : dame.getSolutions()) {
|
||||
String solutionFormatted = new String();
|
||||
|
||||
for (Integer i : solution) {
|
||||
solutionFormatted += (i + 1) + "|";
|
||||
}
|
||||
solutionFormatted = solutionFormatted.substring(0, solutionFormatted.length() - 1);
|
||||
|
||||
out.println("<option value=\"" + solutionFormatted + "\">" + solutionFormatted + "</option>");
|
||||
}
|
||||
%>
|
||||
</select>
|
||||
<div id="show">Bitte eine Lösung wählen, um diese anzuzeigen.</div>
|
||||
<% } %>
|
||||
<br style="clear:both" />
|
||||
<br />Dazu wurde folgende Zeit benötigt: <%= diff %> ms.
|
||||
<% } %>
|
||||
<form action="index.jsp" method="get">
|
||||
<span>Schachbrettgröße = </span><select onchange="submit()" name="n">
|
||||
<%
|
||||
for (int i = 1; i < 14; i++) {
|
||||
out.println("<option" + ((i == n) ? " selected=\"selected\"" : "") + " value=\"" + i + "\">" + i + "</option>");
|
||||
}
|
||||
%>
|
||||
</select>
|
||||
</form>
|
||||
<div id="copy">© Steffen Vogel<br />
|
||||
<a href="mailto:info@steffenvogel.de">info@steffenvogel.de</a><br />
|
||||
<a href="http://www.steffenvogel.de">http://www.steffenvogel.de</a><br />
|
||||
Based on Micha's Javascript & CSS frontend</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
62
WebContent/schach.css
Normal file
62
WebContent/schach.css
Normal file
|
@ -0,0 +1,62 @@
|
|||
body {
|
||||
padding: 10px 0 0 30px;
|
||||
}
|
||||
|
||||
#wrapper {
|
||||
width: 1050px;
|
||||
margin: 0 auto;
|
||||
}
|
||||
|
||||
#header {
|
||||
font-size: 300%;
|
||||
text-decoration: underline;
|
||||
text-align: center;
|
||||
margin-bottom: 10px;
|
||||
}
|
||||
|
||||
#sols {
|
||||
float: left;
|
||||
height: 300px
|
||||
}
|
||||
|
||||
#copy {
|
||||
font-size: 90%;
|
||||
float: right;
|
||||
S
|
||||
}
|
||||
|
||||
#show {
|
||||
float: right;
|
||||
width: 700px;
|
||||
}
|
||||
|
||||
.show {
|
||||
background-color: #000;
|
||||
}
|
||||
|
||||
.show td {
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.show th {
|
||||
height: 30px;
|
||||
background-color: #fff;
|
||||
width: 50px;
|
||||
}
|
||||
|
||||
.headleft {
|
||||
background-color: #fff;
|
||||
width: 30px;
|
||||
}
|
||||
|
||||
.white {
|
||||
background-color: #fff;
|
||||
width: 50px;
|
||||
height: 50px;
|
||||
}
|
||||
|
||||
.black {
|
||||
background-color: #999;
|
||||
width: 50px;
|
||||
height: 50px;
|
||||
}
|
24
WebContent/schach.js
Normal file
24
WebContent/schach.js
Normal file
|
@ -0,0 +1,24 @@
|
|||
function schach(sol) {
|
||||
solution = sol.split('|');
|
||||
n = solution.length;
|
||||
|
||||
html = '<table class="show"><tr><td class="headleft"></td>';
|
||||
for (col = 0; col < n; col++)
|
||||
html += '<td class="headleft">' + (col + 1) + '</td>';
|
||||
|
||||
html += '</tr>';
|
||||
|
||||
for (row = 0; row < n; row++) {
|
||||
html += '<tr><td class="headleft">' + (row + 1) + '</td>';
|
||||
color = row % 2;
|
||||
for (col = 0; col < n; col++) {
|
||||
html += (col % 2) == color ? '<td class="black">' : '<td class="white">';
|
||||
if (col == solution[row] - 1)
|
||||
html += '<img src="dame.gif" alt="X" />';
|
||||
html += '</td>';
|
||||
}
|
||||
html += '</tr>';
|
||||
}
|
||||
html += '</table>';
|
||||
document.getElementById('show').innerHTML = html;
|
||||
}
|
BIN
build/classes/de/steffenvogel/ndame/DameArr.class
Normal file
BIN
build/classes/de/steffenvogel/ndame/DameArr.class
Normal file
Binary file not shown.
119
src/de/steffenvogel/ndame/DameArr.java
Normal file
119
src/de/steffenvogel/ndame/DameArr.java
Normal file
|
@ -0,0 +1,119 @@
|
|||
package de.steffenvogel.ndame;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class DameArr {
|
||||
private int n;
|
||||
private int row;
|
||||
private int col;
|
||||
|
||||
private ArrayList<int[]> solutions;
|
||||
private int[][] locked;
|
||||
private int[] set;
|
||||
|
||||
public int solcount = 0;
|
||||
|
||||
public DameArr(int n) {
|
||||
this.n = n;
|
||||
|
||||
solutions = new ArrayList<int[]>();
|
||||
locked = new int[n][n];
|
||||
set = new int[n];
|
||||
|
||||
row = 0;
|
||||
col = 0;
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
set[i] = -1; // noch keine Dame wurde in diese Zeile gesetzt
|
||||
}
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
for (int j = 0; j < n; j++) {
|
||||
locked[i][j] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void run() {
|
||||
while (row < n) {
|
||||
while (col < n) {
|
||||
if (check(row, col) && col > set[row]) {
|
||||
set(row, col);
|
||||
break;
|
||||
} else {
|
||||
col++;
|
||||
}
|
||||
}
|
||||
|
||||
if (col == n) {
|
||||
if (set[row - 1] >= n - 1 && row == 1)
|
||||
break;
|
||||
else
|
||||
backtrack();
|
||||
} else {
|
||||
row++;
|
||||
col = 0;
|
||||
|
||||
if (row == n) {
|
||||
if (n < 14)
|
||||
solutions.add(set.clone());
|
||||
solcount++;
|
||||
backtrack();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean check(int row, int col) {
|
||||
return (locked[row][col] > 0) ? false : true;
|
||||
}
|
||||
|
||||
private void set(int row, int col) {
|
||||
for (int i = 0; i < n; i++)
|
||||
locked[row][i]++; // Vertikal
|
||||
|
||||
for (int j = 0; j < n; j++)
|
||||
locked[j][col]++; // Horizontal
|
||||
|
||||
for (int k = 0; k < n; k++)
|
||||
if ((col - row + k) < n && (col - row + k) >= 0)
|
||||
locked[k][col - row + k]++; // Diagonal oben links -> unten rechts
|
||||
|
||||
for (int l = 0; l < n; l++)
|
||||
if ((row + col - l) < n && (row + col - l) >= 0)
|
||||
locked[l][row + col - l]++; // Diagonal unten links -> oben rechts
|
||||
|
||||
set[row] = col;
|
||||
}
|
||||
|
||||
void unSet(int row, int col) {
|
||||
for (int i = 0; i < n; i++)
|
||||
locked[row][i]--; // Vertikal
|
||||
|
||||
for (int j = 0; j < n; j++)
|
||||
locked[j][col]--; // Horizontal
|
||||
|
||||
for (int k = 0; k < n; k++)
|
||||
if ((col - row + k) < n && (col - row + k) >= 0)
|
||||
locked[k][col - row + k]--; // Diagonal oben links -> unten rechts
|
||||
|
||||
for (int l = 0; l < n; l++)
|
||||
if ((row + col - l) < n && (row + col - l) >= 0)
|
||||
locked[l][row + col - l]--; // Diagonal unten links -> oben rechts
|
||||
|
||||
set[row] = -1;
|
||||
}
|
||||
|
||||
public ArrayList<int[]> getSolutions() {
|
||||
return solutions;
|
||||
}
|
||||
|
||||
private void backtrack() {
|
||||
row--;
|
||||
|
||||
col = set[row] + 1;
|
||||
unSet(row, set[row]);
|
||||
}
|
||||
}
|
||||
|
Loading…
Add table
Reference in a new issue