UVA 11624 UVA 10047 两道用 BFS进行最短路搜索的题
很少用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 ;
}
UVA 11624 UVA 10047 两道用 BFS进行最短路搜索的题的更多相关文章
- UVA 11624 Fire!(两次BFS+记录最小着火时间)
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- UVA 11624 Fire!【两点BFS】
Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of the m ...
- UVa 11624,两次BFS
题目链接:http://vjudge.net/contest/132239#problem/A 题目链接:https://uva.onlinejudge.org/external/116/11624. ...
- Fire! UVA - 11624 (两步bfs)
题目链接 题意 人要从迷宫走出去,火会向四个方向同时扩散 分析 两步bfs,先出火到达各地时的时间(设初始时间为0,人每走一步为1s,在着一步内火可以向四周可触及的方向同时扩散),然后在bfs人,人能 ...
- UVA - 11624 Fire! bfs 地图与人一步一步先后搜/搜一次打表好了再搜一次
UVA - 11624 题意:joe在一个迷宫里,迷宫的一些部分着火了,火势会向周围四个方向蔓延,joe可以向四个方向移动.火与人的速度都是1格/1秒,问j能否逃出迷宫,若能输出最小时间. 题解:先考 ...
- BFS(两点搜索) UVA 11624 Fire!
题目传送门 /* BFS:首先对火搜索,求出火蔓延到某点的时间,再对J搜索,如果走到的地方火已经烧到了就不入队,直到走出边界. */ /******************************** ...
- E - Fire! UVA - 11624(bfs + 记录火到达某个位置所需要的最小时间)
E - Fire! UVA - 11624 题目描述 乔在迷宫中工作.不幸的是,迷宫的一部分着火了,迷宫的主人没有制定火灾的逃跑计划.请帮助乔逃离迷宫.根据乔在迷宫中的位置以及迷宫的哪个方块着火,你必 ...
- UVA 11624 - Fire! 图BFS
看题传送门 昨天晚上UVA上不去今天晚上才上得去,这是在维护么? 然后去看了JAVA,感觉还不错昂~ 晚上上去UVA后经常连接失败作死啊. 第一次做图的题~ 基本是照着抄的T T 不过搞懂了图的BFS ...
- UVa 11624 (BFS) Fire!
也是一个走迷宫的问题,不过又有了点变化. 这里迷宫里有若干把火,而且火每秒也是向四个方向蔓延的.问人是否能走出迷宫. 我用了两遍BFS,第一遍把所有着火的格子加入队列,然后计算每个格子着火的时间. 第 ...
随机推荐
- Task使用注意
1. CancellationTokenSource的使用 https://binary-studio.com/2015/10/23/task-cancellation-in-c-and-things ...
- Golang:GOROOT、GOPATH、GOBIN变量的含义
背景 移植完了go以后,在配置变量的时候不太清楚这些变量的含义,找了有关的资料. 使用 go env可以获取 go 有关的环境变量,下面是我的go环境: GO111MODULE="" ...
- 扩展的Sobel 算子
Custom Extended Sobel Filters https://arxiv.org/pdf/1910.00138.pdf sobel算子是进行边缘检测的一个重要算子.它通常是一个3x3的 ...
- SqlServer查看锁表与解锁
某些情况下,sqlserver的表会被锁住,比如某个会话窗口有数据一直没提交,窗口又没关闭,这时表就会被锁住 其他任何连接查询表数据时都不会返回 这时需要手工杀掉产生死锁的会话ID,才能恢复正常 查看 ...
- 2018年第九届蓝桥杯【C++省赛B组】(未完)
第一题 标题:第几天 2000年的1月1日,是那一年的第1天. 那么,2000年的5月4日,是那一年的第几天? 注意:需要提交的是一个整数,不要填写任何多余内容 利用excel更加快捷: 答案是125 ...
- Day7 - A - Visible Lattice Points POJ - 3090
A lattice point (x, y) in the first quadrant (x and y are integers greater than or equal to 0), othe ...
- 将xml字符串的所有叶标签转换成Map集合
实际问题:对方服务器接口采用webservice方式返回xml报文,现需解析xml获取所有叶节点的标签名及携带的值 解决方案:利用dom4j解析xml并利用递归获取叶节点,将标签名及标签值封装到Map ...
- SQL添加列、非空、默认值
use MarcoBarcode go alter table [dbo].[WorkOrderRepairSheet] ADD needRepair int go ALTER TABLE [dbo] ...
- 学生选课数据库MySQL语句练习题45道
1. 查询Student表中的所有记录的Sname.Ssex和Class列. select Sname,Ssex,Class from Student;2. 查询教师所有的单位即不重复的Depart列 ...
- System.Data.SqlClient.SqlException: 'Incorrect syntax near 'OFFSET'.
https://www.nopcommerce.com/boards/t/54586/410-not-running-on-local-system.aspx#209684 Hello, I was ...