来自多个来源的关于BFS的TLE

TLE on BFS from several sources

本文关键字:BFS TLE      更新时间:2023-10-16

我正在努力解决这个问题:http://olimpiada-informatica.org/?cmd=downloadE&pbm=velo101&ext=pdf它是西班牙语的,但我会尝试在这里翻译:

当你即将着陆时,你发现有人在着陆带释放了一些迅猛龙
着陆行程呈矩形。我们用一个点(.)标记一个空点,用一个V标记迅猛龙,用#标记上面有障碍物的点(你不能降落在它们上面)
迅猛龙需要一秒钟才能到达另一个地方,但它们只能水平和垂直移动
你被要求用X标记那些点,在这些点上着陆,你将最大限度地延长你的剩余寿命。

我已经做了一个算法,我取迅猛龙的每个位置,并在每个位置上进行BFS,但我得到了TLE,这是我的代码:

http://ideone.com/a6BVv3

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <utility>
using namespace std;
int cost[501][501],xx,yy,n,m;
char mat[501][501];
bool visit[501][501],first = true;
int a[] = {-1,0,0,1}, b[] = {0,-1,1,0};
void check(int x,int y,int level) {
cost[x][y] = level;
for(int i = 0; i < 4; ++i) {
xx = x + a[i];
yy = y + b[i];
if(0 <= xx and xx < n and 0 <= yy and yy < m and mat[xx][yy] == '.') {
if(!visit[xx][yy] or level + 1 < cost[xx][yy]) {
visit[xx][yy] = true;
check(xx,yy,level + 1);
}
}
}
}
int max() {
int r = -1;
for(int i = 0; i < n; ++i) for(int j = 0; j < m; ++j) if(mat[i][j] == '.') r = max(r,cost[i][j]);
return r;
}
void show() {
if(!first) puts("---");
int r = max();
for(int i = 0; i < n; ++i) {
for(int j = 0; j < m; ++j) {
if(cost[i][j] == r) printf("X");
else printf("%c",mat[i][j]);
}
puts("");
}
}
int main() {
while(scanf("%d %d",&n,&m) == 2) {
queue<pair<int,int> > cola;
for(int i = 0; i < n; ++i) {
scanf("n");
for(int j = 0; j < m; ++j) {
scanf("%c",&mat[i][j]);
if(mat[i][j] == 'V') cola.push(make_pair(i,j));
}
}
memset(cost,-1,sizeof cost);
memset(visit,0,sizeof visit);
while(!cola.empty()) {
pair<int,int> aux = cola.front();
visit[aux.first][aux.second] = true;
check(aux.first, aux.second,0);
cola.pop();
}
show();
first = false;
}
return 0;
}

有人知道我该如何改进我的算法吗?

编辑

好的,我已经解决了这个问题,如果有人感兴趣的话,下面是代码:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <utility>
using namespace std;
int cost[501][501],n,m;
char mat[501][501];
bool visit[501][501],first = true;
queue<pair<int,int> > cola;
int a[] = {-1,0,0,1}, b[] = {0,-1,1,0};
int max() {
int r = -1;
for(int i = 0; i < n; ++i) for(int j = 0; j < m; ++j) if(mat[i][j] == '.') r = max(r,cost[i][j]);
return r;
}
void show() {
if(!first) puts("---");
int r = max();
for(int i = 0; i < n; ++i) {
for(int j = 0; j < m; ++j) {
if(cost[i][j] == r) printf("X");
else printf("%c",mat[i][j]);
}
puts("");
}
}
int main() {
int cont = 0,x,y,xx,yy,level;
while(scanf("%d %d",&n,&m) == 2) {
for(int i = 0; i < n; ++i) {
scanf("n");
for(int j = 0; j < m; ++j) {
scanf("%c",&mat[i][j]);
if(mat[i][j] == 'V') cola.push(make_pair(i,j));
}
}
memset(cost,-1,sizeof cost);
memset(visit,0,sizeof visit);
while(!cola.empty()) {
int s_cola = cola.size();
for(int i = 0; i < s_cola; ++i) {
x = cola.front().first, y = cola.front().second;
cola.pop();
level = cost[x][y];
for(int i = 0; i < 4; ++i) {
xx = x + a[i], yy = y + b[i];
if(0 <= xx and xx < n and 0 <= yy and yy < m and mat[xx][yy] == '.') {
if(!visit[xx][yy] or level + 1 < cost[xx][yy]) {
visit[xx][yy] = true;
cost[xx][yy] = level + 1;
cola.push(make_pair(xx,yy));
}
}
}
}
}
show();
first = false;
}
return 0;
}

您正在check()中对整个图进行深度优先搜索。将其与main()中的循环集成,而不是尝试先找到最短路径深度。