zoukankan      html  css  js  c++  java
  • Luogu P2864 [USACO06JAN]树林The Grove(bfs)

    P2864 [USACO06JAN]树林The Grove(bfs)

    题面

    题目描述

    The pasture contains a small, contiguous grove of trees that has no 'holes' in the middle of the it. Bessie wonders: how far is it to walk around that grove and get back to my starting position? She's just sure there is a way to do it by going from her start location to successive locations by walking horizontally, vertically, or diagonally and counting each move as a single step. Just looking at it, she doesn't think you could pass 'through' the grove on a tricky diagonal. Your job is to calculate the minimum number of steps she must take.

    Happily, Bessie lives on a simple world where the pasture is represented by a grid with (R) rows and (C) columns ( (1 leq R leq 50, 1 leq C leq 50) ). Here's a typical example where . is pasture (which Bessie may traverse), X is the grove of trees, * represents Bessie's start and end position, and + marks one shortest path she can walk to circumnavigate the grove (i.e., the answer):

    ...+...
    ..+X+..
    .+XXX+.
    ..+XXX+
    ..+X..+
    ...+++*
    

    The path shown is not the only possible shortest path; Bessie might have taken a diagonal step from her start position and achieved a similar length solution. Bessie is happy that she's starting 'outside' the grove instead of in a sort of 'harbor' that could complicate finding the best path.

    牧场里有一片树林,林子里没有坑.

    贝茜很想知道,最少需要多少步能围绕树林走一圈,最后回到起点.她能上下左右走,也能走对角线格子.

    牧场被分成 (R)(C) 列( (1 leq R leq 50,1 leq C leq 50) ).下面是一张样例的地图,其中表示贝茜 可以走的空地,“X”表示树林,表示起点.而贝茜走的最近的路已经特别地用“ + ”表示 出来.

    题目保证,最短的路径一定可以找到.

    输入输出格式

    输入格式:

    Line (1) : Two space-separated integers: (R) and (C)

    Lines (2..R+1) : Line (i+1) describes row (i) with (C) characters (with no spaces between them).

    输出格式:

    Line (1) : The single line contains a single integer which is the smallest number of steps required to circumnavigate the grove.

    输入输出样例

    输入样例:

    6 7
    .......
    ...X...
    ..XXX..
    ...XXX.
    ...X...
    ......*
    

    输出样例:

    13
    

    思路

    艹,这题的边界判断! (LSK) 你试试这道题。 --zbtrs

    换了三种写法,终于把神犇推荐的这道题 (AC) 了...

    我们直接拿样例做例子:

    .......
    ...X...
    ..XXX..
    ...XXX.
    ...X...
    ......*
    

    比如说最上面的那一颗树,我们在它上面做一条射线。

    .......
    ----
    ...X...
    ..XXX..
    ...XXX.
    ...X...
    ......*
    

    那么绕树林一周的路线一定要经过这条射线。

    我们可以用 (bfs) 更新所有点到出发点的最短距离,并且特判那条射线,要求不能跨过它:

    typedef pair<int,int> PII;//个人代码习惯
    #define mp(a,b) make_pair(a,b)//同上
    int a[8]={-1,-1,-1,+0,+0,+1,+1,+1};//x的变化
    int b[8]={-1,+0,+1,-1,+1,+1,+0,-1};//y的变化
    
    step[sx][sy]=1;
    queue<PII>Q;//队列
    Q.push(mp(sx,sy));//放入出发点
    while(!Q.empty())
    {
        int x=Q.front().first,y=Q.front().second;Q.pop();//取出队首点
        for(int i=0;i<8;i++)//枚举所有情况
        {
            int dx=x+a[i],dy=y+b[i];//能到达的点
            if(step[dx][dy]||!G[dx][dy]) continue;//不能到达的点和已经到过的点
            if(y<=ly&&x==lx&&dx==lx-1) continue;//不能从上往下穿过射线
            if(y<=ly&&x==lx-1&&dx==lx) continue;//不能从下往上穿过射线
            step[dx][dy]=step[x][y]+1;
            Q.push(mp(dx,dy));
        }
    }
    

    然后就顺利 (AC) 了。

    AC代码

    #include<bits/stdc++.h>
    using namespace std;
    typedef pair<int,int> PII;
    #define mp(a,b) make_pair(a,b)
    int n,m,sx,sy,lx,ly,ans=INT_MAX,G[55][55],step[55][55];
    bool is_line_made;
    int a[8]={-1,-1,-1,+0,+0,+1,+1,+1};
    int b[8]={-1,+0,+1,-1,+1,+1,+0,-1};
    int main()
    {
        cin>>n>>m;
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
            {
                char ch;
                cin>>ch;
                if(ch=='*') G[i][j]=1,sx=i,sy=j;
                else if(ch=='.') G[i][j]=1;
                else if(!lx) lx=i,ly=j;
            }
        step[sx][sy]=1;
        queue<PII>Q;
        Q.push(mp(sx,sy));
        while(!Q.empty())
        {
            int x=Q.front().first,y=Q.front().second;Q.pop();
            for(int i=0;i<8;i++)
            {
                int dx=x+a[i],dy=y+b[i];
                if(step[dx][dy]||!G[dx][dy]) continue;
                if(y<=ly&&x==lx&&dx==lx-1) continue;
                if(y<=ly&&x==lx-1&&dx==lx) continue;
                step[dx][dy]=step[x][y]+1;
                Q.push(mp(dx,dy));
            }
        }
        for(int i=1;i<=ly;i++)
        {
            if(step[lx][i]&&step[lx-1][i]&&ans>step[lx][i]+step[lx-1][i]) ans=step[lx][i]+step[lx-1][i];
            if(step[lx][i]&&step[lx-1][i+1]&&ans>step[lx][i]+step[lx-1][i+1]) ans=step[lx][i]+step[lx-1][i+1];
            if(step[lx][i]&&step[lx-1][i-1]&&ans>step[lx][i]+step[lx-1][i-1]) ans=step[lx][i]+step[lx-1][i-1];
        }
        printf("%d",ans-1);
        return 0;
    }
    
  • 相关阅读:
    MongoDB数据类型
    Redis数据类型
    RHEL7 CentOS7 检查查看精简指令
    Linux命令:查看登录用户
    JavaScript错误之:Uncaught ReferenceError: $ is not defined
    Linux下因为系统编码问题造成乱码的解决办法
    Linux系统下的程序开发之:命名规范
    优化php代码
    Git工具:Widows下的使用(提交到Github)
    MongoDB
  • 原文地址:https://www.cnblogs.com/coder-Uranus/p/9755083.html
Copyright © 2011-2022 走看看