[go: up one dir, main page]

Menu

[dc7c97]: / src / polygon-walker.cc  Maximize  Restore  History

Download this file

120 lines (103 with data), 2.5 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
* mazen-ng
* Copyright (C) Jacob Zimmermann 2009 <jacob@jzimm.net>
*
* mazen-ng is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* mazen-ng is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma implementation
#include "polygon-walker.h"
#include <stdexcept>
PolygonWalker::PolygonWalker (const GridMaze& m): Walker (m) {
}
void PolygonWalker::map (xy p, int c) {
s_mat (p, c);
int nc = c + 1;
if (p.x < maze.width () - 1) {
xy pr = p.right ();
if (!maze.vert (pr) && g_mat (pr) > nc)
map (pr, nc);
}
if (p.x > 0 && !maze.vert (p)) {
xy pl = p.left ();
if (g_mat (pl) > nc)
map (pl, nc);
}
xy pu = p.up ();
pu.y %= maze.height ();
if (!maze.horiz (pu) && g_mat (pu) > nc)
map (pu, nc);
if (!maze.horiz (p)) {
xy pd = p.down ();
if (pd.y < 0)
pd.y = maze.height () - 1;
if (g_mat (pd) > nc)
map (pd, nc);
}
}
void PolygonWalker::gen_solution (std::list<xy>& s) const {
xy p = maze.get_entrance ();
int c = g_mat (p);
int best_c, nc;
xy best_p;
int mwidth = maze.width ();
int mheight = maze.height ();
while (c > 0) {
s.push_back (p);
best_c = c;
if (p.x > 0 && !maze.vert (p)) {
xy pl = p.left ();
nc = g_mat (pl);
if (nc < best_c) {
best_p = pl;
best_c = nc;
}
}
if (p.x < mwidth) {
xy pr = p.right ();
if (!maze.vert (pr)) {
nc = g_mat (pr);
if (nc < best_c) {
best_p = pr;
best_c = nc;
}
}
}
if (!maze.horiz (p)) {
xy pd = p.down ();
if (pd.y < 0)
pd.y = mheight - 1;
nc = g_mat (pd);
if (nc < best_c) {
best_p = pd;
best_c = nc;
}
}
if (p.y < mheight) {
xy pu = p.up ();
pu.y %= mheight;
if (!maze.horiz (pu)) {
nc = g_mat (pu);
if (nc < best_c) {
best_p = pu;
best_c = nc;
}
}
}
if (c == best_c && best_c > 0)
throw std::runtime_error ("The impossible happened in PolygonWalker::gen_solution ()");
c = best_c;
p = best_p;
};
}