2 条题解
-
0陈烨鑫 (chenyexin) LV 10 @ 2023-4-23 16:41:15
还是我来吧~
#include <iostream> #include <cstring> #include <deque> using namespace std; const int N = 510; int n, m; char g[N][N]; int dist[N][N]; bool st[N][N]; int bfs() { memset(st, false, sizeof st); memset(dist, 0x3f, sizeof dist); string s = "\\/\\/"; int dx[] = {-1, -1, 1, 1}, dy[] = {-1, 1, 1, -1}; int ix[] = {-1, -1, 0, 0}, iy[] = {-1, 0, 0, -1}; deque<pair<int, int> > dq; dq.push_back({0, 0}); dist[0][0] = 0; while (!dq.empty()) { auto t = dq.front(); dq.pop_front(); int x = t.first, y = t.second; if (x == n && y == m) return dist[x][y]; if (st[x][y]) continue; st[x][y] = true; for (int i = 0; i < 4; i++) { int nx = x + dx[i], ny = y + dy[i]; if (st[nx][ny]) continue; if (0 <= nx && nx <= n && 0 <= ny && ny <= m) { int gx = x + ix[i], gy = y + iy[i]; int w = (g[gx][gy] != s[i]); int d = dist[x][y] + w; if (d < dist[nx][ny]) { dist[nx][ny] = d; if (w) dq.push_back({nx, ny}); else dq.push_front({nx, ny}); } } } } return -1; } int main() { int T; cin >> T; while (T--) { cin >> n >> m; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) cin >> g[i][j]; if ((n + m) % 2) cout << "NO SOLUTION" << endl; else cout << bfs() << endl; } return 0; }
-
-12021-8-7 21:05:13@
C++ :
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int INF = 0x3f3f3f3f; const int MAXN = 1e6 + 100; const int MAXM = 3e3 + 10; template < typename T > inline void read(T &x) { x = 0; T ff = 1, ch = getchar(); while(!isdigit(ch)) { if(ch == '-') ff = -1; ch = getchar(); } while(isdigit(ch)) { x = (x << 1) + (x << 3) + (ch ^ 48); ch = getchar(); } x *= ff; } template < typename T > inline void write(T x) { if(x < 0) putchar('-'), x = -x; if(x > 9) write(x / 10); putchar(x % 10 + '0'); } int T, n, m; int vis[MAXN], dis[MAXN]; int lin[MAXN], tot = 0; char ch; struct edge { int y, v, next; } e[MAXN]; inline void add(int xx, int yy, int vv) { e[++tot].y = yy; e[tot].v = vv; e[tot].next = lin[xx]; lin[xx] = tot; } void Dijkstra() { memset(dis, 0x3f, sizeof(dis)); memset(vis, false, sizeof(vis)); priority_queue < pair < int, int > > q; dis[1] = 0; q.push(make_pair(0, 1)); while(!q.empty()) { int x = q.top().second; q.pop(); if(vis[x]) continue; vis[x] = true; for(int i = lin[x], y; i; i = e[i].next) { if(dis[y = e[i].y] > dis[x] + e[i].v) { dis[y] = dis[x] + e[i].v; if(!vis[y]) q.push(make_pair(-dis[y], y)); } } } } int main() { read(T); while(T--) { read(n); read(m); memset(lin, 0, sizeof(lin)); memset(e, 0, sizeof(e)); tot = 0; for(int i = 1; i <= n; ++i) { for(int j = 1; j <= m; ++j) { ch = getchar(); if(ch == '/') { add(((i - 1) * (m + 1) + j), i * (m + 1) + j + 1, 1); add(i * (m + 1) + j + 1, (i - 1) * (m + 1) + j, 1); add(i * (m + 1) + j, (i - 1) * (m + 1) + j + 1, 0); add((i - 1) * (m + 1) + j + 1, i * (m + 1) + j, 0); } else { add(((i - 1) * (m + 1) + j), i * (m + 1) + j + 1, 0); add(i * (m + 1) + j + 1, (i - 1) * (m + 1) + j, 0); add(i * (m + 1) + j, (i - 1) * (m + 1) + j + 1, 1); add((i - 1) * (m + 1) + j + 1, i * (m + 1) + j, 1); } } ch = getchar(); } Dijkstra(); if(dis[(n + 1) * (m + 1)] == INF) puts("NO SOLUTION"); else write(dis[(n + 1) * (m + 1)]), puts(""); } return 0; }
- 1
信息
- ID
- 86
- 时间
- 1000ms
- 内存
- 128MiB
- 难度
- 6
- 标签
- 递交数
- 61
- 已通过
- 19
- 上传者