其他分享
首页 > 其他分享> > 021-2022-INES A. Armor and Weapons Solution(玄学剪枝,限界)

021-2022-INES A. Armor and Weapons Solution(玄学剪枝,限界)

作者:互联网

LINK

#include<bits/stdc++.h>
#include <unordered_set>

#define int long long
using namespace std;
typedef pair<int, int> PII;
typedef pair<PII, int> PIII;
#define x first
#define y second

const int N = 2e5+10;
unordered_map<int,bool>ust;
int n,m,qy;
int cnt1=20,cnt2=20;//玄学剪枝
queue<pair<PII,int>>q;
int maxx[2*N],maxy[2*N];
int bfs(){
    int res=1e18;
    q.push({{1,1},0});
    while(q.size()){
        auto now=q.front();
        q.pop();
        int x=now.x.x,y=now.x.y,sp=now.y;
        if(sp>=res)break;
        if(x<=maxx[sp]&&y<=maxy[sp])continue;
        if(x>maxx[sp]&&y>maxy[sp])maxx[sp]=x,maxy[sp]=y;
        if(x==n&&y==m){
            res=min(res,sp);
            break;
        }
        int add=0;
        if(ust[x*N+y]==1)add=1;
        if(x==n){
            cnt1--;
            if(cnt1>=0){
               while(y<m){
                   sp++;
                   if(ust[x*N+y]==1)add=1;else add=0;
                   y+=n+add;
               }
               res=min(res,sp);
            }
            continue;
        }
        if(y==m){
            cnt2--;
            if(cnt2>=0){
               while(x<n){
                   sp++;
                   if(ust[x*N+y]==1)add=1;else add=0;
                   x+=m+add;
               } 
               res=min(res,sp);
            }
            continue;
        }
        q.push({{min(x+y+add,n),y},sp+1});
        q.push({{x,min(x+y+add,m)},sp+1});
    }
    return res;
}
signed main()
{
    scanf("%lld%lld",&n,&m);
    scanf("%lld",&qy);
    while(qy--){
        int a,b;
        scanf("%lld%lld",&a,&b);
        ust[a*N+b]=1;
    }
    printf("%lld",bfs());
}

标签:剪枝,maxy,Armor,res,sp,int,cnt1,now,限界
来源: https://www.cnblogs.com/codjjj/p/15938554.html