UVA 11624 UVA 10047 两道用 BFS进行最短路搜索的题

时间:2022-12-09 18:31:38

很少用bfs进行最短路搜索,实际BFS有时候挺方便得,省去了建图以及复杂度也降低了O(N*M);

UVA 11624 写的比较挫

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
struct node{
int ft;
int sta;
}flo[][];
int vis[][];
struct person{
int x,y,t,fx,fy;
};
int R,C;
int dir[][]={{,},{,-},{,},{-,}};
typedef pair<int,int> pill;
queue<pill> v;
queue<person> q;
void init()
{
memset(vis,,sizeof vis);
while (!v.empty()){
pill x=v.front();
v.pop();
for (int i=;i<;i++){
int nx=x.first+dir[i][];
int ny=x.second+dir[i][];
int tmp=flo[x.first][x.second].ft+;;
if (nx< || ny< || nx>=R || ny>=C) continue;
if (flo[nx][ny].ft>= && flo[nx][ny].ft<=tmp || flo[nx][ny].sta==) continue;
flo[nx][ny].ft=flo[x.first][x.second].ft+;
pill b=make_pair(nx,ny);
if (!vis[nx][ny])
v.push(b);
vis[nx][ny]=;
}
}
}
int bfs(person x)
{
memset(vis,,sizeof vis);
while (!q.empty()) q.pop();
q.push(x);
int s=<<;
while (!q.empty()){
person u=q.front();
q.pop();
if (u.t>=s) continue;
if (u.x== || u.y== || u.x==R- || u.y==C-) {s=u.t;break;}
for (int i=;i<;i++){
int xx=u.x+dir[i][];
int yy=u.y+dir[i][];
if (xx< || yy< || xx>=R || yy>=C) continue;
if (xx==u.fx && yy==u.fy) continue;
if (flo[xx][yy].sta!= || flo[xx][yy].ft>= && flo[xx][yy].ft<=u.t+) continue;
person b=(person){xx,yy,u.t+,u.x,u.y};
if (!vis[xx][yy]) q.push(b);
vis[xx][yy]=;
}
}
return s;
}
int main()
{
int t,sx,sy;char ch;
scanf("%d",&t);
while (t--){
while (!v.empty()) v.pop();
scanf("%d%d",&R,&C);
getchar();
for (int i=;i<R;i++){
for (int j=;j<C;j++){
scanf("%c",&ch);
//cout<<ch<<endl;
if (ch=='.') {flo[i][j].sta=;flo[i][j].ft=-;}
else if (ch=='#'){flo[i][j].sta=;flo[i][j].ft=-;}
else if (ch=='F'){
flo[i][j].sta=flo[i][j].ft=;
pill a;a.first=i;a.second=j;v.push(a);
}
else if (ch=='J') sx=i,sy=j;
}
getchar();
}
init();
person a=(person){sx,sy,,-,-};
int ans=bfs(a);
if (ans<(<<)) printf("%d\n",ans+);
else puts("IMPOSSIBLE");
}
return ;
}

UVA 10047

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
int R,C;
int vis[][][][];
int mat[][];
int sx,sy,ex,ey;
int dir[][]={{-,},{,},{,},{,-}};
struct t1{
int x,y,d,c,t;
};
int bfs(t1 a)
{
queue<t1> q;
q.push(a);
memset(vis,,sizeof vis);
while (!q.empty()){
t1 u=q.front();
q.pop();
if (u.x==ex && u.y==ey && u.c==){
//cout<<" pass "<<u.x<<" "<<u.y<<endl;
return u.t;
}
vis[u.x][u.y][u.d][u.c]=;
int nd=u.d+;
if (nd>) nd=;
t1 nx=u;
nx.d=nd;
nx.t=u.t+;
if (!vis[nx.x][nx.y][nx.d][nx.c]) q.push(nx);
vis[nx.x][nx.y][nx.d][nx.c]=;
nd=u.d-;
if (nd<) nd=;
nx=u; nx.d=nd; nx.t=u.t+;
if (!vis[nx.x][nx.y][nx.d][nx.c]) q.push(nx);
vis[nx.x][nx.y][nx.d][nx.c]=;
int xx=u.x+dir[u.d][];
int yy=u.y+dir[u.d][];
if (xx< || yy< || xx>=R || yy>=C) continue;
if (mat[xx][yy]==) continue;
int nc=u.c+;
if (nc>) nc=;
t1 b=(t1){xx,yy,u.d,nc,u.t+};
if (!vis[b.x][b.y][b.d][b.c]) q.push(b);
vis[b.x][b.y][b.d][b.c]=;
}
return -;
}
int main()
{
char ch;
int kase=;
while (scanf("%d%d",&R,&C)){
if (R==) break;
getchar();
memset(mat,,sizeof mat);
for (int i=;i<R;i++){
for (int j=;j<C;j++){
ch=getchar();
if (ch!='#') mat[i][j]=;
if (ch=='S') sx=i,sy=j;
if (ch=='T') ex=i,ey=j;
}
getchar();
}
//cout<<ex<<" exy "<<ey<<endl;
t1 a=(t1){sx,sy,,,};
int ans=bfs(a);
if (kase) puts("");
printf("Case #%d\n",++kase);
if (ans==-)puts("destination not reachable");
else printf("minimum time = %d sec\n",ans);
}
return ;
}