来自多个来源的关于BFS的TLE
TLE on BFS from several sources
我正在努力解决这个问题: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()中的循环集成,而不是尝试先找到最短路径深度。
相关文章:
- 如果我只是不访问queue_front节点的子节点,而是将它们推到队列中呢?还是BFS吗
- 如何用C++编写BFS函数
- 使用 bfs 解决连接组件问题时得到错误的答案
- 使用 BFS 的单词梯 2
- 在 c++ 中执行 BFS 时出现分段错误
- BFS 打印最短路径
- 如何打印 BFS 路径本身而不是此单词梯的路径长度?
- 多源 BFS 多线程
- 简化代码以避免在顺时针旋转2D矩阵时出现TLE
- Node中的子节点列表在C++中的BFS遍历过程中丢失
- BFS遍历给出错误的输出
- 如何在BFS(C++)期间删除队列中的对象
- 为什么在某些情况下,我在这个问题上会得到TLE?
- 如何在这样的代码中避免 TLE?
- 谁能解释一下这个BFS代码是如何工作的?
- DFS + 记忆解决方案在 LeetCode 上获取 TLE
- DFS在kefa和park中给予TLE(代码560C)
- 使用BFS存储和打印最短路径
- BFS 方法中的 TLE for SPOJ AKBAR
- 来自多个来源的关于BFS的TLE