1 solutions

  • 0
    @ 2025-11-19 9:21:17

    BFS+暴力if判断

    #include <iostream>
    #include <queue>
    #include <set>
    #include <unordered_map>
    
    using namespace std;
    
    void func(set<pair<int,int>> & res , queue<pair<int,int>>& q, int n);
    
    int main(){
        int n,k,x,y;
        cin>>n>>k>>x>>y;
        set<pair<int,int>> res;
        queue<pair<int,int>> q;
        q.push({x,y});
        res.insert({x,y});
        while(k-- && !q.empty()){
            func(res,q,n);
        }
        cout<<res.size()<<endl;
    }
    
    void func(set<pair<int,int>>& res , queue<pair<int,int>>& q,int n){
        int it = q.size();
        if(it == 0) return;
    
        while(it--){
        int a = q.front().first;
        int b = q.front().second;
        q.pop();
        if(b+2<=n){
            if(a-1 >= 1 && !res.count({a-1,b+2})){
                res.insert({a-1,b+2});
                q.push({a-1,b+2});
            }
            if(a+1 <= n && !res.count({a+1,b+2})){
                res.insert({a+1,b+2});
                q.push({a+1,b+2});
            }
        }
        if(b+1<=n){
            if(a-2 >= 1 && !res.count({a-2,b+1})){
                res.insert({a-2,b+1});
                q.push({a-2,b+1});
            }
            if(a+2 <= n && !res.count({a+2,b+1})){
                res.insert({a+2,b+1});
                q.push({a+2,b+1});
            }
        }
        if(b-1>=1){
            if(a-2 >= 1 && !res.count({a-2,b-1})){
                res.insert({a-2,b-1});
                q.push({a-2,b-1});
            }
            if(a+2 <= n && !res.count({a+2,b-1})){
                res.insert({a+2,b-1});
                q.push({a+2,b-1});
            }
        }
        if(b-2>=1){
            if(a-1 >= 1 && !res.count({a-1,b-2})){
                res.insert({a-1,b-2});
                q.push({a-1,b-2});
            }
            if(a+1 <= n && !res.count({a+1,b-2})){
                res.insert({a+1,b-2});
                q.push({a+1,b-2});
            }
        }
    }
    }
    
    • @ 2025-11-19 9:22:29

      此代码在该平台AC 100分

  • 1

Information

ID
261
Time
1000ms
Memory
512MiB
Difficulty
2
Tags
# Submissions
333
Accepted
60
Uploaded By