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

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

请帮我看下下面的题我写的代码为什么没有输出,哪里写错了
题目:
可怜的公主在一次次被魔王掳走一次次被骑士们救回来之后,而今,不幸的她再一次面临生命的考验。魔王已经发出消息说将在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”。

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;
                        }
                    }
                }
            }

            }
        
    }

}

  • 写回答

2条回答 默认 最新

  • 码农阿豪@新空间代码工作室 Java领域优质创作者 2024-03-20 21:22
    关注
    让阿豪来帮你解答,本回答参考chatgpt3.5编写提供,如果还有疑问可以评论或留言
    在你的代码中,缺少一部分。在最后一行,你没有结束这个if语句。你应该加上一个右大括号。下面是修改后的代码: #include #include #include 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 }); abookmap1[tx][ty] = 1; abookmap2[tx][ty] = 1; } } } } } // 缺少一对大括号来结束if语句 return 0; }
    评论

报告相同问题?

问题事件

  • 创建了问题 3月20日

悬赏问题

  • ¥15 微信小程序web-view嵌套H5页面IOS左滑会出现相同的页面,有什么解决方法吗?
  • ¥60 如何批量获取json的url
  • ¥15 comsol仿真压阻传感器
  • ¥15 Python线性规划函数optimize.linprog求解为整数
  • ¥15 llama3中文版微调
  • ¥15 pg数据库导入数据序列重复
  • ¥15 三分类机器学习模型可视化分析
  • ¥15 本地测试网站127.0.0.1 已拒绝连接,如何解决?(标签-ubuntu)
  • ¥50 Qt在release捕获异常并跟踪堆栈(有Demo,跑一下环境再回答)
  • ¥30 python,LLM 文本提炼