3 4 4 5 2134 2#23 2#22 2221 1 1 3 3 4 4 7 2134 2#23 2#22 2221 1 1 3 3 4 4 50 2#34 2#23 2#22 2#21 1 1 3 3
1.03 0.00 No Answer
广搜
#include<map> #include<cmath> #include<queue> #include<stack> #include<string> #include<cstdio> #include<vector> #include<cstring> #include<iostream> #include<algorithm> using namespace std; typedef long long LL; const int maxn = 105; int T, n, m, k, bx, by, ex, ey; int a[maxn][maxn]; int c[4][2] = { 1, 0, -1, 0, 0, 1, 0, -1 }; double f[maxn][maxn][maxn]; char s[maxn]; struct point { int x, y, z; point(){}; point(int x, int y, int z) :x(x), y(y), z(z){}; }; void bfs() { for (int i = 1; i <= n;i++) for (int j = 1; j <= m;j++) for (int u = 0; u <= k; u++) f[i][j][u] = 0x7FFFFFFF; queue<point> p; p.push(point(bx, by, 0)); f[bx][by][0] = 0; while (!p.empty()) { point q = p.front(); p.pop(); if (q.z >= k) continue; for (int i = 0; i < 4; i++) { int x = q.x + c[i][0]; int y = q.y + c[i][1]; if (x<1 || x>n || y<1 || y>m || a[x][y] < 0) continue; double ans = 1.0*abs(a[x][y] - a[q.x][q.y]) / (k - q.z); if (f[x][y][q.z + 1]>f[q.x][q.y][q.z] + ans) { f[x][y][q.z + 1] = f[q.x][q.y][q.z] + ans; p.push(point(x, y, q.z + 1)); } } } double ans = 0x7FFFFFFF; for (int i = k - 1; i >= 0; i--) ans = min(ans, f[ex][ey][i]); if (ans + 1e-5 < 0x7FFFFFFF) printf("%.2lf\n", ans); else printf("No Answer\n"); } int main() { scanf("%d", &T); while (T--) { scanf("%d%d%d", &n, &m, &k); for (int i = 1; i <= n; i++) { scanf("%s", s + 1); for (int j = 1; j <= m; j++) if (s[j] == '#') a[i][j] = -1; else a[i][j] = s[j] - '0'; } scanf("%d%d%d%d", &bx, &by, &ex, &ey); if (k) bfs(); else printf("No Answer\n"); } return 0; }