Hiking Trip(bfs+优先队列)

水深无声 2022-08-01 12:15 241阅读 0赞

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

  1. 4 6
  2. 1 2 10
  3. T...TT
  4. TTT###
  5. TT.@#T
  6. ..###@
  7. 0 1 3 0
  8. 4 6
  9. 1 2 2
  10. T...TT
  11. TTT###
  12. TT.@#T
  13. ..###@
  14. 0 1 3 0
  15. 2 2
  16. 5 1 3
  17. T@
  18. @.
  19. 0 0 1 1

Sample Output

  1. Case 1: 14
  2. Case 2: 8
  3. Case 3: -1

Source

2008 Asia Hangzhou Regional Contest Online

这一题是bfs+优先队列!

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

可能的短!

  1. #include<cstdio>
  2. #include<iostream>
  3. #include<queue>
  4. #include<cstring>
  5. using namespace std;
  6. char map[25][25];
  7. int vis[25][25];
  8. int dir[4][2]={0,1,0,-1,1,0,-1,0};
  9. int n,m,t1,t2,t3,sx,sy,ex,ey;
  10. struct node
  11. {
  12. int x;
  13. int y;
  14. int t;
  15. };
  16. struct cmp
  17. {
  18. bool operator()(node a,node b)
  19. {
  20. return a.t>b.t;//对头到队尾时间从小到大!
  21. }
  22. };
  23. int judge(int x,int y)
  24. {
  25. if(x>=0&&x<n&&y>=0&&y<m&&!vis[x][y]&&map[x][y]!='@')
  26. {
  27. return 1;
  28. }
  29. return 0;
  30. }
  31. int bfs(int sx,int sy)
  32. {
  33. priority_queue<node,vector<node>,cmp>q;
  34. int i,x1,y1;
  35. node cur,next;
  36. cur.x=sx;
  37. cur.y=sy;
  38. cur.t=0;
  39. q.push(cur);
  40. vis[sx][sy]=1;
  41. while(!q.empty())
  42. {
  43. cur=q.top();
  44. q.pop();
  45. if(cur.x==ex&&cur.y==ey)
  46. {
  47. return cur.t;
  48. }
  49. for(i=0;i<4;i++)
  50. {
  51. x1=cur.x+dir[i][0];
  52. y1=cur.y+dir[i][1];
  53. if(judge(x1,y1))
  54. {
  55. vis[x1][y1]=1;
  56. next.x=x1;
  57. next.y=y1;
  58. if(map[x1][y1]=='T')
  59. {
  60. next.t=cur.t+t3;
  61. }
  62. else if(map[x1][y1]=='#')
  63. {
  64. next.t=cur.t+t1;
  65. }
  66. else if(map[x1][y1]=='.')
  67. {
  68. next.t=cur.t+t2;
  69. }
  70. q.push(next);
  71. }
  72. //把这个点相关都可以走的点压进去,然后消除这个点!
  73. //为什么按照时间最小点压出去呢?
  74. //首先bfs要求获得最小的时间就应该从当前时间的最小点开始搜索!
  75. }
  76. }
  77. return -1;
  78. }
  79. int main()
  80. {
  81. int i,j,num=1;
  82. while(cin>>n>>m)
  83. {
  84. memset(map,0,sizeof(map));
  85. memset(vis,0,sizeof(vis));
  86. cin>>t1>>t2>>t3;
  87. for(i=0;i<n;i++)
  88. {
  89. cin>>map[i];
  90. }
  91. // cout<<"ok"<<endl;
  92. cin>>sx>>sy>>ex>>ey;
  93. // cout<<"ok"<<endl;
  94. /* for(i=0;i<n;i++)
  95. {
  96. cout<<map[i]<<endl;
  97. }*/
  98. printf("Case %d: %d\n",num++,bfs(sx,sy));
  99. }
  100. }

发表评论

表情:
评论列表 (有 0 条评论,241人围观)

还没有评论,来说两句吧...

相关阅读

    相关 队列优先队列

    队列 队列是一种特殊的[线性表][Link 1],特殊之处在于它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作,和栈一样,队列是一种操作

    相关 优先队列

    优先队列:顾名思义,首先它是一个队列,但是它强调了“优先”二字,所以,已经不能算是一般意义上的队列了,它的“优先”意指取队首元素时有一定的选择性,即根据元素的属性选择某一项值最

    相关 优先队列

    [为什么80%的码农都做不了架构师?>>> ][80_] ![hot3.png][] 优先队列是不同于先进先出队列的另一种队列。每次从队列中取出的是具有最高优先权的元素。