• 蓝桥杯每日一题2023.10.17


    迷宫 - 蓝桥云课 (lanqiao.cn)

    题目描述

    样例:

    1. 01010101001011001001010110010110100100001000101010
    2. 00001000100000101010010000100000001001100110100101
    3. 01111011010010001000001101001011100011000000010000
    4. 01000000001010100011010000101000001010101011001011
    5. 00011111000000101000010010100010100000101100000000
    6. 11001000110101000010101100011010011010101011110111
    7. 00011011010101001001001010000001000101001110000000
    8. 10100000101000100110101010111110011000010000111010
    9. 00111000001010100001100010000001000101001100001001
    10. 11000110100001110010001001010101010101010001101000
    11. 00010000100100000101001010101110100010101010000101
    12. 11100100101001001000010000010101010100100100010100
    13. 00000010000000101011001111010001100000101010100011
    14. 10101010011100001000011000010110011110110100001000
    15. 10101010100001101010100101000010100000111011101001
    16. 10000000101100010000101100101101001011100000000100
    17. 10101001000000010100100001000100000100011110101001
    18. 00101001010101101001010100011010101101110000110101
    19. 11001010000100001100000010100101000001000111000010
    20. 00001000110000110101101000000100101001001000011101
    21. 10100101000101000000001110110010110101101010100001
    22. 00101000010000110101010000100010001001000100010101
    23. 10100001000110010001000010101001010101011111010010
    24. 00000100101000000110010100101001000001000000000010
    25. 11010000001001110111001001000011101001011011101000
    26. 00000110100010001000100000001000011101000000110011
    27. 10101000101000100010001111100010101001010000001000
    28. 10000010100101001010110000000100101010001011101000
    29. 00111100001000010000000110111000000001000000001011
    30. 10000001100111010111010001000110111010101101111000

    题目分析

    使用常见的bfs算法算出最短路,重点在于如何输出路径,我们可以每走一步将上一步使用pre数组存下即可,我们从{1,1}出发到{30,50}结束,可以从结束的位置不断找到pre也就是此位置的上一个位置,如果找到了{1,1}也就是说我们找到了所有的位置,但我们此时是倒着找的需要将其翻转即可,注意需要按照字典序也就是DLRU的顺序寻找

    1. #include
    2. using namespace std;
    3. const int N = 2e3 + 10;
    4. typedef pair<int, int> PII;
    5. PII pre[N][N], endd, startt;
    6. queue q;
    7. string s;
    8. bool st[N][N];
    9. char g[N][N];
    10. int dx[4] = {1, 0, 0, -1};
    11. int dy[4] = {0, -1, 1, 0};
    12. void bfs()
    13. {
    14. st[1][1] = true;
    15. while(q.size())
    16. {
    17. auto t = q.front();
    18. int x = t.first;
    19. int y = t.second;
    20. q.pop();
    21. for(int i = 0; i < 4; i ++)
    22. {
    23. int a = x + dx[i];
    24. int b = y + dy[i];
    25. if(a < 1 || a > 30 || b < 1 || b > 50 || st[a][b] == 1)continue;
    26. if(g[a][b] == '0')
    27. {
    28. q.push({a, b});
    29. pre[a][b] = t;
    30. st[a][b] = true;
    31. }
    32. }
    33. }
    34. }
    35. int main()
    36. {
    37. for(int i = 1; i <= 30; i ++)
    38. {
    39. for(int j = 1; j <= 50; j ++)
    40. {
    41. cin >> g[i][j];
    42. }
    43. }
    44. q.push({1, 1});
    45. bfs();
    46. endd = {30, 50};
    47. while(true)
    48. {
    49. char t;
    50. startt = endd;
    51. endd = pre[endd.first][endd.second];
    52. // cout << endd.first << ' ' << endd.second << '\n';
    53. if(endd.first - startt.first == -1 && endd.second - startt.second == 0)s += "D";
    54. else if(endd.first - startt.first == 1 && endd.second - startt.second == 0)s += "U";
    55. else if(endd.first - startt.first == 0 && endd.second - startt.second == -1)s += "R";
    56. else if(endd.first - startt.first == 0 && endd.second - startt.second == 1)s += "L";
    57. if(endd.first == 1 && endd.second == 1)break;
    58. }
    59. reverse(s.begin(), s.end());
    60. cout << s;
    61. return 0;
    62. }
  • 相关阅读:
    SQL Server 中函数PATINDEX()和CHARINDEX()
    Verilog HDL——任务与函数
    分布式场景仿真中的空间坐标转换研究
    java基础 System类、BigInteger 和 BigDecimal类、时间类
    OCCT v11.0.16 x64 电脑硬件检测烤鸡软件中文
    Java的反射
    架构师习题--嵌入式习题
    TCP/IP网络分层模型
    Qt地铁智慧换乘系统浅学(四 )实现添加线路,添加站点,添加边 并且存储到本地txt文件
    Docker容器内时区相差8小时
  • 原文地址:https://blog.csdn.net/m0_75087931/article/details/133881951