B3625 迷宫寻路60分求调

B3625 迷宫寻路

jiangmuran @ 2023-03-11 19:03:16

RT

#include <iostream>

using namespace std;

int n, m; // n是y!!!m是x!!!
bool map[110][110], run[110][110], flag; // map为地图,0为无墙,1为有墙
// run为是否用过

bool hf(int x, int y) {
    if (x >= m || x < 0 || y >= n || y < 0 || run[x][y] || map[x][y]) {
        return 0;
    }
    return 1;
}

void dfs(int x, int y) {
    if (flag) {
        return;
    }
    if (x == m - 1 && y == n - 1) {
        flag = 1;
        return;
    }
    for (int i = -1; i <= 1; i += 1) {
        for (int j = -1; j <= 1; j += 1) {
            if (hf(x + i, y + j)) {
                run[x + i][y + j] = 1;
                dfs(x + i, y + j);
            }
        }
    }
}

int main() {
    cin >> n >> m;
    char tmp;
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            cin >> tmp;
            if (tmp == '#') {
                map[i][j] = 1;
            } else {
                map[i][j] = 0;
            }
        }
    }
    dfs(0, 0);
    if (flag) {
        cout << "Yes" << endl;
        return 0;
    }
    cout << "No" << endl;
}

by _weishiqi66_ @ 2023-03-11 19:28:42

bool hf(int x, int y) {
    if (x >= m || x < 0 || y >= n || y < 0 || run[x][y] || map[x][y]) {
        return 0;
    }
    return 1;
}

写反了,越界&&不符合条件才应该return 1


by _weishiqi66_ @ 2023-03-11 19:32:50

@ben_ren_yi_tui_gu


by jiangmuran @ 2023-03-11 19:55:19

@weishiqi66 谢谢,还发现一个问题,25行两个for也有问题


|