CSUOJ2031-Barareh on Fire(双向BFS)
Barareh on Fire
Submit Page Summary Time Limit: 3 Sec Memory Limit: 512 Mb Submitted: 102 Solved: 48
Description
The Barareh village is on fire due to the attack of the virtual enemy. Several places are already on fire and the fire is spreading fast to other places. Khorzookhan who is the only person remaining alive in the war with the virtual enemy, tries to rescue himself by reaching to the only helicopter in the Barareh villiage. Suppose the Barareh village is represented by an n × m grid. At the initial time, some grid cells are on fire. If a cell catches fire at time x, all its 8 vertex-neighboring cells will catch fire at time x + k. If a cell catches fire, it will be on fire forever. At the initial time, Khorzookhan stands at cell s and the helicopter is located at cell t. At any time x, Khorzookhan can move from its current cell to one of four edge-neighboring cells, located at the left, right, top, or bottom of its current cell if that cell is not on fire at time x + 1. Note that each move takes one second. Your task is to write a program to find the shortest path from s to t avoiding fire.
Input
There are multiple test cases in the input. The first line of each test case contains three positive integers n, m and k (1 ⩽ n,m,k ⩽ 100), where n and m indicate the size of the test case grid n × m, and k denotes the growth rate of fire. The next n lines, each contains a string of length m, where the jth character of the ith line represents the cell (i, j) of the grid. Cells which are on fire at time 0, are presented by character “f”. There may exist no “f” in the test case. The helicopter and Khorzookhan are located at cells presented by “t” and “s”, respectively. Other cells are filled by “-” characters. The input terminates with a line containing “0 0 0” which should not be processed.
Output
For each test case, output a line containing the shortest time to reach t from s avoiding fire. If it is impossible to reach t from s, write “Impossible” in the output.
Sample Input
7 7 2 f------ -f---f- ----f-- ------- ------f ---s--- t----f- 3 4 1 t--f --s- ---- 2 2 1 st f- 2 2 2 st f- 0 0 0
Sample Output
4 Impossible Impossible 1
Hint
题解:双向bfs即可,预处理
#include<iostream> #include<cstring> #include<cstdio> #include<queue> #include<cmath> #include<algorithm> using namespace std; struct Node{ int x,y,time; }; queue<Node> pq; int vis[110][110],fire[110][110]; int bfsx[8]={1,-1,0,0,1,1,-1,-1}; int bfsy[8]={0,0,1,-1,1,-1,1,-1}; int n,m,k,min_time; char Map[110][110]; void bfsa() { Node now,net; while(!pq.empty()) { now=pq.front(); pq.pop(); for(int i=0;i<8;i++) { net.x=now.x +bfsx[i]; net.y=now.y+bfsy[i]; net.time=now.time+k; if(net.x>=0&&net.x<n&&net.y>=0&&net.y<m&&!vis[net.x][net.y]) { pq.push(net); vis[net.x][net.y]=1; fire[net.x][net.y]=net.time; } } } } int bfsb() { Node now,net; while(!pq.empty()) { now=pq.front(); pq.pop(); for(int i=0;i<4;i++) { net.x=now.x +bfsx[i]; net.y=now.y+bfsy[i]; net.time=now.time+1; if(net.time<fire[net.x][net.y]&&net.x>=0&&net.x<n&&net.y>=0&&net.y<m&&!vis[net.x][net.y]) { if(Map[net.x][net.y]=='t') return net.time; vis[net.x][net.y]=1; pq.push(net); } } } return -1; } int main() { while(~scanf("%d%d%d",&n,&m,&k)) { if(n==0&&m==0&&k==0) break; int num=0; while(!pq.empty()) pq.pop(); memset(vis,0,sizeof(vis)); for(int i=0;i<n;i++) scanf("%s",Map[i]); Node s,t; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(Map[i][j]=='f') { fire[i][j]=0; vis[i][j]=1; pq.push(Node{i,j,0}); num++; } if(Map[i][j]=='s') s.x=i,s.y=j,s.time=0; if(Map[i][j]=='t') t.x=i,t.y=j; } } if(num==0) { int sum; sum=fabs(s.x-t.x)+fabs(s.y-t.y); cout<<sum<<endl; continue; } bfsa(); memset(vis,0,sizeof(vis)); while(!pq.empty()) pq.pop(); vis[s.x][s.y]=1; pq.push(s); int temp=bfsb(); if(temp!=-1) cout<<temp<<endl; else cout<<"Impossible"<<endl; } return 0; } /********************************************************************** Problem: 2031 User: song_hai_lei Language: C++ Result: AC Time:12 ms Memory:2256 kb **********************************************************************/
- Fire! 双向BFS追击问题
- CSUOJ 2031 Barareh on Fire
- HDU2389-Rain on your Parade(二分图匹配bfs模板)
- 704 - Colour Hash(双向bfs)
- (VUa 11624)Fire! -- BFS
- UVA 11624 Fire!【BFS】
- FZU Fire Game(两点BFS)
- UVA Fire!(两点BFS)
- Trees on the level UVA - 122 复习二叉树建立过程,bfs,queue,strchr,sscanf的使用。
- UVa 11624 Fire!(BFS)
- UVA - 11624 Fire!两个bfs
- hdu1430 魔板(双向bfs+输出路径+字典序最小)
- bfs UVA 12569 - Planning mobile robot on Tree (EASY Version)
- uva 122 Trees on the level 二叉树的层次遍历(bfs)
- UVA - 11624 Fire! (两次BFS)
- hdu 3085 Nightmare Ⅱ(双向bfs)
- pku 1077 Eight 经典8数码问题 单向BFS + A* BFS + 双向BFS
- a very weird phenomena base on Fire fox
- Uva11624 Fire(bfs 搜索)
- UVA 11624 Fire! 两次BFS求解