Skip to the content.

:warning: library/graph/Grid8.hpp

Depends on

Code

#pragma once
#include "library/graph/WeightedGraph.hpp"
#define REP_(i, n) for (int i = 0; i < (n); i++)
template <typename T> class Grid8 {
    const int h, w;
    std::optional<T> ban;
    // D,DR,R,RU,U,UL,L,LD
    static constexpr std::pair<int, int> d8[8] = {
        {1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}, {-1, -1}, {0, -1}, {1, -1}};
    template <typename vecvecT> void build(const vecvecT &grid) {
        REP_(y, h) REP_(x, w) {
            int p = id(y, x);
            v[p] = grid[y][x];
            if (ban and v[p] == ban.value())
                continue;
            REP_(d, 8) {
                int y2 = y + d8[d].first, x2 = x + d8[d].second;
                if (in(y2, x2) and (!ban or ban.value() != grid[y2][x2]))
                    G.add_arc(p, id(y2, x2), d);
            }
        }
        G.build();
    }

  public:
    std::vector<T> v;
    WeightedGraph<int> G;
    bool in(int y, int x) const {
        return 0 <= y and y < h and 0 <= x and x < w;
    }
    int id(int y, int x) const {
        assert(in(y, x));
        return y * w + x;
    }
    std::pair<int, int> r2(int a) const {
        assert(0 <= a and a < h * w);
        return {a / w, a % w};
    }

    Grid8(const std::vector<std::vector<T>> &grid,
          const std::optional<T> &ban = std::nullopt)
        : h(grid.size()), w(grid[0].size()), ban(ban), v(h * w), G(h * w) {
        build(grid);
    }
    Grid8(const std::vector<std::string> &s,
          const std::optional<T> &ban = std::nullopt)
        : h(s.size()), w(s[0].size()), ban(ban), v(h * w), G(h * w) {
        static_assert(std::is_same<T, char>::value, "value_type==char");
        build(s);
    }

    int find(const T &c) const {
        REP_(i, h * w) if (v[i] == c) return i;
        return -1;
    }
};
#undef REP_
#line 2 "library/graph/WeightedGraph.hpp"
template <typename T> struct WeightedEdge {
    WeightedEdge() = default;
    WeightedEdge(int from, int to, T weight)
        : from(from), to(to), weight(weight) {}
    int from, to;
    T weight;
    operator int() const { return to; }
};

template <typename T> struct WeightedGraph {
    int n;
    using weight_type = T;
    using edge_type = WeightedEdge<T>;
    std::vector<edge_type> edges;

  protected:
    std::vector<int> in_deg;
    bool prepared;
    class OutgoingEdges {
        WeightedGraph *g;
        int l, r;

      public:
        OutgoingEdges(WeightedGraph *g, int l, int r) : g(g), l(l), r(r) {}
        edge_type *begin() { return &(g->edges[l]); }
        edge_type *end() { return &(g->edges[r]); }
        edge_type &operator[](int i) { return g->edges[l + i]; }
        int size() const { return r - l; }
    };
    class ConstOutgoingEdges {
        const WeightedGraph *g;
        int l, r;

      public:
        ConstOutgoingEdges(const WeightedGraph *g, int l, int r)
            : g(g), l(l), r(r) {}
        const edge_type *begin() const { return &(g->edges[l]); }
        const edge_type *end() const { return &(g->edges[r]); }
        const edge_type &operator[](int i) const { return g->edges[l + i]; }
        int size() const { return r - l; }
    };

  public:
    OutgoingEdges operator[](int v) {
        assert(prepared);
        return {this, in_deg[v], in_deg[v + 1]};
    }
    const ConstOutgoingEdges operator[](int v) const {
        assert(prepared);
        return {this, in_deg[v], in_deg[v + 1]};
    }

    bool is_prepared() const { return prepared; }

    WeightedGraph() : n(0), in_deg(1, 0), prepared(false) {}
    WeightedGraph(int n) : n(n), in_deg(n + 1, 0), prepared(false) {}
    WeightedGraph(int n, int m, bool directed = false, int indexed = 1)
        : n(n), in_deg(n + 1, 0), prepared(false) {
        scan(m, directed, indexed);
    }

    void resize(int n) { n = n; }

    void add_arc(int from, int to, T weight) {
        assert(!prepared);
        assert(0 <= from and from < n and 0 <= to and to < n);
        edges.emplace_back(from, to, weight);
        in_deg[from + 1]++;
    }
    void add_edge(int u, int v, T weight) {
        add_arc(u, v, weight);
        add_arc(v, u, weight);
    }
    void add_arc(const edge_type &e) { add_arc(e.from, e.to, e.weight); }
    void add_edge(const edge_type &e) { add_edge(e.from, e.to, e.weight); }

    void scan(int m, bool directed = false, int indexed = 1) {
        edges.reserve(directed ? m : 2 * m);
        while (m--) {
            int u, v;
            std::cin >> u >> v;
            u -= indexed;
            v -= indexed;
            T weight;
            std::cin >> weight;
            if (directed)
                add_arc(u, v, weight);
            else
                add_edge(u, v, weight);
        }
        build();
    }

    void build() {
        assert(!prepared);
        prepared = true;
        for (int v = 0; v < n; v++)
            in_deg[v + 1] += in_deg[v];
        std::vector<edge_type> new_edges(in_deg.back());
        auto counter = in_deg;
        for (auto &&e : edges)
            new_edges[counter[e.from]++] = e;
        edges = new_edges;
    }

    void graph_debug() const {
#ifndef __DEBUG
        return;
#endif
        assert(prepared);
        for (int from = 0; from < n; from++) {
            std::cerr << from << ";";
            for (int i = in_deg[from]; i < in_deg[from + 1]; i++)
                std::cerr << "(" << edges[i].to << "," << edges[i].weight
                          << ")";
            std::cerr << "\n";
        }
    }
};
#line 3 "library/graph/Grid8.hpp"
#define REP_(i, n) for (int i = 0; i < (n); i++)
template <typename T> class Grid8 {
    const int h, w;
    std::optional<T> ban;
    // D,DR,R,RU,U,UL,L,LD
    static constexpr std::pair<int, int> d8[8] = {
        {1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}, {-1, -1}, {0, -1}, {1, -1}};
    template <typename vecvecT> void build(const vecvecT &grid) {
        REP_(y, h) REP_(x, w) {
            int p = id(y, x);
            v[p] = grid[y][x];
            if (ban and v[p] == ban.value())
                continue;
            REP_(d, 8) {
                int y2 = y + d8[d].first, x2 = x + d8[d].second;
                if (in(y2, x2) and (!ban or ban.value() != grid[y2][x2]))
                    G.add_arc(p, id(y2, x2), d);
            }
        }
        G.build();
    }

  public:
    std::vector<T> v;
    WeightedGraph<int> G;
    bool in(int y, int x) const {
        return 0 <= y and y < h and 0 <= x and x < w;
    }
    int id(int y, int x) const {
        assert(in(y, x));
        return y * w + x;
    }
    std::pair<int, int> r2(int a) const {
        assert(0 <= a and a < h * w);
        return {a / w, a % w};
    }

    Grid8(const std::vector<std::vector<T>> &grid,
          const std::optional<T> &ban = std::nullopt)
        : h(grid.size()), w(grid[0].size()), ban(ban), v(h * w), G(h * w) {
        build(grid);
    }
    Grid8(const std::vector<std::string> &s,
          const std::optional<T> &ban = std::nullopt)
        : h(s.size()), w(s[0].size()), ban(ban), v(h * w), G(h * w) {
        static_assert(std::is_same<T, char>::value, "value_type==char");
        build(s);
    }

    int find(const T &c) const {
        REP_(i, h * w) if (v[i] == c) return i;
        return -1;
    }
};
#undef REP_
Back to top page