The Grove bfs+射线思想

The Grove
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 944 Accepted: 465
Description

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 <= R <= 50, 1 <= C <= 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.
Input

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).
Output

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

6 7
.......
...X...
..XXX..
...XXX.
...X...
......*
Sample Output

13

思路:

思路是参考一位大神的,当时找了很多题解,别人都是一百多行的代码,他的只有五十行,研究了一下,方法很简单

选择一棵树做水平射线,从起点到射线下端的距离加上起点到射线上端的距离再加1就是环绕距离(选择最短的一条)

随便找棵树然后画一条射线作为分界线,使之不能通过,然后BFS就行啦

以样例为例:

在第二行唯一一棵树那划条射线,然后BFS结果如下:(-1为树木,0为起点)

最短是7+5+1=13;

在这个数据里面并不明显,划过线的部分是不能通过的……

再看这个数据:

 

结果如下:最短是1+10+1=12;

#include<cstdio>
#include<cmath>
#include<algorithm>
#include<queue>
#include<cstring>
using namespace std;

struct node
{
    int x,y;
}a;

int r,c,x,y,map[51][51];
int dr[8][2]={{0,1},{0,-1},{-1,1},{-1,-1},{-1,0},{1,0},{1,1},{1,-1}};

void bfs()
{
    queue<node>q;
    q.push(a);
    node next;
    while(!q.empty())
    {
        a=q.front();q.pop();
        for(int i=0;i<8;i++)
        {
            next.x=a.x+dr[i][0];
            next.y=a.y+dr[i][1];
            if(a.x==x&&a.y<y&&next.x==x-1) continue;
            if(a.x==(x-1)&&a.y<=y&&next.x==x) continue;
            if(next.x<0||next.x>=r||next.y<0||next.y>=c) continue;
            if(map[next.x][next.y]>map[a.x][a.y]+1)
            {
                map[next.x][next.y]=map[a.x][a.y]+1;
                q.push(next);
            }
        }
    }
}

int main()
{
    while(~scanf("%d%d",&r,&c))
    {
        getchar();
        memset(map,0,sizeof(map));
        char ch;x=-1;
        for(int i=0;i<r;i++)
        {
            for(int j=0;j<c;j++)
            {
                scanf("%c",&ch);
                if(ch=='X')
                {
                    map[i][j]=-1;
                    if(x==-1) {x=i;y=j;}
                }
                else if(ch=='*') {a.x=i;a.y=j;}
                else map[i][j]=2500;
            }
            getchar();
        }
        bfs();
        int ans=2500;
        for(int j=y-1;j>=0;j--)
        {
            ans=min(ans,map[x][j]+map[x-1][j]);
            ans=min(ans,map[x][j]+map[x-1][j+1]);
            if(j) ans=min(ans,map[x][j]+map[x-1][j-1]);
        }
        printf("%d\n",ans+1);
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值