Hiking Trip(bfs+优先队列)
来源:互联网 发布:ff14人男大叔捏脸数据 编辑:程序博客网 时间:2024/05/16 16:14
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.
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, VP, VS, VT (1 <= VP <= 100, 1 <= VS <= 100, 1 <= VT <= 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, SR, SC, TR, TC, (0 <= SR < R, 0 <= SC < C, 0 <= TR < R, 0 <= TC < 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.
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 61 2 10T...TTTTT###TT.@#T..###@0 1 3 04 61 2 2T...TTTTT###TT.@#T..###@0 1 3 02 25 1 3T@@.0 0 1 1
Sample Output
Case 1: 14Case 2: 8Case 3: -1
Source
2008 Asia Hangzhou Regional Contest Online
这一题是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
- Hiking Trip(bfs+优先队列)
- HDU2425 Hiking Trip(bfs+优先队列)
- HDU 2425 Hiking Trip(bfs+优先队列)
- HDU 2425 Hiking Trip(bfs+优先队列)
- HDU2425:Hiking Trip(BFS+优先队列)
- HDU 2425-Hiking Trip(BFS+优先队列)
- HDU 2425 Hiking Trip(BFS+优先队列)
- HDU2425:Hiking Trip(BFS+优先队列)
- HDU 2425 Hiking Trip(搜索+优先队列)
- hdu5360--Hiking(优先队列)
- hdu 2425 Hiking Trip(bfs)
- HDU 5360 Hiking(优先队列)
- Hiking(vector+优先队列 实现贪心)
- HDU5630 Hiking(贪心+优先队列)
- HDU 5360 Hiking (贪心+优先队列)
- hdu 5360 Hiking (贪心+优先队列)
- hdu 2425——Hiking Trip(BFS)
- HDU 5360 Hiking 优先队列
- redis源码分析(1) 服务端网络链接模型
- swift入门第四章函数
- 真TMD难,代码。我是菜鸟别喷
- codeforces 24A Ring road (dfs)
- 【linux命令解析】【nohup &】
- Hiking Trip(bfs+优先队列)
- LINUX系统监控常用命令
- Hibernate入门篇(三)——编写第一个Hibernate例子
- dede分页样式修改(支持bootstrap分页样式)
- ArcGIS桌面软件不能打开的解决办法
- java和ajax实现带文件的form提交
- CSS 设置按钮,文本输入框
- make参数详解
- 六月二十三