hdu2425
来源:互联网 发布:三国无双7帝国捏脸数据 编辑:程序博客网 时间:2024/05/18 17:45
Hiking Trip
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1267 Accepted Submission(s): 552
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
#include<iostream>#include<queue>#include<cstring>#include<cstdio>using namespace std;const int maxn = 25;char map[maxn][maxn];bool visit[maxn][maxn];int dir[4][2] = {{-1,0},{1,0},{0,-1},{0,1}};int r, c, p, s, t, sr, sc, tr, tc;int Case = 0;struct node{int x, y, step;friend bool operator < (const node n1, const node n2){return n1.step>n2.step;}};bool right(int x, int y){if(x>=0&&y>=0&&x<r&&y<c&&map[x][y]!='@'&&!visit[x][y]){return true;}return false;}int add(int x, int y){if(map[x][y]=='#'){return p;}else if(map[x][y] == '.'){return s;}else{return t;}}int bfs(){node n;int i;n.x = sr;n.y = sc;n.step = 0;visit[sr][sc] = 1;priority_queue<node> q;q.push(n);node n1;while(!q.empty()){n = q.top();q.pop();for(i = 0; i < 4; i++){//node n1;n1.x = n.x+dir[i][0];n1.y = n.y + dir[i][1];if(right(n1.x, n1.y)){n1.step = n.step+add(n1.x, n1.y);if(n1.x==tr&&n1.y==tc){return n1.step;}visit[n1.x][n1.y] = 1;q.push(n1);}}}return -1;}int main(){//freopen("in.txt", "r", stdin);while(cin>>r>>c){cin>>p>>s>>t;for(int i = 0; i < r; i++){for(int j = 0; j < c; j++){cin>>map[i][j];}}cin>>sr>>sc>>tr>>tc;memset(visit, 0, sizeof(visit));printf("Case %d: %d\n",++Case,bfs()); }return 0;}
找不出错误的原因............
0 0
- hdu2425
- hdu2425
- hdu2425解题报告
- hdu2425——Hiking Trip
- HDU2425:Hiking Trip(BFS+优先队列)
- HDU2425:Hiking Trip(BFS+优先队列)
- HDU2425 Hiking Trip(bfs+优先队列)
- BI学习路线
- Android 新浪博客分享问题总结
- Android学习之——ScrollView和ListView,GridView嵌套,显示顶
- 【读书笔记】Objective-C高级编程--Block概要
- VS2010自动化测试系列 收藏
- hdu2425
- 简介ViewGroup
- 编程注意规范
- 指针—选择法
- 线段树
- 黑马程序员---c语言输入输出函数辨析
- ActionBarActivity: cannot be resolved to a type
- ios学习 登陆框的简单获取(一)
- 你该怎样用svn才能避免冲突? (内附逻辑图和详细解释)