HDU 2425-hiking trip (BFS + priority queue)

Source: Internet
Author: User

Hiking trip Time Limit: 5000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 1303 accepted submission (s): 576


Problem descriptionhiking 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.

Inputthere 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.

Outputfor 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###[email protected]#T..###@0 1 3 04 61 2 2T...TTTTT###[email protected]#T..###@0 1 3 02 25 1 3[email protected]@.0 0 1 1

Sample output
Case 1: 14Case 2: 8Case 3: -1  前几年的区域赛签到题~ 一开始看了一道过的最多的居然是道网络流。。sad 果断扔了看这道搜索。  题意:给出起点和终点 求最短到达时间,图中有4种点,有墙,树,路,石头,除了石头不能走,其他的三种都可以走,且走每一种点花费的时间均已给出。优先队列+BFS水过。
#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <vector>#include <queue>using namespace std;struct node{int x,y,step;bool operator <(const node &c)const{   return c.step<step;}};char ma[22][22];int sx,sy,ex,ey,m,n,vp,vs,vt;bool vis[22][22];int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}};void bfs(){   memset(vis,0,sizeof(vis));   priority_queue <node> Q;   node now,next;   now.x=sx;now.y=sy;now.step=0;   vis[now.x][now.y]=1;   Q.push(now);   while(!Q.empty())   {     now=Q.top();Q.pop();     if(now.x==ex&&now.y==ey)  {  printf("%d\n",now.step);  return ;  }  for(int i=0;i<4;i++)  {  next.x=now.x+dir[i][0];  next.y=now.y+dir[i][1];  if(next.x>=0&&next.x<m&&next.y>=0&&next.y<n&&!vis[next.x][next.y]&&ma[next.x][next.y]!='@'){if(ma[next.x][next.y]=='T'){next.step=now.step+vt;vis[next.x][next.y]=1;Q.push(next);}else if(ma[next.x][next.y]=='.'){next.step=now.step+vs;vis[next.x][next.y]=1;Q.push(next);}else if(ma[next.x][next.y]=='#'){next.step=now.step+vp;vis[next.x][next.y]=1;Q.push(next);}}  }   }   printf("-1\n");}int main(){int cas=1;while(scanf("%d %d",&m,&n)!=EOF){scanf("%d %d %d",&vp,&vs,&vt);for(int i=0;i<m;i++)scanf("%s",ma[i]);scanf("%d%d%d%d",&sx,&sy,&ex,&ey);printf("Case %d: ",cas++);bfs();}return 0;}


HDU 2425-hiking trip (BFS + priority queue)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.