【语法小技巧】重载运算符

从小到大排:
struct node{
	int id,dep;ll t,ans;
	bool operator <(const node &a) const{
		return t>a.t;	
	}
}p[maxn];
/*
bool operator <(node a,node b){
	return a.t>b.t;
}
*/
从大到小排:
struct node{
	int id,dep;ll t,ans;
	bool operator <(const node &a) const{
		return t<a.t;	
	}
}p[maxn];
/*
bool operator <(node a,node b){
	return a.t<b.t;
}
*/

以cf1063B为例

#include<bits/stdc++.h>
#define ll long long
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define per(i,n,a) for(int i=n;i>=a;i--)
#define endl '\n'
#define mem(a) memset(a,0,sizeof(a))
#define IO ios::sync_with_stdio(false);cin.tie(0);
using namespace std;
const int INF=0x3f3f3f3f;
const ll inf=0x3f3f3f3f3f3f3f3f;
const int mod=1e9+7;
const int maxn=2000+5;
struct node{
	int x,y,l,r;
	bool operator <(const node &a) const{
		if(l==a.l) return r<a.r;
		return l<a.l;
	}
};
int dx[4]={-1,0,0,1};
int dy[4]={0,1,-1,0};
int vis[maxn][maxn];
char a[maxn][maxn];
int n,m,X,Y,L,R,cnt;
bool check(int x,int y){
    return x>=1 && x<=n && y>=1 && y<=m && !vis[x][y] && a[x][y]!='*';
}
void bfs(){
	priority_queue<node> q;
	q.push(node{X,Y,L,R});
	cnt+=1;vis[X][Y]=1;
	while(!q.empty()){
		node t=q.top();q.pop();
		for(int i=0;i<4;i++){
           	int tx=t.x+dx[i];
            int ty=t.y+dy[i];
            if(!check(tx,ty)){
                continue;
            }
            if(i==0 || i==3){
                cnt++;
                vis[tx][ty]=true;
                q.push(node{tx,ty,t.l,t.r});
            }
            else if(i==1){
                if(t.r>=1){
                    cnt++;
                    vis[tx][ty]=true;
                    q.push(node{tx,ty,t.l,t.r-1});
                }
            }
            else if(i==2){
                if(t.l>=1){
                    cnt++;
                    vis[tx][ty]=true;
                    q.push(node{tx,ty,t.l-1,t.r});
                }
            }
		}
	}
}	
int main(){
	cin>>n>>m;
	cin>>X>>Y;
	cin>>L>>R;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=m;j++)
			cin>>a[i][j];
	bfs();
	cout<<cnt<<endl;
}

注意,重载运算operator只和priority_queue搭配的时候,与一般排序定义相反。
否则和普通排序一样

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值