当前位置:   article > 正文

TYVJ-1030 如草的入侵 FLOOD FILL_wy90om浮力线路

wy90om浮力线路

水题一道....要细心!!!

  1. /*
  2. * tyvj-p1030 乳草的入侵
  3. * mike-w
  4. * 2011-10-26
  5. * ---------------------
  6. * simple flood fill
  7. * 注意左下角是(1,1)
  8. * url: http://tyvj.cpwz.cn/Problem_Show.asp?id=1030
  9. */
  10. #include<stdio.h>
  11. #include<stdlib.h>
  12. #include<string.h>
  13. #define SIZE 110
  14. #define QSIZE (SIZE*SIZE)
  15. typedef struct _pos
  16. {
  17. int x,y;
  18. }pos;
  19. char f[SIZE][SIZE];
  20. int g[SIZE][SIZE];
  21. pos q[QSIZE];
  22. int qtail,qhead,qlen;
  23. int xx,yy,sx,sy;
  24. int d[8][2]={
  25. {1,1},{1,0},{1,-1},
  26. {0,1},{0,-1},{-1,1},
  27. {-1,0},{-1,-1}
  28. };
  29. int push(int x,int y)
  30. {
  31. q[qtail].x=x;
  32. q[qtail].y=y;
  33. if(++qtail==QSIZE)
  34. qtail=0;
  35. qlen++;
  36. return 0;
  37. }
  38. int pop(int *x,int *y)
  39. {
  40. *x=q[qhead].x;
  41. *y=q[qhead].y;
  42. if(++qhead==QSIZE)
  43. qhead=0;
  44. qlen--;
  45. return 0;
  46. }
  47. int flood_fill(void)
  48. {
  49. int x,y,tx,ty,i;
  50. push(sx,sy);
  51. g[sx][sy]=0; /* 原来第一天不算... */
  52. while(qlen>0)
  53. {
  54. pop(&x,&y);
  55. for(i=0;i<8;i++)
  56. {
  57. tx=x+d[i][0];
  58. ty=y+d[i][1];
  59. if(tx>=0 && tx<xx && ty>=0 && ty<yy
  60. && f[tx][ty]=='.' && !g[tx][ty])
  61. {
  62. push(tx,ty);
  63. g[tx][ty]=g[x][y]+1;
  64. }
  65. }
  66. }
  67. return 0;
  68. }
  69. int main(void)
  70. {
  71. int i,j,ans;
  72. freopen("in","r",stdin);
  73. scanf("%d%d%d%d",&yy,&xx,&sy,&sx);
  74. sx=xx-sx;
  75. sy--;
  76. for(i=0;i<xx;i++)
  77. scanf("%s",f[i]);
  78. flood_fill();
  79. for(i=0,ans=0;i<xx;i++)
  80. for(j=0;j<yy;j++)
  81. if(ans<g[i][j])
  82. ans=g[i][j];
  83. printf("%d\n",ans);
  84. return 0;
  85. }


 

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/232047
推荐阅读
相关标签
  

闽ICP备14008679号