Hiking Trip(bfs+优先队列)

Hiking Trip

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1444    Accepted Submission(s): 628


Problem 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
 

Source

这一题是bfs+优先队列! 

bfs保证最后搜索到母的地,优先队列保证每次搜索的时间尽

可能的短!

#include<cstdio>
#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
char map[25][25];
int vis[25][25];
int dir[4][2]={0,1,0,-1,1,0,-1,0};
int n,m,t1,t2,t3,sx,sy,ex,ey;
struct node
{
    int x;
    int y;
    int t;
};
struct cmp
{
    bool operator()(node a,node b)
    {
        return a.t>b.t;//对头到队尾时间从小到大!
    }
};
int judge(int x,int y)
{
    if(x>=0&&x<n&&y>=0&&y<m&&!vis[x][y]&&map[x][y]!='@')
    {
        return 1;
    }
    return 0;
}
int bfs(int sx,int sy)
{
    priority_queue<node,vector<node>,cmp>q;
    int i,x1,y1;
    node cur,next;
    cur.x=sx;
    cur.y=sy;
    cur.t=0;
    q.push(cur);
    vis[sx][sy]=1;
    while(!q.empty())
    {
        cur=q.top();
        q.pop();
        if(cur.x==ex&&cur.y==ey)
        {
            return cur.t;
        }
        for(i=0;i<4;i++)
        {
            x1=cur.x+dir[i][0];
            y1=cur.y+dir[i][1];
            if(judge(x1,y1))
            {
                vis[x1][y1]=1;
                next.x=x1;
                next.y=y1;
                if(map[x1][y1]=='T')
                {
                    next.t=cur.t+t3;
                }
                else if(map[x1][y1]=='#')
                {
                    next.t=cur.t+t1;
                }
                else if(map[x1][y1]=='.')
                {
                    next.t=cur.t+t2;
                }
                q.push(next);
            }
            //把这个点相关都可以走的点压进去,然后消除这个点!
            //为什么按照时间最小点压出去呢?
            //首先bfs要求获得最小的时间就应该从当前时间的最小点开始搜索!
        }
    }
    return -1;

}
int main()
{
    int i,j,num=1;
    while(cin>>n>>m)
    {
        memset(map,0,sizeof(map));
        memset(vis,0,sizeof(vis));
        cin>>t1>>t2>>t3;
        for(i=0;i<n;i++)
        {
            cin>>map[i];
        }
       // cout<<"ok"<<endl;
       cin>>sx>>sy>>ex>>ey;
       // cout<<"ok"<<endl;
       /* for(i=0;i<n;i++)
        {
            cout<<map[i]<<endl;
        }*/
       printf("Case %d: %d\n",num++,bfs(sx,sy));
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
网页标题:My Portfolio HTML代码: ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>My Portfolio</title> <link rel="stylesheet" href="style.css"> </head> <body> <header> <h1>My Portfolio</h1> <nav> <ul> <li><a href="#">Home</a></li> <li><a href="#">About</a></li> <li><a href="#">Projects</a></li> <li><a href="#">Contact</a></li> </ul> </nav> </header> <main> <section> <h2>About Me</h2> <img src="profile.jpg" alt="My Profile Picture"> <p>Hi, I'm John Doe. I'm a web developer with 5 years of experience. I'm passionate about creating beautiful and functional websites that help businesses grow. In my free time, I enjoy hiking and playing guitar.</p> </section> <section> <h2>My Projects</h2> <ul> <li> <h3>Project 1</h3> <img src="project1.jpg" alt="Project 1"> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed vitae lobortis velit. Fusce nec risus at tellus sagittis vehicula.</p> </li> <li> <h3>Project 2</h3> <img src="project2.jpg" alt="Project 2"> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed vitae lobortis velit. Fusce nec risus at tellus sagittis vehicula.</p> </li> <li> <h3>Project 3</h3> <img src="project3.jpg" alt="Project 3"> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed vitae lobortis velit. Fusce nec risus at tellus sagittis vehicula.</p> </li> </ul> </section> <section> <h2>Contact Me</h2> <form> <label for="name">Name:</label> <input type="text" id="name" name="name"> <label for="email">Email:</label> <input type="email" id="email" name="email"> <label for="message">Message:</label> <textarea id="message" name="message"></textarea> <input type="submit" value="Send"> </form> </section> </main> <footer> <p>© 2021 My Portfolio</p> </footer> </body> </html> ``` CSS代码: ```css body { font-family: Arial, sans-serif; margin: 0; padding: 0; } header { background-color: #333; color: #fff; display: flex; justify-content: space-between; align-items: center; padding: 1rem; } nav ul { list-style: none; margin: 0; padding: 0; display: flex; } nav li { margin-left: 1rem; } nav a { color: #fff; text-decoration: none; font-weight: bold; } main { max-width: 800px; margin: 0 auto; padding: 2rem; } section { margin-bottom: 2rem; } h2 { margin-bottom: 1rem; } img { max-width: 100%; height: auto; margin-bottom: 1rem; } ul { list-style: none; margin: 0; padding: 0; display: grid; grid-template-columns: repeat(3, 1fr); grid-gap: 2rem; } li h3 { margin-bottom: 1rem; } form label { display: block; margin-bottom: 0.5rem; } form input, form textarea { display: block; width: 100%; padding: 0.5rem; margin-bottom: 1rem; border: 1px solid #ccc; border-radius: 0.25rem; } form input[type="submit"] { background-color: #333; color: #fff; border: none; padding: 0.5rem 1rem; border-radius: 0.25rem; cursor: pointer; } ``` JavaScript代码: ```javascript // No JavaScript code needed for this project ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值