Fixed the bug that made walls not receive the algorithm.

main
Zed A. Shaw 3 months ago
parent 4d748d1f48
commit ebb5360c5c
  1. 10
      map.cpp
  2. 3
      map.hpp
  3. 47
      tests/map.cpp

@ -5,8 +5,8 @@
using std::vector, std::pair;
using namespace fmt;
void dump_map(Matrix &map) {
println("-----------------");
void dump_map(const std::string &msg, Matrix &map) {
println("----------------- {}", msg);
for(auto row : map) {
for(auto col : row) {
print("{} ", col);
@ -40,15 +40,15 @@ Matrix dijkstra_map(Matrix &input_map, Matrix &walls_map, int limit) {
size_t w = input_map[0].size();
// Initialize the new array with every pixel at limit distance
Matrix new_arr = Matrix(h, MatrixRow(w, 1));
// NOTE: this is normally ones() * limit
Matrix new_arr = Matrix(h, MatrixRow(w, limit));
Matrix closed = walls_map;
PairList starting_pixels;
PairList open_pixels;
limit = limit == 0 ? h * w : limit;
// First pass: Add starting pixels and put them in closed
for(size_t counter = 0; counter < h * w; counter++) {
for(size_t counter = 0; counter < h * w; counter++) {
size_t i = counter % w;
size_t j = counter / w;
if(input_map[j][i] == 0) {

@ -1,6 +1,7 @@
#pragma once
#include <vector>
#include <utility>
#include <string>
struct Pair {
size_t j = 0;
@ -11,7 +12,7 @@ typedef std::vector<Pair> PairList;
typedef std::vector<int> MatrixRow;
typedef std::vector<MatrixRow> Matrix;
void dump_map(Matrix &map);
void dump_map(const std::string &msg, Matrix &map);
void add_neighbors(Matrix &closed, size_t j, size_t i);
Matrix dijkstra_map(Matrix &input_map, Matrix &walls_map, int limit=0);

@ -1,35 +1,34 @@
#include <catch2/catch_test_macros.hpp>
#include "map.hpp"
#include <fmt/core.h>
#include <nlohmann/json.hpp>
#include <fstream>
using namespace fmt;
using namespace nlohmann;
using std::string;
json load_test_data(const string &fname) {
std::ifstream infile(fname);
return json::parse(infile);
}
TEST_CASE("dijkstra algo test", "[map]") {
Matrix in_map = {
{1, 1, 1, 0},
{1, 0, 1, 1},
{1, 0, 1, 1},
{1, 1, 1, 1},
};
Matrix walls = {
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 1, 0},
{0, 0, 1, 0},
};
Matrix expected = {
{1, 1, 1, 0},
{1, 0, 1, 1},
{1, 0, 0, 2},
{1, 1, 0, 3},
};
json data = load_test_data("./tests/dijkstra.json");
Matrix res = dijkstra_map(in_map, walls);
for(auto &test : data) {
Matrix in_map = test["input"];
Matrix walls = test["walls"];
Matrix expected = test["expected"];
int limit = test["limit"];
Matrix res = dijkstra_map(in_map, walls, limit);
println("--- EXPECTED:");
dump_map(expected);
println("--- RESULT:");
dump_map(res);
if(res != expected) {
println("ERROR! ------");
dump_map("EXPECTED", expected);
dump_map("RESULT", res);
}
REQUIRE(res == expected);
REQUIRE(res == expected);
}
}

Loading…
Cancel
Save