UVA11624 Fire 两次BFS
来源:互联网 发布:mac系统怎么重装win10 编辑:程序博客网 时间:2024/06/05 03:14
题目就懒得粘了。。。
题意:给出一张图,J表示人的位置,F表示火的位置,.表示路,#表示墙。火每次向四个方向同时移动。问人能不能逃出迷宫,如果能,输出最短时间。
解题思路:理解了题意就很简单,不过有一点值得注意,这个题目可能有多个着火点,多个!!Wrong answer16次才发现!!然后就很简单了,把所有的起点推进队列进行处理就行了,然后再用个length数组存储火到达改点的最短时间。最后再对人进行BFS,走到一个点的时候判断到达此处的时间跟火到达此处的时间,直到跑到边界就可以跳出。以下是我的AC代码,length数组的初始化也跪了一次
#include <cstdio>#include <memory.h>#include <iostream>using namespace std;#include <queue>#define maxn 1005int m,n;int flag[maxn][maxn],success,lenth[maxn][maxn];char pic[maxn][maxn];int mm[4][2]= {{1,0},{0,1},{-1,0},{0,-1}};struct trip{ int x,y,step;};int check1(int x,int y){ if(x<0||x>=m||y<0||y>=n||pic[x][y]=='#'||pic[x][y]=='F') return 1; return flag[x][y];}int bfs1(){ memset(flag,0,sizeof(flag)); memset(lenth,0x3f3f3f3f,sizeof(lenth)); queue<trip> q; trip fff; for(int i=0; i<m; i++) for(int j=0; j<n; j++) if(pic[i][j]=='F') { fff.x=i; fff.y=j; fff.step=0; lenth[i][j]=0; q.push(fff); } trip now,next; while(!q.empty()) { now=q.front(); q.pop(); for(int i=0; i<4; i++) { next.x=now.x+mm[i][0]; next.y=now.y+mm[i][1]; if(check1(next.x,next.y)) continue; next.step=now.step+1; lenth[next.x][next.y]=next.step; flag[next.x][next.y]=1; q.push(next); } }}int check2(int x,int y){ if(pic[x][y]=='#'||x<0||x>=m||y<0||y>=n) return 1; return flag[x][y];}void bfs2(int r,int s){ queue<trip> q; trip now,next; now.x=r; now.y=s; now.step=0; q.push(now); while(!q.empty()) { now=q.front(); q.pop(); if(now.x==0||now.x==m-1||now.y==0||now.y==n-1) { printf("%d\n",now.step+1); success=1; return; } for(int i=0; i<4; i++) { next.x=now.x+mm[i][0]; next.y=now.y+mm[i][1]; if(check2(next.x,next.y)) continue; if(now.step+1>=lenth[next.x][next.y]) continue; next.step=now.step+1; flag[next.x][next.y]=1; q.push(next); } }}int main(){ //freopen("in.txt","r",stdin); int t; scanf("%d",&t); while(t--) { success=0; scanf("%d%d",&m,&n); for(int i=0; i<m; i++) scanf("%s",pic[i]); bfs1(); memset(flag,0,sizeof(flag)); for(int i=0; i<m; i++) for(int j=0; j<n; j++) { if(pic[i][j]=='J') { bfs2(i,j); break; } } if(!success) printf("IMPOSSIBLE\n"); } return 0;}
阅读全文
0 0
- UVA11624-Fire!(两次bfs)
- UVA11624 Fire 两次BFS
- UVA11624 Fire! 两次BFS 读懂题意很重要
- UVA11624 Fire! 两次BFS(J)
- [bfs] UVA11624-Fire!
- UVA11624 Fire BFS
- uva11624 Fire! (双bfs)
- UVA11624 Fire! BFS
- UVA11624 Fire!【BFS+细心】
- UVa11624-Fire!(bfs)
- Uva11624 Fire(bfs 搜索)
- uva11624 fire bfs 最短路
- UVa11624 Fire!(BFS+迷宫)
- UVA11624:Fire!(BFS + 优化)
- UVA11624 Fire! —— BFS
- UVA11624 Fire! (两点广搜BFS)
- fire!--两次bfs
- Fire! UVA11624
- Linux 下cut用法总结
- POJ
- 并发Queue一
- hibernate学习笔记(一)
- 多线程总结
- UVA11624 Fire 两次BFS
- Java程序-1
- JSP&Servlet jsp(2)
- SimpleDateFormat 使用注意事项
- linux下boost库的安装及使用
- 连接池
- Could not resolve hostname t143ghl: Name or service not known lost connection
- 配置yum仓库
- Codeforces 780C-Andryusha and Colored Balloons dfs