蒟蒻求助
查看原帖
蒟蒻求助
567002
Zhangky2021楼主2023/1/5 15:55

4-10MLE,该怎么优化广搜的队列空间啊

#include<bits/stdc++.h>
using namespace std;
int n, k;
char a[500][500];
struct pos{
	int x, y, cost, w;
	pos (int ax, int ay, int acost, int aw){
		x=ax, y=ay, cost=acost, w=aw;
	}
};
int bfs(int x, int y, int cost, int w){
	queue <pos> q;
	q.push(pos(x, y, cost, w));
	while(!q.empty()){
		pos now=q.front();
	    q.pop();
	    int x=now.x, y=now.y, cost=now.cost, w=now.w;
	    if(x<1||x>n||y<1||y>n||a[x][y]=='#'||w<1&&a[x][y]=='X')continue;
	    if(x==n&&y==n)return cost;
	    if(a[x][y]=='%'){
	    a[x][y]='.';
		q.push(pos(x+1, y, cost+1, k));
		q.push(pos(x-1, y, cost+1, k));
		q.push(pos(x, y+1, cost+1, k));
	   	q.push(pos(x, y-1, cost+1, k));
		}
		q.push(pos(x+1, y, cost+1, w-1));
		q.push(pos(x-1, y, cost+1, w-1));
		q.push(pos(x, y+1, cost+1, w-1));
		q.push(pos(x, y-1, cost+1, w-1));
	}
	return -1;
}
int main(){
    cin>>n>>k;
    for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			cin>>a[i][j];
		}
	}
	cout<<bfs(1, 1, 0, 0);
}
2023/1/5 15:55
加载中...