动不了一点 2024-03-20 21:12 采纳率: 17.2%
浏览 4

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

请帮我看下下面的题我写的代码为什么没有输出,哪里写错了
题目:
可怜的公主在一次次被魔王掳走一次次被骑士们救回来之后,而今,不幸的她再一次面临生命的考验。魔王已经发出消息说将在T时刻吃掉公主,因为他听信谣言说吃公主的肉也能长生不老。年迈的国王正是心急如焚,告招天下勇士来拯救公主。不过公主早已习以为常,她深信智勇的骑士LJ肯定能将她救出。
现据密探所报,公主被关在一个两层的迷宫里,迷宫的入口是S(0,0,0),公主的位置用P表示,时空传输机用#表示,墙用表示,平地用.表示。骑士们一进入时空传输机就会被转到另一层的相对位置,但如果被转到的位置是墙的话,那骑士们就会被撞死。骑士们在一层中只能前后左右移动,每移动一格花1时刻。层间的移动只能通过时空传输机,且不需要任何时间。
Input
输入的第一行C表示共有C个测试数据,每个测试数据的前一行有三个整数N,M,T。 N,M迷宫的大小N
M(1 <= N,M <=10)。T如上所意。接下去的前NM表示迷宫的第一层的布置情况,后NM表示迷宫第二层的布置情况。
Output
如果骑士们能够在T时刻能找到公主就输出“YES”,否则输出“NO”。

样例输出:
1
5 5 14
S*#*.
.#...
.....
****.
...#.

...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() {
    queue<point>q;
    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;
                        }
                    }
                }
            }

            }
        
    }

}

  • 写回答

3条回答 默认 最新

  • 码农阿豪@新空间 新星创作者: 前端开发技术领域 2024-03-20 21:17
    关注
    让阿豪来帮你解答,本回答参考chatgpt3.5编写提供,如果还有疑问可以评论或留言
    问题:代码为什么没有输出,哪里写错了? 回答: 从代码的缩进来看,代码可能存在部分被裁剪的情况,无法完整运行。在存在问题的情况下,建议将完整的代码分享,有利于更好的发现问题所在。 同时,从代码中也可以看到一些错误,比如:
    • 在输入时没有将空格完全去掉,如“N,M”应为“N, M”;
    • 在定义next1数组时,第二维应为3而非2,如“int next1[4][3]”;
    • 在第一层的循环中,map1数组的下标范围为[1, n],但在第二层的循环中下标却为[1, m],可能导致程序崩溃;
    • 在判断是否跨层时,判断语句可能存在逻辑问题,可能导致队列永远无法清空。 为了更好的解答问题,建议提供完整的代码。同时,也可以参考以下代码进行改进:
    #include <iostream>
    #include <queue>
    #include <cstring>
    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<point> 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;
    }
    
    评论

报告相同问题?

问题事件

  • 创建了问题 3月20日