这道题哪里错了?(C++)

题目描述
小H在一个划分成了n*m个方格的长方形封锁线上。 每次他能向上下左右四个方向移动一格(当然小H不可以静止不动), 但不能离开封锁线,否则就被打死了。 刚开始时他有满血6点,每移动一格他要消耗1点血量。一旦小H的 血量降到 0, 他将死去。 他可以沿路通过拾取鼠标(什么鬼。。。)来补满血量。只要他走到有鼠标的格子,他不需要任何时间即可拾取。格子上的鼠标可以瞬间补满,所以每次经过这个格子都有鼠标。就算到了某个有鼠标的格子才死去, 他也不能通过拾取鼠标补满 HP。 即使在家门口死去, 他也不能算完成任务回到家中。

地图上有 5 种格子:

数字 0: 障碍物。

数字 1: 空地, 小H可以自由行走。

数字 2: 小H出发点, 也是一片空地。

数字 3: 小H的家。

数字 4: 有鼠标在上面的空地。

小H能否安全回家?如果能, 最短需要多长时间呢?

输入格式
第一行两个整数n,m, 表示地图的大小为n*m。

下面 n 行, 每行 m 个数字来描述地图。

输出格式
一行, 若小H不能回家, 输出-1,否则输出他回家所需最短时间。

输入输出样例
输入 #1复制
3 3
2 1 1
1 1 0
1 1 3
输出 #1复制
4
说明/提示
1<=n,m<=9

#include <iostream>
#include <cstring>
using namespace std;
int x1, y1;
int x2, y2;
int n, m;
int min = 0x7f7f7f7f;
int G[15][15];
bool A[15][15];
int HP = 6;
int ans = 0;
int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};
int dfs(int x, int y, int hp, int ANS) {
    hp--;
    if (hp <= 0) {
        return -1;
    }
    if (G[x][y] == 3) {
        return ans;
    }
    if (G[x][y] == 4) {
        hp = 6;
    }
    ans++;
    for (int i = 0; i < 4; i++) {
        int tx = x + dir[i][0];
        int ty = y + dir[i][1];
        if (A[tx][ty] == true && tx >= 1 && tx <= n && ty >= 1 && ty <= m) {
            A[tx][ty] = false;
            dfs(tx, ty, hp, ans);
            A[tx][ty] = true; 
        }
    }
}
int main() {
    memset(A, true, sizeof(A));
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            cin >> G[i][j];
            if (G[i][j] == 0) {
                A[i][j] = false;
            }
            if (G[i][j] == 2) {
                x1 = i;
                x2 = j;
            }
            if (G[i][j] == 3) {
                x2 = i;
                x2 = j;
            }
        }
    }
    cout << dfs(x1, y1, HP, ans) << endl;
    return 0;
}

为什么样例输出是4,而我的是3


#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int x1, y1;
int x2, y2;
int n, m;
int MIN = 0x7f7f7f7f;
int G[15][15];
int A[15][15];
int HP = 6;
int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};
void dfs(int x, int y, int hp, int ANS) {
    if (hp <= 0) {
        return ;
    }
    if (G[x][y] == 3) {
        MIN = min(MIN, ANS);
        return ;
    }
    if (G[x][y] == 4) {
        hp = 6;
    }
    ANS++;
    for (int i = 0; i < 4; i++) {
        int tx = x + dir[i][0];
        int ty = y + dir[i][1];
        if (G[tx][ty] != 0 && A[tx][ty] <= 1 && tx >= 1 && tx <= n && ty >= 1 && ty <= m) {
            A[tx][ty]++;
            dfs(tx, ty, hp - 1, ANS);
            A[tx][ty]--; 
        }
    }
}
int main() {
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            cin >> G[i][j];
            if (G[i][j] == 0) {
                A[i][j] = false;
            }
            if (G[i][j] == 2) {
                x1 = i;
                y1 = j;
            }
            if (G[i][j] == 3) {
                x2 = i;
                y2 = j;
            }
        }
    }
    dfs(x1, y1, HP, 0);
    if(MIN == 0x7f7f7f7f) MIN = -1;
    cout << MIN << endl;
    return 0;
}

你好,我是有问必答小助手,本次您提出的有问必答问题,您已经自行解决。

本次提问扣除的有问必答次数,已经为您补发到账户,我们后续会持续优化,扩大我们的服务范围,为您带来更好地服务。