[关闭]
@Chilling 2017-01-16T08:16:34.000000Z 字数 2098 阅读 813

FZU-2124: 吃豆人

搜索


Description

吃豆人是一款非常经典的游戏,游戏中玩家控制吃豆人在地图上吃光所有豆子,并且避免被怪物抓住。

这道题没有怪物,将游戏的画面分成n*m的格子,每格地形可能为空地或者障碍物,吃豆人可以在空地上移动,吃豆人每移动一格需要1s时间,并且只能朝上下左右四个方向移动,特别的是吃豆人还能吐出舌头,舌头每移动一格需要0.1s时间,舌头只可以走直线。不必考虑吃豆人转身所需要的时间。

举例,吃豆人在(1,1)坐标,而豆子在(1,5)坐标,并且中间没有障碍物,此时朝豆子方向吐舌头~,经过0.8s就可以吃到豆子(来回各0.4s,吐出去的舌头要缩回来的嘛)。

游戏中还有加速道具,一旦得到加速道具,吃豆人就获得2倍移动速度,吐舌头的速度没有增加,即走1格用0.5s。现在地图上有且只有一颗豆子。游戏中有.代表空地;X表示障碍,吃豆人不能越过障碍;B代表豆子;S代表加速道具,并且地图上道具总数不超过1个,道具所在的位置为空地,得到道具后立即使用,道具立即消失,地形变为空地,不能用舌头去取道具;P表示吃豆人,吐舌头的时候吃豆人不能移动。

Input

输入包含多组数据。输入第一行有两个个整数n,m(2<=n,m<=20),接着一个n*m的地图矩阵。

对于50%的数据,地图上没有道具。

Output

输出一行,最快用多少s吃到豆子,结果保留1位小数,如果吃不到,输出-1。

Sample Input

2 2
XP
B.
3 2
XP
.S
B.

Sample Output

1.2
1.7

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<math.h>
  4. #include<queue>
  5. #include<algorithm>
  6. using namespace std;
  7. char a[22][22];
  8. int dir[4][2]={1,0,-1,0,0,1,0,-1};
  9. int n,m,x,y,xx,yy,flag;
  10. double ans[22][22][2]; //到x,y点,有加速或无加速时最短的时间,最后再比较到目的点两种方式的最短时间
  11. struct node
  12. {
  13. int x,y;
  14. int s;
  15. double sec;
  16. };
  17. void BFS()
  18. {
  19. node now,next;
  20. queue<node>q;
  21. now.x=x;
  22. now.y=y;
  23. now.s=0;
  24. now.sec=0.0;
  25. q.push(now);
  26. ans[now.x][now.y][now.s]=now.sec;
  27. while(!q.empty())
  28. {
  29. now=q.front();
  30. q.pop();
  31. if(now.x==xx||now.y==yy)
  32. {
  33. flag=1;
  34. for(int i=min(now.x,xx);i<=max(now.x,xx);i++)
  35. {
  36. for(int j=min(now.y,yy);j<=max(now.y,yy);j++)
  37. {
  38. if(a[i][j]=='X')
  39. {
  40. flag=0;
  41. break;
  42. }
  43. }
  44. }
  45. if(flag==1)
  46. ans[xx][yy][now.s]=min(ans[xx][yy][now.s],now.sec+0.2*(abs(now.x-xx)+abs(now.y-yy)));
  47. }
  48. for(int i=0;i<4;i++)
  49. {
  50. next=now;
  51. next.x+=dir[i][0];
  52. next.y+=dir[i][1];
  53. if(next.x>=0&&next.x<n&&next.y>=0&&next.y<m&&a[next.x][next.y]!='X')
  54. {
  55. if(now.s==0)
  56. next.sec+=1.0;
  57. if(now.s==1)
  58. next.sec+=0.5;
  59. if(a[next.x][next.y]=='S')
  60. next.s=1;
  61. else
  62. {
  63. if(now.s==0)
  64. next.s=0;
  65. else
  66. next.s=1;
  67. }if(ans[next.x][next.y][next.s]>next.sec)
  68. {
  69. ans[next.x][next.y][next.s]=next.sec;
  70. q.push(next);
  71. }
  72. }
  73. }
  74. }
  75. }
  76. int main()
  77. {
  78. int i,j,k;
  79. while(scanf("%d%d",&n,&m)!=EOF)
  80. {
  81. for(i=0;i<22;i++)
  82. for(j=0;j<22;j++)
  83. for(k=0;k<2;k++)
  84. ans[i][j][k]=999999.0;
  85. memset(a,0,sizeof(a));
  86. for(i=0;i<n;i++)
  87. scanf("%s",a[i]);
  88. for(i=0;i<n;i++)
  89. {
  90. for(j=0;j<m;j++)
  91. {
  92. if(a[i][j]=='P')
  93. {
  94. x=i;
  95. y=j;
  96. }
  97. if(a[i][j]=='B')
  98. {
  99. xx=i;
  100. yy=j;
  101. }
  102. }
  103. }
  104. BFS();
  105. double aa=min(ans[xx][yy][0],ans[xx][yy][1]);
  106. if(aa==999999.0) //说明这一点并没有被经过
  107. printf("-1");
  108. else
  109. printf("%.1f\n",min(ans[xx][yy][0],ans[xx][yy][1]));
  110. }
  111. return 0;
  112. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注