• 数据结构--管道


    题目:有长度为L的探测管,从坐标(X,Y)为起点对周围水管进行探测。水管形状如下

    红色数字代表水管型号,输入第一行T,代表T个样例;

    第二行输入N,代表图的大小N*N;

    第三行输入探测管的起始探测的坐标(X,Y)以及长度L;

    以下N行输入数字,0代表没有水管,1~7代表如上水管形状;测试样例如下,(3,3)为起始探测点,探测管长度为7,五角星记为能探测到的管子,一共有17根管子可以探测到;

    输出最多能探测多少根水管。(本样例中探测起点不算长度,实际考试具体忘了算不算。测试样例是手编样例,不是考试样例。水管形状对应的数字也为手编)

    1

    6

    3 3 7

    6 4 1 3 5 0

    3 7 6 4 2 0

    0 4 3 1 4 5

    4 2 4 1 2 2

    7 7 1 7 3 6

    1 0 7 2 2 4

    完整代码:

    1. // 管道.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
    2. //
    3. #include
    4. using namespace std;
    5. //探测管长度,探测的管道数
    6. int L,step=0,n;
    7. //管道
    8. int pipe[101][101];
    9. //标记管道是否被走过
    10. int book[101][101];
    11. //管子种类(方向分布:右下左上)
    12. int category[9][5] = {
    13. {0,0,0,0},{1,1,1,1},{0,1,0,1},{1,0,1,0},{1,1,0,0},{0,1,1,0},{0,0,1,1},{1,0,0,1}
    14. };
    15. //1表示通,0表示不通
    16. struct queue {
    17. int x;
    18. int y;
    19. int step;
    20. }Q[10001];
    21. //置零操作
    22. void init() {
    23. for(int i=1;i<=n;i++)
    24. for (int j = 1;j <= n;j++) {
    25. book[i][j] = 0;
    26. pipe[i][j] = 0;
    27. }
    28. }
    29. //判断当前管道的方向能否与上一个管道方向对应
    30. int direction(int nextdir,int k) {
    31. //判断这个管道与k相反的方向是否为1
    32. //首先判断k的相反方向
    33. int nek;
    34. switch (k) {
    35. case 0:nek = 2;break;
    36. case 1:nek = 3;break;
    37. case 3:nek = 1;break;
    38. case 2:nek = 0;break;
    39. }
    40. //cout<
    41. return category[nextdir][nek];
    42. }
    43. void BFS(int startx,int starty) {
    44. int next[4][2] = { {0,1},{1,0},{0,-1},{-1,0} };//右下左上
    45. //队列初始化
    46. int head = 0, tail = 0;
    47. Q[head].x = startx;
    48. Q[head].y = starty;
    49. Q[head].step=0;
    50. book[startx][starty]=1;
    51. tail++;
    52. //判断起点管道种类pipe[startx][starty]
    53. int tx,ty;
    54. while(Q[head].step <=L) {
    55. //cout<
    56. int newpipe=pipe[Q[head].x][Q[head].y];
    57. //cout<
    58. //判断当前有几个方向可以走
    59. for (int k = 0;k < 4;k++) {
    60. //该类管道在category中的行数(因为从0开试)
    61. //如果当前管道的方向为1,说明是通路可以去下一个
    62. if (category[newpipe][k] == 1) {
    63. //新的坐标
    64. tx = Q[head].x + next[k][0];
    65. ty = Q[head].y + next[k][1];
    66. //判断边界
    67. if(tx>=1&&ty>=1&&tx<=n&&ty<=n){
    68. //判断新坐标是否能走 能走则入队
    69. if (direction(pipe[tx][ty],k)==1&&book[tx][ty]==0) {
    70. //做标记 已走过
    71. book[tx][ty]=1;
    72. //新的坐标入队
    73. Q[tail].x = tx;
    74. Q[tail].y = ty;
    75. cout<
    76. tail++;
    77. //探测到的管道数+1,第一个不算后面要减一
    78. Q[tail].step=Q[head].step+1;
    79. step++;
    80. //探测管长度--
    81. }
    82. }
    83. }
    84. }
    85. head++;
    86. }
    87. return;
    88. }
    89. int main()
    90. {
    91. int i, j, T,startx,starty;
    92. cin >> T;
    93. //T组样例
    94. for (i = 1;i <= T;i++) {
    95. cin >> n;
    96. //输入起点坐标和探测管长度
    97. cin >> startx >> starty >> L;
    98. //输入管道地图
    99. for (int p = 1;p <= n;p++)
    100. for (int q = 1;q <= n;q++)
    101. cin >> pipe[p][q];
    102. BFS(startx, starty);
    103. cout<1;
    104. for(int i=0;i<8;i++){
    105. for (int j = 0;j < 4;j++) {
    106. cout<
    107. }
    108. cout<
    109. }
    110. //cout<
    111. init();
    112. step = 0;
    113. }
    114. return 0;
    115. }

  • 相关阅读:
    【前端三栏布局总结】常见的前端三栏布局有哪些
    实现打印功能
    开发者驱动的软件公司,如何赚取万亿美元?
    多线程案例——阻塞式队列
    HMS Core基于地理位置请求广告,流量变现快人一步
    Tomcat服务启动失败:java.lang.OutOfMemoryError: Java heap space
    公益培训报名小程序
    ps神经网络滤镜安装包,ps神经网络滤镜用不了
    日常学习记录随笔-大数据之日志(hadoop)收集实战
    超级哇塞的快排,你值得学会!
  • 原文地址:https://blog.csdn.net/qq_63533863/article/details/126180986