• POJ 1383 Labyrinth (bfs 树的直径)


    Labyrinth

    题目链接:

    http://acm.hust.edu.cn/vjudge/contest/130510#problem/E

    Description

    The northern part of the Pyramid contains a very large and complicated labyrinth. The labyrinth is divided into square blocks, each of them either filled by rock, or free. There is also a little hook on the floor in the center of every free block. The ACM have found that two of the hooks must be connected by a rope that runs through the hooks in every block on the path between the connected ones. When the rope is fastened, a secret door opens. The problem is that we do not know which hooks to connect. That means also that the neccessary length of the rope is unknown. Your task is to determine the maximum length of the rope we could need for a given labyrinth.

    Input

    The input consists of T test cases. The number of them (T) is given on the first line of the input file. Each test case begins with a line containing two integers C and R (3 <= C,R <= 1000) indicating the number of columns and rows. Then exactly R lines follow, each containing C characters. These characters specify the labyrinth. Each of them is either a hash mark (#) or a period (.). Hash marks represent rocks, periods are free blocks. It is possible to walk between neighbouring blocks only, where neighbouring blocks are blocks sharing a common side. We cannot walk diagonally and we cannot step out of the labyrinth. The labyrinth is designed in such a way that there is exactly one path between any two free blocks. Consequently, if we find the proper hooks to connect, it is easy to find the right path connecting them.

    Output

    Your program must print exactly one line of output for each test case. The line must contain the sentence "Maximum rope length is X." where Xis the length of the longest path between any two free blocks, measured in blocks.

    Sample Input

    ``` 2 3 3 ### #.# ### 7 6 ####### #.#.### #.#.### #.#.#.# #.....# ####### ```

    Sample Output

    ``` Maximum rope length is 0. Maximum rope length is 8. ```

    Source

    2016-HUST-线下组队赛-5
    ##题意: 以邻接矩阵的形式给出树,求树的直径.
    ##题解: 两遍搜索求树的直径即可. 由于数据规模较大,这题很容易爆栈,所以得用bfs.
    ##代码: ``` cpp #include #include #include #include #include #include #include #include #include #include #define maxn 1010 #define inf 0x3f3f3f3f #define mod 1000000007 #define mid(a,b) ((a+b)>>1) #define IN freopen(".in","r",stdin); using namespace std;

    int n,m;
    char mp[maxn][maxn];

    bool is_ok(int x, int y) {
    return x>=0 && y>=0 && x<n && y<m;
    }
    int dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};

    int ans,px,py;
    bool vis[maxn][maxn];

    //void dfs(int x, int y, int step) {
    // vis[x][y] = 1;
    // bool flag = 0;
    // for(int i=0; i<4; i++) {
    // int xx = x + dir[i][0];
    // int yy = y + dir[i][1];
    // if(!is_ok(xx,yy) || mp[xx][yy]=='#' || vis[xx][yy]) continue;
    // dfs(xx, yy, step+1);
    // flag = 1;
    // }
    //
    // if(!flag && step > ans) {
    // ans = step;
    // px = x, py = y;
    // }
    //}

    struct node {
    int x,y,step;
    };
    void bfs() {
    queue q; while(!q.empty()) q.pop();
    node cur,next;
    memset(vis, 0, sizeof(vis));
    cur = {px,py,0}; vis[px][py] = 1;
    q.push(cur);
    while(!q.empty()) {
    bool flag = 0;
    cur = q.front(); q.pop();
    for(int i=0; i<4; i++) {
    int xx = cur.x + dir[i][0];
    int yy = cur.y + dir[i][1];
    if(!is_ok(xx,yy) || mp[xx][yy]=='#' ||vis[xx][yy]) continue;
    vis[xx][yy] = 1;
    next = {xx,yy,cur.step+1};
    q.push(next);
    flag = 1;
    }
    if(!flag && cur.step > ans) {
    ans = cur.step;
    px = cur.x; py = cur.y;
    }
    }
    }

    int main()
    {
    IN;

    int T;
    cin >> T;
    
    while (T--){
    
        scanf("%d %d", &m, &n); getchar();
        for (int i = 0; i < n; i++) {
            gets(mp[i]);
        }
    
        px = py = -1;
        for (int i = 0; i < n; i++) {
            for(int j=0; j < m; j++) {
                if(mp[i][j] == '.') {
                    px = i, py = j;
                    break;
                }
            }
            if(px != -1) break;
        }
    
        ans = 0;
        bfs();
        ans = 0;
        bfs();
    
        printf("Maximum rope length is %d.
    ", ans);
    }
    
    return 0;
    

    }

  • 相关阅读:
    extjs store 设置额外参数刷新数据
    springsecurity oauth2sso 客户端单点登陆
    extjs store定义 通过ajax访问json数据
    maven deploy异常
    spring security authenticated与fullyAuthenticated的区别
    extjs 部署时动态切换上下文路径
    npm install异常error code EPERM
    包子问题 (背包,找规律?)
    结账问题 (一点点的数学知识)(蓝桥杯)
    倍数问题 (倍数》余数 // 思路的转换问题)(蓝桥杯)
  • 原文地址:https://www.cnblogs.com/Sunshine-tcf/p/5816442.html
Copyright © 2020-2023  润新知