zoukankan      html  css  js  c++  java
  • 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;
    

    }

  • 相关阅读:
    sqlserver更新数据表结构增加新字段时,和它相关联的视图出现错列现象
    form表单中的Input使用disabled不能提交的解决方法
    MVC4升级到MVC5的问题,安全透明方法“WebMatrix.WebData.PreApplicationStartCode.Start()”尝试访问安全关键方法“System.Web.WebPages.Razor.WebPageRazorHost.AddGlobalImport(System.String)”失败
    快速建立一个手机网站
    NoSQLMongoDB选择
    基于HTML5手机技术方案(如Phonegap)的调试方案
    最后的绝唱Symbian 最新的IDE开发环境搭建(Updated on 20110307)
    由NodeJS浅谈无敌的前端开发工程师
    物联网(1)手机NFC识别方案平台
    大话网站从Hello World到高并发网站
  • 原文地址:https://www.cnblogs.com/Sunshine-tcf/p/5816442.html
Copyright © 2011-2022 走看看