题目意思
给你个01地图大小n * m,你只能走最大T个1的地方。
问你能最远走多远?起点随便选终点距离最远即可。地图规模最大30 * 30
解题思路
地图规模比较小,可以随便写复杂度,那么我们直接使用最暴力的算法,枚举每一个位置成为起点,对每一个位置算一次DFS,把去到每一个点的最短1的数量算出来,当然稍微减枝一下,如果超过了T个1直接保持无穷大不需要递归,还一个就是最优性减枝,第二次来到这个地方花费比之前还多,那也没必要走下去了。这样算出每一个地方需要踩过的1的数量,在枚举全部的终点去判断起点终点距离求MAX即可。
#pragma GCC target("avx,sse2,sse3,sse4,popcnt") #pragma GCC optimize("O2,O3,Ofast,inline,unroll-all-loops,-ffast-math") #include <bits/stdc++.h> using namespace std; #define js ios::sync_with_stdio(false);cin.tie(0); cout.tie(0) #define all(__vv__) (__vv__).begin(), (__vv__).end() #define endl "\n" #define pai pair<int, int> #define ms(__x__,__val__) memset(__x__, __val__, sizeof(__x__)) typedef long long ll; typedef unsigned long long ull; typedef long double ld; inline ll read() { ll s = 0, w = 1; char ch = getchar(); for (; !isdigit(ch); ch = getchar()) if (ch == '-') w = -1; for (; isdigit(ch); ch = getchar()) s = (s << 1) + (s << 3) + (ch ^ 48); return s * w; } inline void print(ll x, int op = 10) { if (!x) { putchar('0'); if (op) putchar(op); return; } char F[40]; ll tmp = x > 0 ? x : -x; if (x < 0)putchar('-'); int cnt = 0; while (tmp > 0) { F[cnt++] = tmp % 10 + '0'; tmp /= 10; } while (cnt > 0)putchar(F[--cnt]); if (op) putchar(op); } inline ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; } ll qpow(ll a, ll b) { ll ans = 1; while (b) { if (b & 1) ans *= a; b >>= 1; a *= a; } return ans; } ll qpow(ll a, ll b, ll mod) { ll ans = 1; while (b) { if (b & 1)(ans *= a) %= mod; b >>= 1; (a *= a) %= mod; }return ans % mod; } inline int lowbit(int x) { return x & (-x); } const int dir[][2] = { {0,1},{1,0},{0,-1},{-1,0},{1,1},{1,-1},{-1,1},{-1,-1} }; const int MOD = 1e9 + 7; const int INF = 0x3f3f3f3f; const int N = 32; int a[N][N], cnt[N][N]; int n, m, t; void dfs(int x, int y, int step) { if (step > t) return; if (step >= cnt[x][y]) return; cnt[x][y] = step; for (int i = 0; i < 4; ++i) { int xx = x + dir[i][0], yy = y + dir[i][1]; if (xx <= 0 or xx > n or yy <= 0 or yy > m) continue; dfs(xx, yy, step + a[xx][yy]); } } int main() { scanf("%d %d %d", &n, &m, &t); for (int i = 1; i <= n; ++i) { char s[N]; scanf("%s", s + 1); for (int j = 1; j <= m; ++j) a[i][j] = s[j] - '0'; } int ans = 0; for (int i = 1; i <= n; ++i) for (int j = 1; j <= m; ++j) { memset(cnt, 0x3f, sizeof cnt); dfs(i, j, a[i][j]); for (int ii = 1; ii <= n; ++ii) for (int jj = 1; jj <= m; ++jj) if (cnt[ii][jj] <= t) ans = max(ans, (ii - i) * (ii - i) + (jj - j) * (jj - j)); } printf("%.6f\n", sqrt(ans)); return 0; }