zoukankan      html  css  js  c++  java
  • poj1383

    求树中最长路的方法:任选一结点为根,找最深结点。并以最深结点为根,找最深结点,其深度即为所求。

    本题两次bfs解决

    View Code
    #include <iostream>
    #include <cstdlib>
    #include <cstring>
    #include <cstdio>
    using namespace std;

    #define maxn 1005

    struct Point
    {
    int x, y;
    }q[maxn * maxn];

    int n, m;
    char map[maxn][maxn];
    int dist[maxn][maxn];
    int dir[4][2] = {{1, 0},{0, 1},{-1, 0},{0, -1}};

    void input()
    {
    scanf("%d%d", &m, &n);
    for (int i = 0; i < n; i++)
    scanf("%s", map[i]);
    }

    bool in(Point b)
    {
    return b.x < n && b.y < m && b.x >= 0 && b.y >= 0;
    }

    void bfs(int sx, int sy)
    {
    int front = 0;
    int rear = 0;
    Point a;
    a.x = sx;
    a.y = sy;
    q[rear++] = a;
    dist[a.x][a.y] = 0;
    while (front != rear)
    {
    a = q[front++];
    if (front == maxn * maxn)
    front = 0;
    for (int i= 0; i < 4; i++)
    {
    Point b = a;
    b.x += dir[i][0];
    b.y += dir[i][1];
    if (in(b) && map[b.x][b.y] == '.' && dist[b.x][b.y] == -1)
    {
    dist[b.x][b.y] = dist[a.x][a.y] + 1;
    q[rear++] = b;
    if (rear == maxn * maxn)
    rear = 0;
    }
    }
    }
    }

    void work()
    {
    memset(dist, -1, sizeof(dist));
    for (int i = 0; i < n; i++)
    {
    bool did = false;
    for (int j = 0; j < m; j++)
    if (map[i][j] == '.')
    {
    bfs(i, j);
    did = true;
    break;
    }
    if (did)
    break;
    }
    int temp = -1;
    int sx = 0, sy = 0;
    for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
    if (dist[i][j] > temp)
    {
    temp = dist[i][j];
    sx = i;
    sy = j;
    }
    memset(dist, -1, sizeof(dist));
    if (temp == -1)
    {
    printf("Maximum rope length is %d.\n", 0);
    return;
    }
    bfs(sx, sy);
    temp = -1;
    for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
    if (dist[i][j] > temp)
    temp = dist[i][j];
    printf("Maximum rope length is %d.\n", temp);
    }

    int main()
    {
    //freopen("t.txt", "r", stdin);
    int t;
    scanf("%d", &t);
    while (t--)
    {
    input();
    work();
    }
    return 0;
    }

  • 相关阅读:
    关于HDFS默认block块大小
    从计算框架MapReduce看Hadoop1.0和2.0的区别
    Linux的作业管理
    php 随机密码和盐 来自wordpress
    SSH管理(重启 停止 运行 安装)centos7
    卸载iptables 小心了!!怎么关闭和卸载iptables
    mysqld: Out of memory 解决办法(mysql)
    ie6 ie7 ie8 ie9兼容问题终极解决方案
    call_user_func()的参数不能为引用传递 自定义替代方法
    centos 域名硬解析(linux)
  • 原文地址:https://www.cnblogs.com/rainydays/p/2184426.html
Copyright © 2011-2022 走看看