题目链接 P4162 [SCOI2009]最长距离
注意,‘0’号格子是可以走的,‘1’是用来隔断的。
然后,要确定最长距离,我们可以考虑成一个点到另一个点的最少需要搬掉几个障碍物才可以,那么问题可以转变成最短路问题,然后在一个个的去查询即可。
时间复杂度
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <unordered_map>
#include <unordered_set>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define eps 1e-8
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
#define MP(a, b) make_pair(a, b)
using namespace std;
typedef unsigned long long ull;
typedef unsigned int uit;
typedef long long ll;
const int maxN = 33;
const int dir[4][2] =
{
-1, 0,
0, -1,
0, 1,
1, 0
};
int N, M, K;
char mp[maxN][maxN];
inline int _ID(int x, int y) { return M * x + y + 1; }
inline bool In_Map(int x, int y) { return x >=0 && y >= 0 && x < N && y < M; }
int dis[maxN][maxN];
bool inque[maxN][maxN];
queue<pair<int, int>> Q;
inline void spfa(int x, int y)
{
while(!Q.empty()) Q.pop();
for(int i=0; i<N; i++) for(int j=0; j<M; j++) { dis[i][j] = INF; inque[i][j] = false; }
Q.push(MP(x, y)); inque[x][y] = true; dis[x][y] = mp[x][y] == '1';
pair<int, int> now; int xx, yy;
while(!Q.empty())
{
now = Q.front(); Q.pop(); x = now.first; y = now.second; inque[x][y] = false;
for(int i=0; i<4; i++)
{
xx = x + dir[i][0]; yy = y + dir[i][1];
if(!In_Map(xx, yy)) continue;
if(dis[xx][yy] > dis[x][y] + (mp[xx][yy] == '1'))
{
dis[xx][yy] = dis[x][y] + (mp[xx][yy] == '1');
if(!inque[xx][yy])
{
inque[xx][yy] = true;
Q.push(MP(xx, yy));
}
}
}
}
}
int main()
{
scanf("%d%d%d", &N, &M, &K);
for(int i=0; i<N; i++) scanf("%s", mp[i]);
double ans = 0.;
for(int x=0; x<N; x++)
{
for(int y=0; y<M; y++)
{
spfa(x, y);
for(int i=0; i<N; i++)
{
for(int j=0; j<M; j++)
{
if(dis[i][j] <= K) ans = max(ans, sqrt((x - i) * (x - i) + (y - j) * (y - j)));
}
}
}
}
printf("%.6lf\n", ans);
return 0;
}