[关闭]
@Chilling 2017-01-16T16:15:38.000000Z 字数 1995 阅读 889

HDU-2579: Dating with girls(2)

搜索


Description

If you have solved the problem Dating with girls(1).I think you can solve this problem too.This problem is also about dating with girls. Now you are in a maze and the girl you want to date with is also in the maze.If you can find the girl, then you can date with the girl.Else the girl will date with other boys. What a pity!
The Maze is very strange. There are many stones in the maze. The stone will disappear at time t if t is a multiple of k(2<= k <= 10), on the other time , stones will be still there.
There are only ‘.’ or ‘#’, ’Y’, ’G’ on the map of the maze. ’.’ indicates the blank which you can move on, ‘#’ indicates stones. ’Y’ indicates the your location. ‘G’ indicates the girl's location . There is only one ‘Y’ and one ‘G’. Every seconds you can move left, right, up or down.
此处输入图片的描述

Input

The first line contain an integer T. Then T cases followed. Each case begins with three integers r and c (1 <= r , c <= 100), and k(2 <=k <= 10).
The next r line is the map’s description.

Output

For each cases, if you can find the girl, output the least time in seconds, else output "Please give me another chance!".

Sample Input

1
6 6 2
...Y..
...#..
.#....
...#..
...#..
..#G#.

Sample Output

7

  1. #include<stdio.h>
  2. #include<queue>
  3. #include<string.h>
  4. using namespace std;
  5. int x,y,m,n,k,flag,ys;
  6. char a[111][111];
  7. int dir[4][2]={1,0,-1,0,0,1,0,-1};
  8. int vis[111][111][11];
  9. struct node
  10. {
  11. int x,y,sec;
  12. };
  13. void BFS()
  14. {
  15. node now,next;
  16. queue<node>q;
  17. now.x=x;
  18. now.y=y;
  19. now.sec=0;
  20. q.push(now);
  21. vis[now.x][now.y][0]=1;
  22. while(!q.empty())
  23. {
  24. now=q.front();
  25. q.pop();
  26. if(a[now.x][now.y]=='G')
  27. {
  28. flag=1;
  29. printf("%d\n",now.sec);
  30. return;
  31. }
  32. for(int i=0;i<4;i++)
  33. {
  34. next=now;
  35. next.x+=dir[i][0];
  36. next.y+=dir[i][1];
  37. next.sec++;
  38. ys=next.sec%k;
  39. if(next.x>=0&&next.x<m&&next.y>=0&&next.y<n&&vis[next.x][next.y][ys]==0)
  40. {
  41. if(a[next.x][next.y]!='#')
  42. {
  43. vis[next.x][next.y][ys]=1; //到这点的时间取余为某值用的最小时间
  44. q.push(next);
  45. }
  46. if(ys==0&&a[next.x][next.y]=='#')
  47. {
  48. vis[next.x][next.y][ys]=1;
  49. q.push(next);
  50. }
  51. }
  52. }
  53. }
  54. }
  55. int main()
  56. {
  57. int t,i,j;
  58. scanf("%d",&t);
  59. while(t--)
  60. {
  61. memset(a,0,sizeof(a));
  62. memset(vis,0,sizeof(vis));
  63. flag=0;
  64. scanf("%d%d%d",&m,&n,&k);
  65. for(i=0;i<m;i++)
  66. scanf("%s",a[i]);
  67. for(i=0;i<m;i++)
  68. for(j=0;j<n;j++)
  69. {
  70. if(a[i][j]=='Y')
  71. {
  72. x=i;
  73. y=j;
  74. }
  75. }
  76. BFS();
  77. if(flag==0)
  78. printf("Please give me another chance!\n");
  79. }
  80. return 0;
  81. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注