#include #include #include #include #include #include #include #include #define rep(i, a, b) for (int i = int(a); i < int(b); i++) using namespace std; using ll = long long int; // NOLINT using P = pair; // clang-format off #ifdef _DEBUG_ #define dump(...) do{ cerr << __LINE__ << ":\t" << #__VA_ARGS__ << " = "; debug_print(__VA_ARGS__); } while(false) template void debug_print(const T &t, const Ts &...ts) { cerr << t; ((cerr << ", " << ts), ...); cerr << endl; } #else #define dump(...) do{ } while(false) #endif template vector make_v(size_t a, T b) { return vector(a, b); } template auto make_v(size_t a, Ts... ts) { return vector(a, make_v(ts...)); } template bool chmin(T &a, const T& b) { if (a > b) {a = b; return true; } return false; } template bool chmax(T &a, const T& b) { if (a < b) {a = b; return true; } return false; } template void print(const T& t, const Ts&... ts) { cout << t; ((cout << ' ' << ts), ...); cout << '\n'; } template void input(Ts&... ts) { (cin >> ... >> ts); } template istream &operator,(istream &in, T &t) { return in >> t; } struct Inf { template constexpr operator T() { return numeric_limits::max() / 2; } }; // clang-format on #include #include #include #include enum class CostType { NoEdge, One, ZeroOne, Plus, PlusMinus, }; template class Graph { using Edge = pair; using ShortestInfo = pair, vector>; vector> g; CostType cost_type; ShortestInfo no_edge(Vertex s) { auto costs = vector(g.size(), Inf); auto prev = vector(g.size(), None); costs[s] = 0; return make_pair(move(costs), move(prev)); } ShortestInfo bfs01(Vertex s) { auto [costs, prev] = no_edge(s); deque deq; deq.emplace_back(0, s); while (deq.size()) { auto [c, v] = deq.front(); deq.pop_front(); if (costs[v] < c) continue; for (auto &&[nc, nv] : adjacent(v)) { if (costs[nv] > c + nc) { costs[nv] = c + nc; if (nc == 0) { deq.emplace_front(c, nv); } else { deq.emplace_back(c + 1, nv); } prev[nv] = v; } } } return make_pair(move(costs), move(prev)); } ShortestInfo bfs(Vertex s) { auto [costs, prev] = no_edge(s); queue que; que.emplace(0, s); while (que.size()) { auto [c, v] = que.front(); que.pop(); if (costs[v] < c) continue; for (auto &&[nc, nv] : adjacent(v)) { if (costs[nv] > c + nc) { costs[nv] = c + nc; que.emplace(c + nc, nv); prev[nv] = v; } } } return make_pair(move(costs), move(prev)); } ShortestInfo dijkstra(Vertex s) { auto [costs, prev] = no_edge(s); priority_queue, greater> pq; pq.emplace(0, s); while (pq.size()) { auto [c, v] = pq.top(); pq.pop(); if (costs[v] < c) continue; for (auto &&[nc, nv] : adjacent(v)) { if (costs[nv] > c + nc) { costs[nv] = c + nc; pq.emplace(c + nc, nv); prev[nv] = v; } } } return make_pair(move(costs), move(prev)); } ShortestInfo bellman_ford(Vertex s) { auto [costs, prev] = no_edge(s); for (size_t i = 0, n = g.size(); i < 2 * n; i++) { for (Vertex v = 0; v < static_cast(n); v++) { if (costs[v] == Inf) continue; for (auto &&[cost, nv] : adjacent(v)) { if (costs[v] == -Inf || costs[v] + cost < costs[nv]) { if (i >= n) { costs[nv] = -Inf; } else { costs[nv] = costs[v] + cost; } prev[nv] = v; } } } } return make_pair(move(costs), move(prev)); } void update_cost_type(Cost cost) { CostType ct = CostType::NoEdge; if (signbit(cost)) { ct = CostType::PlusMinus; } else if (cost != 0 && cost != 1) { ct = CostType::Plus; } else if (cost == 0) { ct = CostType::ZeroOne; } else if (cost == 1) { ct = CostType::One; } cost_type = static_cast(max(static_cast(ct), static_cast(cost_type))); } public: Graph(size_t n) : g(n), cost_type(CostType::NoEdge) {} void add_edge(Vertex s, Vertex t, Cost cost) { update_cost_type(cost); g[s].emplace_back(cost, t); } const vector &adjacent(Vertex v) const { return g[v]; } size_t size() const { return g.size(); } CostType get_cost_type() const { return cost_type; } ShortestInfo shortest(Vertex s) { switch (cost_type) { case CostType::ZeroOne: return bfs01(s); case CostType::One: return bfs(s); case CostType::Plus: return dijkstra(s); case CostType::PlusMinus: return bellman_ford(s); case CostType::NoEdge: return no_edge(s); } __builtin_unreachable(); } constexpr static Cost Inf = numeric_limits::max(); constexpr static Vertex None = -1; }; template class GridGraphWrapper : public Graph { vector> map; constexpr static int Dx[] = {0, 1}; constexpr static int Dy[] = {1, 0}; Vertex flatten(Vertex y, Vertex x) { return y * static_cast(map[0].size()) + x; } bool isin(Vertex y, Vertex x) { return 0 <= y && y < static_cast(map.size()) && 0 <= x && x < static_cast(map[y].size()); } public: GridGraphWrapper(size_t h, size_t w) : Graph(h * w), map(h, vector(w, Block)) {} void set_cost(Vertex y, Vertex x, Cost c) { map[y][x] = c; } void build() { for (Vertex y = 0; y < static_cast(map.size()); y++) { for (Vertex x = 0; x < static_cast(map[y].size()); x++) { if (map[y][x] == Block) continue; Vertex id = flatten(y, x); for (size_t k = 0; k < ::size(Dx); k++) { Vertex nx = x + Dx[k], ny = y + Dy[k]; if (isin(ny, nx) && map[ny][nx] != Block) { Vertex nid = flatten(ny, nx); this->add_edge(id, nid, map[ny][nx]); } } } } } constexpr static Cost Block = Graph::Inf; }; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int h, w; cin, h, w; GridGraphWrapper<> g(h, w); rep(i, 0, h) { rep(j, 0, w) { char c; cin, c; if (c == 'k') { g.set_cost(i, j, i + j + 1); } else { g.set_cost(i, j, 1); } } } g.build(); auto [cost, prev] = g.shortest(0); print(cost[h * w - 1]); return 0; }