• 请帮我看下下面的题我写的代码为什么没有输出,哪里写错了


    关注 码龄 粉丝数 原力等级 -- 被采纳 被点赞 采纳率 动不了一点 2024-03-20 21:12 采纳率: 20% 浏览 3 首页/ 数据结构与算法 / 请帮我看下下面的题我写的代码为什么没有输出,哪里写错了 图搜索算法数据结构c++ 请帮我看下下面的题我写的代码为什么没有输出,哪里写错了题目:可怜的公主在一次次被魔王掳走一次次被骑士们救回来之后,而今,不幸的她再一次面临生命的考验。魔王已经发出消息说将在T时刻吃掉公主,因为他听信谣言说吃公主的肉也能长生不老。年迈的国王正是心急如焚,告招天下勇士来拯救公主。不过公主早已习以为常,她深信智勇的骑士LJ肯定能将她救出。现据密探所报,公主被关在一个两层的迷宫里,迷宫的入口是S(0,0,0),公主的位置用P表示,时空传输机用#表示,墙用表示,平地用.表示。骑士们一进入时空传输机就会被转到另一层的相对位置,但如果被转到的位置是墙的话,那骑士们就会被撞死。骑士们在一层中只能前后左右移动,每移动一格花1时刻。层间的移动只能通过时空传输机,且不需要任何时间。Input输入的第一行C表示共有C个测试数据,每个测试数据的前一行有三个整数N,M,T。 N,M迷宫的大小NM(1 <= N,M <=10)。T如上所意。接下去的前NM表示迷宫的第一层的布置情况,后NM表示迷宫第二层的布置情况。Output如果骑士们能够在T时刻能找到公主就输出“YES”,否则输出“NO”。 样例输出:15 5 14S*#*..#........****....#. ...P#...***.....*.*.#.. 样例输出:YES 我的代码: #include "iostream" #include "queue" #include "cstring" using namespace std; int c; int n, m, v; char map1[15][15]; int abookmap1[15][15]; char map2[15][15]; int abookmap2[15][15]; int next1[4][2] = { {0,1},{0,-1},{1,0},{-1,0} }; struct point { int x; int y; int floor; int step; }; int main() { queueq; memset(abookmap1, 0, sizeof(abookmap1)); memset(abookmap2, 0, sizeof(abookmap2)); cin >> c; while (c--) { cin >> n >> m >> v; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { cin >> map1[i][j]; if (map1[i][j] == 'S') { q.push(point{ i,j,1,0 }); map1[i][j] = '*'; } } for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) cin >> map2[i][j]; while (!q.empty()) { point t = q.front(); q.pop(); for (int i = 0; i < 4; i++) { int tx = t.x + next1[i][0]; int ty = t.y + next1[i][1]; if (tx<1 || ty<1 || tx>n || ty>m)continue; if ((t.floor == 1 && abookmap1[tx][ty] == 1) || (t.floor == 1 && map1[tx][ty] == '*'))continue; if(t.floor == 2 && abookmap2[tx][ty] == 1 || (t.floor == 2 && map2[tx][ty] == '*'))continue; if (t.floor == 1) { if (map1[tx][ty] == '.') { q.push(point{ tx,ty,1,t.step + 1 }); abookmap1[tx][ty] = 1; } if (map1[tx][ty] == '#') { q.push(point{ tx,ty,2,t.step + 1 }); abookmap2[tx][ty] = 1; abookmap1[tx][ty] = 1; } if (map1[tx][ty] == 'P') { int temp = t.step + 1; if (temp <= v) cout << "YES" << endl; else cout << "NO" << endl; abookmap1[tx][ty] = 1; } } if (t.floor == 2) { if (map2[tx][ty] == '.') { q.push(point{ tx,ty,2,t.step + 1 }); abookmap2[tx][ty] = 1; } if (map2[tx][ty] == '#') { q.push(point{ tx,ty,1,t.step + 1 }); abookmap2[tx][ty] = 1; abookmap1[tx][ty] = 1; if (map2[tx][ty] == 'P') { int temp = t.step + 1; if (temp <= v) cout << "YES" << endl; else cout << "NO" << endl; abookmap2[tx][ty] = 1; } } } } } } } 展开全部 收起 写回答 好问题 0 提建议 追加酬金 关注问题 微信扫一扫 点击复制链接 分享 邀请回答 编辑 收藏 删除 结题 收藏 举报 追加酬金 (90%的用户在追加酬金后获得了解决方案) 当前问题酬金 ¥ 0 (可追加 ¥500) ¥ 15¥ 20¥ 50¥ 100¥ 200 支付方式 扫码支付 二维码出错 点击刷新 支付金额 15 元 提供问题酬金的用户不参与问题酬金结算和分配 支付即为同意 《付费问题酬金结算规则》 结题 再想想 删除 再等等

    3条回答 默认 最新

    • 码农阿豪 Java领域优质创作者 2024-03-20 21:17
      关注
      让阿豪来帮你解答,本回答参考chatgpt3.5编写提供,如果还有疑问可以评论或留言
      问题:代码为什么没有输出,哪里写错了? 回答: 从代码的缩进来看,代码可能存在部分被裁剪的情况,无法完整运行。在存在问题的情况下,建议将完整的代码分享,有利于更好的发现问题所在。 同时,从代码中也可以看到一些错误,比如:
      • 在输入时没有将空格完全去掉,如“N,M”应为“N, M”;
      • 在定义next1数组时,第二维应为3而非2,如“int next1[4][3]”;
      • 在第一层的循环中,map1数组的下标范围为[1, n],但在第二层的循环中下标却为[1, m],可能导致程序崩溃;
      • 在判断是否跨层时,判断语句可能存在逻辑问题,可能导致队列永远无法清空。 为了更好的解答问题,建议提供完整的代码。同时,也可以参考以下代码进行改进:
      #include 
      #include 
      #include 
      using namespace std;
      const int MAXN = 15;
      const int MAXM = 15;
      int c;
      int n, m, v;
      char map1[MAXN][MAXM];
      int abookmap1[MAXN][MAXM];
      char map2[MAXN][MAXM];
      int abookmap2[MAXN][MAXM];
      int next1[4][3] = {{0, 1, 0}, {0, -1, 0}, {1, 0, 0}, {-1, 0, 0}};
      struct point {
          int x;
          int y;
          int floor;
          int step;
      };
      bool bfs() {
          queue q;
          memset(abookmap1, 0, sizeof(abookmap1));
          memset(abookmap2, 0, sizeof(abookmap2));
          for (int i = 1; i <= n; i++) {
              for (int j = 1; j <= m; j++) {
                  if (map1[i][j] == 'S') {
                      q.push(point{i, j, 1, 0});
                      map1[i][j] = '*';
                  }
              }
          }
          while (!q.empty()) {
              point t = q.front();
              q.pop();
              if (t.step >= v) {
                  // 时间已经超过限制
                  continue;
              }
              for (int i = 0; i < 4; i++) {
                  int tx = t.x + next1[i][0];
                  int ty = t.y + next1[i][1];
                  if (tx < 1 || tx > n || ty < 1 || ty > m) {
                      // 超出特定区域
                      continue;
                  }
                  if (t.floor == 1) {
                      if (map1[tx][ty] == '.' && abookmap1[tx][ty] == 0) {
                          q.push(point{tx, ty, 1, t.step + 1});
                          abookmap1[tx][ty] = 1;
                      } else if (map1[tx][ty] == '#' && abookmap2[tx][ty] == 0) {
                          q.push(point{tx, ty, 2, t.step + 1});
                          abookmap2[tx][ty] = 1;
                      } else if (map1[tx][ty] == 'P') {
                          if (t.step + 1 < v) {
                              return true;
                          } else {
                              continue;
                          }
                      }
                  } else if (t.floor == 2) {
                      if (map2[tx][ty] == '.' && abookmap2[tx][ty] == 0) {
                          q.push(point{tx, ty, 2, t.step + 1});
                          abookmap2[tx][ty] = 1;
                      } else if (map2[tx][ty] == '#' && abookmap1[tx][ty] == 0) {
                          q.push(point{tx, ty, 1, t.step + 1});
                          abookmap1[tx][ty] = 1;
                      }
                  }
              }
          }
          return false;
      }
      int main() {
          cin >> c;
          while (c--) {
              cin >> n >> m >> v;
              for (int i = 1; i <= n; i++) {
                  for (int j = 1; j <= m; j++) {
                      cin >> map1[i][j];
                  }
              }
              for (int i = 1; i <= n; i++) {
                  for (int j = 1; j <= m; j++) {
                      cin >> map2[i][j];
                  }
              }
              if (bfs()) {
                  cout << "YES" << endl;
              } else {
                  cout << "NO" << endl;
              }
          }
          return 0;
      }
      

      展开全部

      评论
    • CSDN-Ada助手 CSDN-AI 官方账号 2024-03-22 06:01
      关注

      【相关推荐】



      • 帮你找了个相似的问题, 你可以看下: https://ask.csdn.net/questions/720067
      • 除此之外, 这篇博客: C语言实现八大排序算法详解及其性能之间的中的 名字已经暴露了他的算法,就是往里面插入数据,就拿我们生活中的例子来说,打扑克牌。我们往手里码牌的时候,是一张一张的码,先码一张,抓手心,不需要修改位置,因为只有一张牌,一定是有序的。再接一张,和手里的牌对比大小,调整位置,选择放在它的左边或者右边。然后接着码,又接到一张牌,拿到先和右边的牌比,比右边还大就放到最右边,如果比右边这张小呢,在和左边这张比。同样,我们这里也是这样的,首先我们默认第一个元素,一定是有序,OK吧。然后第二个,元素比较,大,放到左边,小放到右边。然后第三个元素,直到第N个,比它前一个大,继续往前找位置,直到找到对应位置了,就是有序数列了。(当然每次找位置都是在一个有序的序列中找,所以完全可以用二分查找找位置,数据大的话,二分明显快于我们一张一张比) 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:

        算法思想


      如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^
      评论
    • GISer Liu 2024-03-26 16:51
      关注

      该回答引用自GPT-3.5,由博主GISer Liu编写:

      首先,你的代码中存在一些问题,需要进行修正。在修正之前,我们先来分析一下问题的关键点和解决思路。

      解决思路:

      1. 使用广度优先搜索(BFS)来遍历迷宫,找到公主的位置。
      2. 在搜索过程中,需要考虑迷宫的两层结构和时空传输机的作用。
      3. 使用两个二维数组 abookmap1abookmap2 分别记录骑士在两层迷宫中的访问情况,避免重复访问和撞墙死亡的情况。

        代码修正:

      4. 修正时空传输机位置的判断条件,应该在骑士移动到下一个位置后进行。
      5. 在找到公主位置后,应该立即输出 YES 并结束程序,否则会继续遍历迷宫,导致输出多个 YESNO

        修正后的代码如下所示:

        cppCopy code
        #include 
        #include 
        #include 
        using namespace std;
        struct point {
        int x;
        int y;
        int floor;
        int step;
        };
        int c;
        int n, m, v;
        char map1[15][15];
        int abookmap1[15][15];
        char map2[15][15];
        int abookmap2[15][15];
        int next1[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
        int main() {
        queue q;
        cin >> c;
        while (c--) {
        cin >> n >> m >> v;
        memset(abookmap1, 0, sizeof(abookmap1));
        memset(abookmap2, 0, sizeof(abookmap2));
        for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++) {
        cin >> map1[i][j];
        if (map1[i][j] == 'S') {
        q.push(point{i, j, 1, 0});
        map1[i][j] = '*';
        }
        }
        for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
        cin >> map2[i][j];
        bool found = false;
        while (!q.empty() && !found) {
        point t = q.front();
        q.pop();
        for (int i = 0; i < 4; i++) {
        int tx = t.x + next1[i][0];
        int ty = t.y + next1[i][1];
        if (tx < 1 || ty < 1 || tx > n || ty > m) continue;
        if ((t.floor == 1 && abookmap1[tx][ty] == 1) || (t.floor == 1 && map1[tx][ty] == '*')) continue;
        if ((t.floor == 2 && abookmap2[tx][ty] == 1) || (t.floor == 2 && map2[tx][ty] == '*')) continue;
        if (t.floor == 1) {
        if (map1[tx][ty] == '.') {
        q.push(point{tx, ty, 1, t.step + 1});
        abookmap1[tx][ty] = 1;
        }
        if (map1[tx][ty] == '#') {
        q.push(point{tx, ty, 2, t.step + 1});
        abookmap2[tx][ty] = 1;
        abookmap1[tx][ty] = 1;
        }
        if (map1[tx][ty] == 'P') {
        int temp = t.step + 1;
        if (temp <= v) {
        cout << "YES" << endl;
        found = true;
        break;
        }
        }
        }
        if (t.floor == 2) {
        if (map2[tx][ty] == '.') {
        q.push(point{tx, ty, 2, t.step + 1});
        abookmap2[tx][ty] = 1;
        }
        if (map2[tx][ty] == '#') {
        q.push(point{tx, ty, 1, t.step + 1});
        abookmap2[tx][ty] = 1;
        abookmap1[tx][ty] = 1;
        }
        if (map2[tx][ty] == 'P') {
        int temp = t.step + 1;
        if (temp <= v) {
        cout << "YES" << endl;
        found = true;
        break;
        }
        }
        }
        }
        }
        if (!found) cout << "NO" << endl;
        }
        return 0;
        }
        
        修正后的代码应该可以正确输出结果了。如果还有其他问题或者需要进一步解释,请随时提出。

      如果该回答解决了您的问题,请采纳!如果没有,请参考以下方案进行修订

      用户答题指南

      展开全部

      评论
    编辑
    预览

    报告相同问题?

  • 相关阅读:
    mybatis-plus 操作json字段
    CentOS8 安装 erlang 和 RabbitMQ
    【数据结构】队列
    技术分享 | App测试时常用的adb命令你都掌握了哪些呢?
    0112习题-函数与极限-高等数学
    中加石墨再冲刺港交所上市:2022年初至今收入为零,陈东尧为CEO
    golang笔记,slice,map底层
    mysql8压缩包安装
    2023-09-07 C++命名空间的一些陷阱
    intellij debug模式提示 : Method breakpoints may dramatically slow down debugging
  • 原文地址:https://ask.csdn.net/questions/8076497