【搜索优化】ID/A*/IDA*(更新中)

【搜索优化】ID/A/IDA

题目积累

Big Banned Grid

https://atcoder.jp/contests/abc413/tasks/abc413_g
A*板子题

int h,w,k;
map<PII,bool> p;
struct node{
	int x,y,v;
	bool operator < (const node &b) const{
		return v>b.v;
	}
};
int get(int x,int y){
	int res=(h-x)+(w-y);
	return res;
}
void solve(){
    cin>>h>>w>>k;
	for(int i=1;i<=k;i++){
		int r,c;
		cin>>r>>c;
		p[{r,c}]=1;
	}
	//A*优化
    double now=clock();//时间
	priority_queue<node> q;//优先队列找更优的状态
	
	//其他就是正常的bfs模版
	q.push({1,1,get(1,1)});
	p[{1,1}]=1;
	while(q.size()){
	
		//注意这句
		if(clock()-now>=1.80*CLOCKS_PER_SEC) break;
		
		auto [x,y,val]=q.top();
		q.pop();
		if(!val){//走到终点 get出来的值为0
			cout<<"Yes"<<endl;
			return;
		}
		for(int i=0;i<4;i++){
			int nx=x+dx[i],ny=y+dy[i];
			if(nx<=0 || nx>h || ny<=0 || ny>w) continue;
			if(p[{nx,ny}]) continue;
			p[{nx,ny}]=1;
			q.push({nx,ny,get(nx,ny)});
		}
	}
	cout<<"No"<<endl;
}
posted @ 2025-07-06 20:42  White_ink  阅读(4)  评论(0)    收藏  举报