initial import
This commit is contained in:
commit
de3f616219
9 changed files with 384 additions and 0 deletions
5
.buildpath
Normal file
5
.buildpath
Normal file
|
@ -0,0 +1,5 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<buildpath>
|
||||
<buildpathentry kind="src" path=""/>
|
||||
<buildpathentry kind="con" path="org.eclipse.php.core.LANGUAGE"/>
|
||||
</buildpath>
|
5
.gitignore
vendored
Normal file
5
.gitignore
vendored
Normal file
|
@ -0,0 +1,5 @@
|
|||
.settings
|
||||
.cproject
|
||||
.project
|
||||
.classpath
|
||||
|
16
Makefile
Normal file
16
Makefile
Normal file
|
@ -0,0 +1,16 @@
|
|||
CXX = g++
|
||||
CXXFLAGS = -g -Wall
|
||||
|
||||
OBJS = ndame.o
|
||||
TARGET = dame
|
||||
|
||||
$(TARGET): $(OBJS)
|
||||
$(CXX) -o $(TARGET) $(OBJS)
|
||||
|
||||
all: $(TARGET)
|
||||
|
||||
clean:
|
||||
rm -f $(OBJS) $(TARGET)
|
||||
|
||||
%.o: $(SRCDIR)/%.cpp
|
||||
$(CXX) $(CCXFLAGS) -c $<
|
BIN
dame.gif
Normal file
BIN
dame.gif
Normal file
Binary file not shown.
After Width: | Height: | Size: 923 B |
60
index.php
Normal file
60
index.php
Normal file
|
@ -0,0 +1,60 @@
|
|||
<?php
|
||||
@error_reporting(E_ALL);
|
||||
@ini_set('arg_separator.output','&');
|
||||
@ini_set('display_errors','on');
|
||||
|
||||
set_time_limit(20);
|
||||
|
||||
$n = empty($_GET['n']) ? 5 : (int) $_GET['n'];
|
||||
if ($n > 13) {
|
||||
die('Sorry but his is too large!');
|
||||
}
|
||||
|
||||
echo '<?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">
|
||||
<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">
|
||||
<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>
|
||||
<div id="wrapper">
|
||||
<div id="header">n-Damen Problem</div>
|
||||
';
|
||||
|
||||
$start = microtime(true);
|
||||
$solutions = shell_exec('./dame ' . $n);
|
||||
$end = microtime(true);
|
||||
$diff = round(($end - $start) * 1000);
|
||||
$count = count(explode("\n", $solutions));
|
||||
|
||||
echo 'Für das n-Damen-Problem mit Schachbrettgröße ' . $n . ' wurden ' . $count . ' Lösungen gefunden.<br /><br />
|
||||
<select id="sols" onchange="schach(this.value)" size="2">';
|
||||
|
||||
foreach (explode("\n", $solutions) as $solution) {
|
||||
echo '<option value="' . $solution . '">' . $solution . '</option>';
|
||||
}
|
||||
|
||||
echo '</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.php" method="get">
|
||||
Schachbrettgröße = <select onchange="submit()" name="n">';
|
||||
|
||||
for ($i = 3; $i < 14; $i++) {
|
||||
echo '<option ' . (($i == $n) ? 'selected="selected" ' : '') . 'value="' . $i . '">' . $i . '</option>';
|
||||
}
|
||||
|
||||
echo '</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>';
|
||||
|
||||
?>
|
182
ndame.cpp
Normal file
182
ndame.cpp
Normal file
|
@ -0,0 +1,182 @@
|
|||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
|
||||
//#define DEBUG
|
||||
|
||||
#include "ndame.h"
|
||||
|
||||
NDame::NDame(int n) {
|
||||
this->n = n;
|
||||
this->row = 0;
|
||||
this->col = 0;
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
set.push_back(-1); // noch keine Dame wurde in diese Zeile gesetzt
|
||||
for (int j = 0; j < n; j++) {
|
||||
std::vector<int> temp(n, 0);
|
||||
locked.push_back(temp);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void NDame::Run() {
|
||||
while (row < n) {
|
||||
while (col < n) {
|
||||
if (Check(row, col) && col > set[row]) {
|
||||
Set(row, col);
|
||||
|
||||
#ifdef DEBUG
|
||||
Show(&set);
|
||||
#endif /* DEBUG */
|
||||
|
||||
break;
|
||||
} else {
|
||||
col++;
|
||||
|
||||
#ifdef DEBUG
|
||||
std::cout << "col++" << std::endl;
|
||||
#endif /* DEBUG */
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
if (col == n) {
|
||||
if (set[row - 1] >= n -1 && row == 0)
|
||||
break;
|
||||
else
|
||||
Backtrack();
|
||||
} else {
|
||||
row++;
|
||||
col = 0;
|
||||
|
||||
if (row == n) {
|
||||
#ifdef DEBUG
|
||||
std::cout << "New Solution: Nr. " << sol_count << std::endl;
|
||||
#endif /* DEBUG */
|
||||
|
||||
solutions.push_back(set);
|
||||
Backtrack();
|
||||
}
|
||||
|
||||
#ifdef DEBUG
|
||||
std::cout << "row++; col = 0;" << std::endl;
|
||||
#endif /* DEBUG */
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool NDame::Check(int row, int col) {
|
||||
bool check;
|
||||
check = (locked[row][col] > 0) ? false : true;
|
||||
|
||||
#ifdef DEBUG
|
||||
std::cout << "Check: " << row << "|" << col << " " << check << std::endl;
|
||||
#endif /* DEBUG */
|
||||
|
||||
return check;
|
||||
}
|
||||
|
||||
void NDame::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;
|
||||
|
||||
#ifdef DEBUG
|
||||
std::cout << "Neue Dame: " << row << "|" << col << std::endl;
|
||||
#endif /* DEBUG */
|
||||
|
||||
}
|
||||
|
||||
void NDame::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;
|
||||
|
||||
#ifdef DEBUG
|
||||
std::cout << "Dame entfernt: " << row << "|" << col << std::endl;
|
||||
#endif /* DEBUG */
|
||||
|
||||
}
|
||||
|
||||
void NDame::Show(const std::vector<int> *p) {
|
||||
std::cout << " ||";
|
||||
for (int l = 0; l < n; l++)
|
||||
std::cout << l << "|";
|
||||
std::cout << std::endl;
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
std::cout << ((i < 10) ? " " : "") << i << "||";
|
||||
for (int j = 0; j < n; j++)
|
||||
std::cout << ((p->at(i) == j) ? 'X' : ' ') << "|";
|
||||
|
||||
std::cout << " " << p->at(i) << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
if (argc >= 2) {
|
||||
//std::cout << "Berechne das n-Damenproblem für " << argv[1] << " Damen" << std::endl;
|
||||
NDame dame(atoi(argv[1]));
|
||||
dame.Run();
|
||||
|
||||
dame.ShowSolutions();
|
||||
|
||||
//std::cout << dame.solutions.size() << " Lösungen" << std::endl;
|
||||
|
||||
if (argc == 3)
|
||||
dame.Show(&dame.solutions.at(atoi(argv[2]) - 1));
|
||||
} else {
|
||||
std::cout << "Bitte geben Sie einen Paramenter an!" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
void NDame::ShowSolutions() {
|
||||
for (std::vector<std::vector<int> >::iterator i = solutions.begin(); i
|
||||
!= solutions.end(); i++) {
|
||||
for (std::vector<int>::iterator p = i->begin(); p != i->end(); p++)
|
||||
std::cout << *p + 1 << ((*p != i->back()) ? "|" : "");
|
||||
if (*i != solutions.back())
|
||||
std::cout << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
void NDame::Backtrack() {
|
||||
|
||||
#ifdef DEBUG
|
||||
std::cout << "Backtrack: we will reject dame " << row - 1 << "|" << set[row - 1] << std::endl;
|
||||
#endif /* DEBUG */
|
||||
|
||||
col = set[row - 1] + 1;
|
||||
UnSet(row - 1, set[row - 1]);
|
||||
|
||||
#ifdef DEBUG
|
||||
Show(&set);
|
||||
#endif /* DEBUG */
|
||||
|
||||
row--;
|
||||
}
|
30
ndame.h
Normal file
30
ndame.h
Normal file
|
@ -0,0 +1,30 @@
|
|||
#ifndef NDAME_H_
|
||||
#define NDAME_H_
|
||||
|
||||
#include <vector>
|
||||
|
||||
class NDame {
|
||||
public:
|
||||
NDame(int n);
|
||||
|
||||
int n;
|
||||
int row;
|
||||
int col;
|
||||
|
||||
std::vector<std::vector<int> > solutions;
|
||||
std::vector<std::vector<int> > locked;
|
||||
std::vector<int> set;
|
||||
|
||||
void Run();
|
||||
void Show(const std::vector<int> *p);
|
||||
void ShowSolutions();
|
||||
void Set(int row, int col);
|
||||
void UnSet(int row, int col);
|
||||
void Backtrack();
|
||||
bool Check(int row, int col);
|
||||
|
||||
private:
|
||||
|
||||
};
|
||||
|
||||
#endif /*NDAME_H_*/
|
62
schach.css
Normal file
62
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
schach.js
Normal file
24
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 += '<th>' + (col + 1) + '</th>';
|
||||
|
||||
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;
|
||||
}
|
Loading…
Add table
Reference in a new issue