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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
| #include <bits/stdc++.h>
using namespace std; #define INF 0x3f3f3f3f #define LC(x) (x<<1) #define RC(x) ((x<<1)+1) #define MID(x,y) ((x+y)>>1) #define pb(x) push_back(x) #define sf(x) scanf("%d", &x) #define all(a) (a).begin(),(a).end() #define clr(arr,val) memset(arr,val,sizeof(arr)) #define FAST_IO ios::sync_with_stdio(false);cin.tie(0); #define caseT int _T;scanf("%d",&_T);for (int q=1; q<=_T; ++q) typedef pair<int, int> pii;
typedef long long ll; const double PI = acos(-1.0); const int N = 110; char G[N][N]; struct edge { int to, nxt, w; edge() {}; edge(int _to, int _nxt, int _w): to(_to), nxt(_nxt), w(_w) {}; } E[N * N * 4]; int head[N * N], tot; int dis[N * N], vis[N * N], dir[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; int n, m; pii trans[27][2];
void init() { clr(head, -1); tot = 0; for (int i = 0; i < 27; ++i) trans[i][0] = trans[i][1] = {-1, -1}; } inline void add(int s, int t, int w) { E[tot] = edge(t, head[s], w); head[s] = tot++; } inline bool chk(int x, int y) { return x >= 0 && x < n && y >= 0 && y < m; } void spfa(int s) { clr(dis, INF); clr(vis, 0); deque<int>q; q.push_back(s); dis[s] = 0; vis[s] = 1; while (!q.empty()) { int u = q.front(); q.pop_front(); vis[u] = 0; for (int i = head[u]; ~i; i = E[i].nxt) { int v = E[i].to; if(dis[v] > dis[u] + E[i].w) { dis[v] = dis[u] + E[i].w; if(!vis[v]) { if(!q.empty() && dis[v] < dis[q.front()]) q.push_front(v); else q.push_back(v); vis[v] = 1; } } } } } int main(void) { init(); scanf("%d%d", &n, &m); for (int i = 0; i < n; ++i) { scanf("%s", G[i]); for (int j = 0; j < m; ++j) { if(isupper(G[i][j])) { int x = G[i][j] - 'A'; if(trans[x][0].first == -1) trans[x][0] = {i, j}; else trans[x][1] = {i, j}; } } } for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { if(G[i][j] == '0') { for (int k = 0; k < 4; ++k) { int ii = i + dir[k][0], jj = j + dir[k][1]; if(chk(ii, jj) && G[ii][jj] != '1') { add(i * m + j, ii * m + jj, 1); } } } else if(isupper(G[i][j])) { int x = G[i][j] - 'A'; if(trans[x][0] == make_pair(i, j)) { int ii = trans[x][1].first, jj = trans[x][1].second; for (int k = 0; k < 4; ++k) { int iii = ii + dir[k][0], jjj = jj + dir[k][1]; if(chk(iii, jjj) && G[iii][jjj] != '1') add(i * m + j, iii * m + jjj, 1); } } else { int ii = trans[x][0].first, jj = trans[x][0].second; for (int k = 0; k < 4; ++k) { int iii = ii + dir[k][0], jjj = jj + dir[k][1]; if(chk(iii, jjj) && G[iii][jjj] != '1') add(i * m + j, iii * m + jjj, 1); } } } } } spfa(0); dis[n * m - 1] == INF ? puts("No Solution.") : printf("%d\n", dis[n * m - 1]); return 0; }
|