gpt4 book ai didi

c++ - 来自多个来源的 BFS 上的 TLE

转载 作者:行者123 更新时间:2023-11-28 07:51:51 26 4
gpt4 key购买 nike

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

You are about to land on earth when you find out that someone has released some Velociraptors in the landing strip.
The landing trip has a rectangular shape. We mark with a dot (.)an empty spot, with a V the velociraptors and with a # the spots with obstacles on them (you cannot land on them).
It takes a second to the velociraptors to go to another spot, but they can only move horizontally and vertically.
You are asked to mark with an X those spots in which, landing on them, you would maximize your remaining lifetime.

我已经完成了一个算法,在该算法中我占据了迅猛龙的每个位置并在每个位置上进行了 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() 中的循环集成,而不是尝试以深度优先的方式寻找最短路径。

关于c++ - 来自多个来源的 BFS 上的 TLE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13617579/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com