• CDZSC_2015寒假新人(4)——搜索 H


    Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

    Description

    Hiking in the mountains is seldom an easy task for most people, as it is extremely easy to get lost during the trip. Recently Green has decided to go on a hiking trip. Unfortunately, half way through the trip, he gets extremely tired and so needs to find the path that will bring him to the destination with the least amount of time. Can you help him? 
    You've obtained the area Green's in as an R * C map. Each grid in the map can be one of the four types: tree, sand, path, and stone. All grids not containing stone are passable, and each time, when Green enters a grid of type X (where X can be tree, sand or path), he will spend time T(X). Furthermore, each time Green can only move up, down, left, or right, provided that the adjacent grid in that direction exists. 
    Given Green's current position and his destination, please determine the best path for him. 
     

    Input

    There are multiple test cases in the input file. Each test case starts with two integers R, C (2 <= R <= 20, 2 <= C <= 20), the number of rows / columns describing the area. The next line contains three integers, V P, V S, V T (1 <= V P <= 100, 1 <= V S <= 100, 1 <= V T <= 100), denoting the amount of time it requires to walk through the three types of area (path, sand, or tree). The following R lines describe the area. Each of the R lines contains exactly C characters, each character being one of the following: ‘T’, ‘.’, ‘#’, ‘@’, corresponding to grids of type tree, sand, path and stone. The final line contains four integers, S R, S C, T R, T C, (0 <= S R < R, 0 <= S C < C, 0 <= T R < R, 0 <= T C < C), representing your current position and your destination. It is guaranteed that Green's current position is reachable � that is to say, it won't be a '@' square. 
    There is a blank line after each test case. Input ends with End-of-File. 
     

    Output

    For each test case, output one integer on one separate line, representing the minimum amount of time needed to complete the trip. If there is no way for Green to reach the destination, output -1 instead. 
     

    Sample Input

    4 6
    1 2 10
    T...TT
    TTT###
    TT.@#T
    ..###@
    0 1 3 0
    4 6
    1 2 2
    T...TT
    TTT###
    TT.@#T
    ..###@
    0 1 3 0
    2 2
    5 1 3
    T@
    @.
    0 0 1 1
     

    Sample Output

    Case 1: 14
    Case 2: 8
    Case 3: -1
     
     
     
     
     
    思路:bfs通过时间来找最优解,在VIS标记上处理,如果未被标记和时间比原来标记的少就标记
     
     
     
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <queue>
    using namespace std;
    int arr[25][25];
    int vis[25][25];
    int v[4],r,c;
    int xx[]={-1,1,0,0};
    int yy[]={0,0,-1,1};
    struct node
    {
        int x;
        int y;
        int time;
    };
    int bfs(node a,node b)
    {
        memset(vis,0,sizeof(vis));
        queue<node>q;
        node cmp,tmp;
        a.time=0;
        q.push(a);
        while(!q.empty())
        {
            cmp=q.front();
            q.pop();
            for(int i=0; i<4; i++)
            {
                tmp.x=cmp.x+xx[i];
                tmp.y=cmp.y+yy[i];
                if(tmp.x>=0&&tmp.x<r&&tmp.y>=0&&tmp.y<c&&arr[tmp.x][tmp.y]!=0)
                {
                    if(vis[tmp.x][tmp.y]==0||cmp.time+v[arr[tmp.x][tmp.y]]<vis[tmp.x][tmp.y])
                    {
                        tmp.time=cmp.time+v[arr[tmp.x][tmp.y]];
                        vis[tmp.x][tmp.y]=tmp.time;
                        q.push(tmp);
                    }
                }
            }
        }
        if(vis[b.x][b.y]!=0)
        {
            return vis[b.x][b.y];
        }
        return -1;
    }
    
    int main()
    {
    #ifdef CDZSC_OFFLINE
        freopen("in.txt","r",stdin);
    #endif
        int i,j,len,T=1,sum;
        char str[25];
        node begin,end;
        while(scanf("%d%d",&r,&c)!=EOF)
        {
            scanf("%d%d%d",&v[1],&v[2],&v[3]);
            for(i=0; i<r; i++)
            {
                scanf("%s",str);
                len=strlen(str);
                for(j=0; j<len; j++)
                {
                    if(str[j]=='#')
                    {
                        arr[i][j]=1;
                    }
                    if(str[j]=='.')
                    {
                        arr[i][j]=2;
                    }
                    if(str[j]=='T')
                    {
                        arr[i][j]=3;
                    }
                    if(str[j]=='@')
                    {
                        arr[i][j]=0;
                    }
                }
            }
            scanf("%d%d%d%d",&begin.x,&begin.y,&end.x,&end.y);
            if(begin.x==end.x&&begin.y==end.y)
            {
                printf("Case %d: %d
    ",T++,0);
            }
            else
            {
                sum=bfs(begin,end);
                printf("Case %d: %d
    ",T++,sum);
            }
        }
        return 0;
    }
     
  • 相关阅读:
    列表框JList 及动作监听
    WindowListener(附带适配器改良方案)
    常用的几种文本组件(JTextComponent)
    JToggleButton组件
    java实现鼠标拖拽动作监听
    Flex读文本文件
    WPF socket通讯 UDP接收消息
    在线签名,并保存到本地
    flex直接访问服务器
    windows 8各种流之间的转换
  • 原文地址:https://www.cnblogs.com/Wing0624/p/4256243.html
Copyright © 2020-2023  润新知