题目大意:
给出一个
R
∗
C
R*C
R∗C的图,给出起点与终点,已知左右方向移动的时间为1,上下移动的时间为未知实数v。求从起点到终点的最短移动时间为已知实数L时,未知实数v是多少。
1
≤
R
,
C
≤
100
,
0
≤
v
<
10
1≤ R,C ≤ 100,0≤ v <10
1≤R,C≤100,0≤v<10
分析:
n , m ≤ 100 n,m≤100 n,m≤100,直接 b f s bfs bfs求出一条可行路径然后求出 v v v即可。
代码:
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
#define eps 1e-9
#define N 105
using namespace std;
const int dx[4] = {0, 0, 1, -1};
const int dy[4] = {1, -1, 0, 0};
bool vis[N][N];
char a[N][N];
int T, n, m;
double len;
bool Check(int px, int py)
{
if (px < 1 || px > n || py < 1 || py > m) return 0;
if (vis[px][py] || a[px][py] == '#') return 0;
return 1;
}
queue <int> Qx, Qy, Qnum1, Qnum2;
void bfs(int px, int py, int qx, int qy)
{
double ans = 10;
memset(vis, 0, sizeof(vis));
if (px == qx && py == qy) { printf("0\n"); return; }
while (Qx.size()) Qx.pop();
while (Qy.size()) Qy.pop();
while (Qnum1.size()) Qnum1.pop();
while (Qnum2.size()) Qnum2.pop();
Qx.push(px), Qy.push(py);
Qnum1.push(0), Qnum2.push(0);
vis[px][py] = 1;
while (Qx.size())
{
int ax = Qx.front(), ay = Qy.front(); Qx.pop(); Qy.pop();
int a1num = Qnum1.front(), a2num = Qnum2.front(); Qnum1.pop(); Qnum2.pop();
for (int i = 0; i < 4; i++)
{
int bx = ax + dx[i], by = ay + dy[i];
if (!Check(bx, by)) continue;
vis[bx][by] = 1;
if (i >= 0 && i <= 1)
{
if (bx != qx || by != qy && a1num + 1 <= len) Qx.push(bx), Qy.push(by), Qnum1.push(a1num + 1), Qnum2.push(a2num);
if (bx == qx && by == qy) { if (a2num == 0) printf("0\n"); else printf("%.5lf\n", (double)(len - a1num - 1) / a2num); return; }
}
if (i >= 2 && i <= 3)
{
if (bx != qx || by != qy && a1num <= len) Qx.push(bx), Qy.push(by), Qnum1.push(a1num), Qnum2.push(a2num + 1);
if (bx == qx && by == qy) { printf("%.5lf\n",(double)(len - a1num) / (a2num + 1)); return; }
}
}
}
}
int main()
{
freopen("maze.in", "r", stdin);
freopen("maze.out", "w", stdout);
scanf("%d\n", &T);
while (T--)
{
scanf("%lf %d %d\n", &len, &n, &m);
int px, py, qx, qy;
for (int i = 1; i <= n; i++)
{
gets(a[i] + 1);
for (int j = 1; j <= m; j++)
{
if (a[i][j] == 'S') px = i, py = j;
if (a[i][j] == 'E') qx = i, qy = j;
}
}
bfs(px, py, qx, qy);
}
return 0;
}