1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
| #include <iostream> #include <cstring> #include <algorithm> #include <deque> #include <string>
#define PII pair<int, int> #define x first #define y second
using namespace std;
const int N = 510;
int t, n, m; char g[N][N]; bool st[N][N]; int dist[N][N];
char way[5] = "\\/\\/"; int iy[] = {-1, 0, 0, -1}, ix[] = {-1, -1, 0, 0}; int dx[] = {-1, -1, 1, 1}, dy[] = {-1, 1, 1, -1};
int bfs() { deque<PII> q; memset(st, false, sizeof st); memset(dist, 0x3f, sizeof dist); q.push_back({0, 0}); dist[0][0] = 0; while(!q.empty()) { auto t = q.front(); q.pop_front(); if (t.x == n && t.y == m) return dist[t.x][t.y]; if (st[t.x][t.y]) continue; st[t.x][t.y] = true; for (int i = 0; i < 4; ++i) { int x = t.x + dx[i], y = t.y + dy[i]; if (x < 0 || y < 0 || x > n || y > m) continue; int gx = t.x + ix[i], gy = t.y + iy[i]; int w = (g[gx][gy] != way[i]); int d = dist[t.x][t.y] + w; if (d <= dist[x][y]) { dist[x][y] = d; if (!w) q.push_front({x, y}); else q.push_back({x, y}); } } } return -1; }
int main() { cin >> t; while(t --) { cin >> n >> m; string temp; for (int i = 0; i < n; ++i) { cin >> temp; for (int j = 0; j < m; ++j) g[i][j] = temp[j]; } if ((n + m) % 2) cout << "NO SOLUTION\n"; else { cout << bfs() << endl; } } }
|