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;
    

    }

  • 相关阅读:
    英黑客侵入Zynga系统窃得价值1200万美元筹码 狼人:
    瑞星发布2010企业安全报告 九成国内企业曾被入侵 狼人:
    赛门铁克大中国区总裁吴锡源:云安全是一项系统工程 狼人:
    频遭攻击 索尼无奈关闭多国网站 狼人:
    Pwn2Own黑客大赛首日:Safari、IE8被攻破 狼人:
    疯子的研究:瘫痪整个互联网绝非天方夜谭 狼人:
    Google收购安全分析软件厂商Zynamics 狼人:
    RSA大会:黑客正在觊觎个人用户而非网络 狼人:
    从索尼泄密看云计算安全 狼人:
    RSA公布被攻击内幕:钓鱼邮件惹祸 狼人:
  • 原文地址:https://www.cnblogs.com/Sunshine-tcf/p/5816442.html
Copyright © 2011-2022 走看看