From e9724ddf1ef6b9c2d4ec6d4741d1d496984221f0 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Thu, 10 Nov 2022 21:32:59 +0800 Subject: [PATCH] P4667 [BalticOI 2011 Day1]Switch the Lamp On https://www.luogu.com.cn/record/93519277 --- Luogu/P4667/P4667.cpp | 76 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 Luogu/P4667/P4667.cpp diff --git a/Luogu/P4667/P4667.cpp b/Luogu/P4667/P4667.cpp new file mode 100644 index 00000000..23cea9af --- /dev/null +++ b/Luogu/P4667/P4667.cpp @@ -0,0 +1,76 @@ +#include +#include +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 505; +const int INF = 0x3f3f3f3f; + +int n, m, dist[N * N]; +std::vector> g[N * N]; +bool vis[N * N]; + +int id(int x, int y) { + return x * (m + 1) + y; +} + +void dijkstra() { + std::fill_n(dist, N * N, INF); + + std::priority_queue, std::vector>, std::greater>> q; + + q.emplace(0, id(0, 0)); + dist[id(0, 0)] = 0; + + while (!q.empty()) { + int u = q.top().second; + q.pop(); + + if (vis[u]) continue; + vis[u] = true; + + for (auto e : g[u]) { + int v = e.first, + w = e.second; + + if (dist[v] > dist[u] + w) { + dist[v] = dist[u] + w; + q.emplace(dist[v], v); + } + } + } +} + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> n >> m; + + for (int i = 1; i <= n; i++) { + for (int j = 1; j <= m; j++) { + char c; + + cin >> c; + + g[id(i, j)].emplace_back(id(i - 1, j - 1), c != '\\'); + g[id(i - 1, j - 1)].emplace_back(id(i, j), c != '\\'); + g[id(i, j - 1)].emplace_back(id(i - 1, j), c != '/'); + g[id(i - 1, j)].emplace_back(id(i, j - 1), c != '/'); + } + } + + dijkstra(); + + if (dist[id(n, m)] == INF) { + cout << "NO SOLUTION" << endl; + } else { + cout << dist[id(n, m)] << endl; + } + + return 0; +}