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###

[email protected]#T

..###@

0 1 3 0

4 6

1 2 2

T...TT

TTT###

[email protected]#T

..###@

0 1 3 0

2 2

5 1 3

[email protected]

@.

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\n",T++,0);
        }
        else
        {
            sum=bfs(begin,end);
            printf("Case %d: %d\n",T++,sum);
        }
    }
    return 0;
}
时间: 2024-10-10 11:00:03

CDZSC_2015寒假新人(4)——搜索 H的相关文章

CDZSC_2015寒假新人(4)——搜索 - P

Description  Parentheses Balance  You are given a string consisting of parentheses () and []. A string of this type is said to be correct: (a) if it is the empty string (b) if A and B are correct, AB is correct, (c) if A is correct, (A ) and [A ] is

CDZSC_2015寒假新人(4)——搜索 G

Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description Ignatius被魔王抓走了,有一天魔王出差去了,这可是Ignatius逃亡的好机会. 魔王住在一个城堡里,城堡是一个A*B*C的立方体,可以被表示成A个B*C的矩阵,刚开始Ignatius被关在(0,0,0)的位置,离开城堡的门在(A-1,B-1,C-1)的位置,现在知道魔王将在T分钟后回到城堡,Ignatius每分钟

CDZSC_2015寒假新人(4)——搜索 B

Description There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can't move on red tiles, he can move only

CDZSC_2015寒假新人(4)——搜索 - D

Description You're in space. You want to get home. There are asteroids. You don't want to hit them. Input Input to this problem will consist of a (non-empty) series of up to 100 data sets. Each data set will be formatted according to the following de

CDZSC_2015寒假新人(4)——搜索 A

Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Description Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and

CDZSC_2015寒假新人(2)——数学 - H

Description Sky从小喜欢奇特的东西,而且天生对数字特别敏感,一次偶然的机会,他发现了一个有趣的四位数2992,这个数,它的十进制数表示,其四位数字之和为2+9+9+2=22,它的十六进制数BB0,其四位数字之和也为22,同时它的十二进制数表示1894,其四位数字之和也为22,啊哈,真是巧啊.Sky非常喜欢这种四位数,由于他的发现,所以这里我们命名其为Sky数.但是要判断这样的数还是有点麻烦啊,那么现在请你帮忙来判断任何一个十进制的四位数,是不是Sky数吧. Input 输入含有一些

CDZSC_2015寒假新人(4)——搜索 L

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description A message from humans to extraterrestrial intelligence was sent through the Arecibo radio telescope in Puerto Rico on the afternoon of Saturday November 16, 1974

CDZSC_2015寒假新人(4)——搜索 N

Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Description Standard web browsers contain features to move backward and forward among the pages recently visited. One way to implement these features is to use two stacks to

CDZSC_2015寒假新人(4)——搜索 C

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description There is a strange lift.The lift can stop can at every floor as you want, and there is a number Ki(0 <= Ki <= N) on every floor.The lift have just two butto