求助!!!DFS 70分
  • 板块P1605 迷宫
  • 楼主YZZCDS
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/7/11 15:12
  • 上次更新2024/7/11 15:58:49
查看原帖
求助!!!DFS 70分
1112264
YZZCDS楼主2024/7/11 15:12

我用DFS写的,但是只能得到70分,在#2 #7 #8中WA了。我该怎么改???

#include <bits/stdc++.h>
using namespace std;
typedef long long l;
l n, m, t, ans;
l sx, sy, fx, fy;
bool ways[10][10];
l t1, t2;
l dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, -1, 1};

void dfs(l x, l y) {
    if (x == fx && y == fy) {
        ans ++;
        return;
    }
    l tx, ty;
    for (int i = 0; i < 4; i ++) {
        tx = x + dx[i], ty = y + dy[i];
        if (ways[tx][ty]) {
            ways[tx][ty] = false;
            dfs(tx, ty);
            ways[tx][ty] = true;
        }
    }
}

int main () {
    cin >> n >> m >> t;
    cin >> sx >> sy >> fx >> fy;
    for (int i = 1; i <= n; i ++) {
        for (int j = 1; j <= m; j ++) {
            ways[i][j] = true;
        }
    }
    for (int i = 0; i < t; i ++) {
        cin >> t1 >> t2;
        ways[t1][t2] = false;
    }
    dfs(sx, sy);
    cout << ans << endl;
    return 0;
}
2024/7/11 15:12
加载中...