[关闭]
@lychee123 2017-03-18T13:44:47.000000Z 字数 1367 阅读 979

HDU-2579:Dating with girls(2)

BFS


题面链接

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