1 条题解

  • 0
    @ 2025-3-23 15:10:15

    进入 dfs 之前标记

    #include <bits/stdc++.h>
    using namespace std;
    int n, m, t;
    int sx, sy, fx, fy;
    bool g[10][10];
    int ans;
    bool vis[10][10];
    int dx[] = {0, 0, 1, -1};
    int dy[] = {1, -1, 0, 0};
    void dfs(int x, int y)
    {
        if (x == fx && y == fy)
        {
            ans++;
            return;
        }
        for (int i = 0; i <= 3; i++)
        {
            int xx = x + dx[i];
            int yy = y + dy[i];
            if (1 <= xx && xx <= n &&
                1 <= yy && yy <= m &&
                g[xx][yy] == false &&
                vis[xx][yy] == false)
            {
                vis[xx][yy] = true;
                dfs(xx, yy);
                vis[xx][yy] = false;
            }
        }
    }
    int main()
    {
        cin >> n >> m >> t;
        cin >> sx >> sy >> fx >> fy;
        for (int i = 1; i <= t; i++)
        {
            int x, y;
            cin >> x >> y;
            g[x][y] = true;
        }
        ans = 0;
        vis[sx][sy] = true;
        dfs(sx, sy);
        vis[sx][sy] = false;
        cout << ans;
        return 0;
    }
    

    进入 dfs 之后标记

    #include <bits/stdc++.h>
    using namespace std;
    int n, m, t;
    int sx, sy, fx, fy;
    bool g[10][10];
    int ans;
    bool vis[10][10];
    int dx[] = {0, 0, 1, -1};
    int dy[] = {1, -1, 0, 0};
    void dfs(int x, int y)
    {
        if (x == fx && y == fy)
        {
            ans++;
            return;
        }
        vis[x][y] = true;
        for (int i = 0; i <= 3; i++)
        {
            int xx = x + dx[i];
            int yy = y + dy[i];
            if (1 <= xx && xx <= n &&
                1 <= yy && yy <= m &&
                g[xx][yy] == false &&
                vis[xx][yy] == false)
            {
                dfs(xx, yy);
            }
        }
        vis[x][y] = false;
    }
    int main()
    {
        cin >> n >> m >> t;
        cin >> sx >> sy >> fx >> fy;
        for (int i = 1; i <= t; i++)
        {
            int x, y;
            cin >> x >> y;
            g[x][y] = true;
        }
        ans = 0;
        dfs(sx, sy);
        cout << ans;
        return 0;
    }
    
    • 1

    信息

    ID
    1833
    时间
    1000ms
    内存
    125MiB
    难度
    2
    标签
    递交数
    6
    已通过
    3
    上传者