zoukankan      html  css  js  c++  java
  • BZOJ 1295 最长距离 BFS+枚举

    题目链接:

    https://www.lydsy.com/JudgeOnline/problem.php?id=1295

    题目大意:

    windy有一块矩形土地,被分为 N*M 块 1*1 的小格子。 有的格子含有障碍物。 如果从格子A可以走到格子B,那么两个格子的距离就为两个格子中心的欧几里德距离。 如果从格子A不可以走到格子B,就没有距离。 如果格子X和格子Y有公共边,并且X和Y均不含有障碍物,就可以从X走到Y。 如果windy可以移走T块障碍物,求所有格子间的最大距离。 保证移走T块障碍物以后,至少有一个格子不含有障碍物。

    思路:

    枚举两点,计算出至少需要移开多少块障碍物,用BFS就可以。

    注意,如果每次都进行BFS,那么将要进行(n*m)^2次BFS,会超时。

    所以可以枚举起点进行BFS,每次BFS处理出到达其余点需要移开的障碍物即可。这样只需要进行(n*m)次BFS

     1 #include<bits/stdc++.h>
     2 #define IOS ios::sync_with_stdio(false);//不可再使用scanf printf
     3 #define Max(a, b) ((a) > (b) ? (a) : (b))//禁用于函数,会超时
     4 #define Min(a, b) ((a) < (b) ? (a) : (b))
     5 #define Mem(a) memset(a, 0, sizeof(a))
     6 #define Dis(x, y, x1, y1) ((x - x1) * (x - x1) + (y - y1) * (y - y1))
     7 #define MID(l, r) ((l) + ((r) - (l)) / 2)
     8 #define lson ((o)<<1)
     9 #define rson ((o)<<1|1)
    10 #define Accepted 0
    11 #pragma comment(linker, "/STACK:102400000,102400000")//栈外挂
    12 using namespace std;
    13 inline int read()
    14 {
    15     int x=0,f=1;char ch=getchar();
    16     while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
    17     while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    18     return x*f;
    19 }
    20 
    21 typedef long long ll;
    22 const int maxn = 100 + 10;
    23 const int MOD = 1000000007;//const引用更快,宏定义也更快
    24 const int INF = 1e9 + 7;
    25 const double eps = 1e-6;
    26 double f(int a, int b, int x, int y)
    27 {
    28     double ans = 1.0 * (a - x) * (a - x) + 1.0 * (b - y) * (b - y);
    29     return sqrt(ans);
    30 }
    31 int dir[4][2] = {1,0,0,1,-1,0,0,-1};
    32 char Map[35][35];
    33 bool vis[35][35];
    34 
    35 int n, m, t;
    36 struct node
    37 {
    38     int x, y;
    39     int dis;
    40     node(){}
    41     node(int x, int y, int dis):x(x), y(y), dis(dis){}
    42     bool operator < (const node& a)const
    43     {
    44         return dis > a.dis;
    45     }
    46 };
    47 int dis[35][35];
    48 int BFS(int a, int b)
    49 {
    50     memset(vis, 0, sizeof(vis));
    51     priority_queue<node>q;
    52     q.push(node(a, b, Map[a][b] - '0'));
    53     vis[a][b] = 1;
    54     while(!q.empty())
    55     {
    56         node now = q.top();
    57         q.pop();
    58         dis[now.x][now.y] = now.dis;
    59         for(int i = 0; i < 4; i++)
    60         {
    61             int x = now.x + dir[i][0];
    62             int y = now.y + dir[i][1];
    63             if(x >= 0 && x < n && y >= 0 && y < m && !vis[x][y])
    64             {
    65                 vis[x][y] = 1;
    66                 if(Map[x][y] == '0')q.push(node(x, y, now.dis));
    67                 else q.push(node(x, y, now.dis + 1));
    68             }
    69         }
    70     }
    71 }
    72 int main()
    73 {
    74     scanf("%d%d%d", &n, &m, &t);
    75     for(int i = 0; i < n; i++)scanf("%s", &Map[i]);
    76     double ans = 0;
    77     for(int sx = 0; sx < n; sx++)
    78         for(int sy = 0; sy < m; sy++)
    79     {
    80         BFS(sx, sy);//从每个点出发预处理一下dis
    81         for(int tx = 0; tx < n; tx++)
    82             for(int ty = 0; ty < m; ty++)
    83         {
    84             if(sx == tx && sy == ty)continue;
    85             if(dis[tx][ty] <= t)
    86             {
    87                 ans = max(ans, f(sx, sy, tx, ty));
    88             }
    89         }
    90     }
    91     printf("%.6f
    ", ans);
    92     return Accepted;
    93 }
  • 相关阅读:
    poj 3321 Apple Tree
    hdu 1520 Anniversary party
    Light OJ 1089 Points in Segments (II)
    Timus 1018 Binary Apple Tree
    zoj 3299 Fall the Brick
    HFUT 1287 法默尔的农场
    Codeforces 159C String Manipulation 1.0
    GraphQL + React Apollo + React Hook 大型项目实战(32 个视频)
    使用 TypeScript & mocha & chai 写测试代码实战(17 个视频)
    GraphQL + React Apollo + React Hook + Express + Mongodb 大型前后端分离项目实战之后端(19 个视频)
  • 原文地址:https://www.cnblogs.com/fzl194/p/9683686.html
Copyright © 2011-2022 走看看