洛谷 P1605 迷宫

暴搜就完了,不能记忆化。

#include<iostream>
#include<cstring>
using namespace std;
struct node{
    int x, y;
};
int N, M, T;
node start, ed;
int dirtX[5] = {0, -1, 0, 1, 0}, dirtY[5] = {0, 0, 1, 0, -1};
bool wall[10][10], st[10][10];

int dfs(int x, int y){
    if(x < 1 || x > N || y < 1 || y > M || wall[x][y]) return 0;
    if(x == ed.x && y == ed.y) return 1;
    int res = 0;
    st[x][y] = true;
    for(int i = 1; i <= 4; i++){
    	if(st[x + dirtX[i]][y + dirtY[i]]) continue;
        res += dfs(x + dirtX[i], y + dirtY[i]);
    }
    st[x][y] = false;
    return res;
}

int main(){
    cin >> N >> M >> T;
    int sx, sy, tx, ty;
    cin >> sx >> sy >> tx >> ty;
    start = {sx, sy};
    ed = {tx, ty};
    while(T--){
        int x, y;
        cin >> x >> y;
        wall[x][y] = true;
    }
    cout << dfs(start.x, start.y);
    return 0;
}
posted @ 2025-05-28 23:09  Yuhhhhh  阅读(11)  评论(0)    收藏  举报