zoukankan      html  css  js  c++  java
  • poj 3182 The Grove

    The Grove
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 641   Accepted: 297

    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

    Source


    思路:

    突破口肯定是必须要围绕果园走一圈了,那么起点到一个点分顺时针和逆时针经过果园用bfs求两次最短路就够了,比赛时想了很久都没想到处理顺时针和逆时针的方法,就只能想到这步了,思维还是不能突破呀。

    怎样处理顺时针和逆时针呢?从果园中引一条射线出去与地图边界相交,从起点出发两次bfs,一次控制只能向上经过射线,一次控制只能向下进过射线就够了。因为绕果园必须要经过射线上一点,所以最后用射线上的点来更新答案就OK了。


    注意:

    那个射线不能随便作的,要保证射线不与果园再次相交。想一想,为什么?


    代码:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <string>
    #include <map>
    #include <stack>
    #include <vector>
    #include <set>
    #include <queue>
    //#pragma comment (linker,"/STACK:102400000,102400000")
    #define maxn 105
    #define mod 1000000007
    #define INF 0x3f3f3f3f
    using namespace std;
    
    typedef long long ll;
    int n,m,ans;
    int sx,sy;
    int dx[]={-1,1,0,0,-1,-1,1,1};
    int dy[]={0,0,-1,1,-1,1,-1,1};
    int dist[maxn][maxn][2];
    bool vis[maxn][maxn][2];
    char mp[maxn][maxn];
    char s[maxn];
    struct Node
    {
        int x,y;
    }cur,now;
    queue<Node>q;
    
    void presolve()
    {
        int i,j,k,flag=0;
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=m;j++)
            {
                if(mp[i][j]=='X')
                {
                    flag=1;
                    for(k=j-1;k>=1;k--)  // 将射线标记
                    {
                        if(mp[i][k]=='X') continue ;
                        mp[i][k]='Y';
                    }
                    break ;
                }
            }
            if(flag) break ;
        }
    }
    void bfs(int k)
    {
        int i,j,nx,ny,tx,ty;
        while(!q.empty()) q.pop();
        cur.x=sx;
        cur.y=sy;
        dist[sx][sy][k]=0;
        vis[sx][sy][k]=1;
        q.push(cur);
        while(!q.empty())
        {
            now=q.front();
            q.pop();
            nx=now.x;
            ny=now.y;
            for(i=0;i<8;i++)
            {
                tx=nx+dx[i];
                ty=ny+dy[i];
                if(tx<1||tx>n||ty<1||ty>m||mp[tx][ty]=='X'||vis[tx][ty][k]) continue ;
                if(k&&mp[tx][ty]=='Y')           // 到Y时控制过去的方向就好了
                {
                    if(i==1||i==6||i==7) continue ;
                }
                else if(!k&&mp[tx][ty]=='Y')     // 到Y时控制过去的方向就好了
                {
                    if(i==0||i==4||i==5) continue ;
                }
                cur.x=tx;
                cur.y=ty;
                dist[tx][ty][k]=dist[nx][ny][k]+1;
                vis[tx][ty][k]=1;
                q.push(cur);
            }
        }
    }
    void solve()
    {
        int i,j;
        ans=INF;
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=m;j++)
            {
                if(mp[i][j]=='Y')
                {
                    ans=min(ans,dist[i][j][0]+dist[i][j][1]);
                }
            }
        }
    }
    int main()
    {
        int i,j,t;
        while(~scanf("%d%d",&n,&m))
        {
            for(i=1;i<=n;i++)
            {
                scanf("%s",s);
                for(j=1;j<=m;j++)
                {
                    mp[i][j]=s[j-1];
                    if(mp[i][j]=='*') sx=i,sy=j;
                }
            }
            presolve();
            memset(vis,0,sizeof(vis));
            bfs(0);
            bfs(1);
            solve();
            printf("%d
    ",ans);
        }
        return 0;
    }



  • 相关阅读:
    看懂SqlServer查询计划
    Android开发16——获取网络资源之基础应用
    Android开发15——给TextView加上滚动条
    PeekMessage、GetMessage的区别
    获取不到Repeater控件中的CheckBox选中状态
    第十九讲:动态链接库
    孙鑫VC++视频教程笔记
    CEdit 控制键盘操作
    网络编程中粘包的处理方法
    VC++编程之道读书笔记(2)
  • 原文地址:https://www.cnblogs.com/suncoolcat/p/3347879.html
Copyright © 2011-2022 走看看