博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CSUOJ2031-Barareh on Fire(双向BFS)
阅读量:5364 次
发布时间:2019-06-15

本文共 2929 字,大约阅读时间需要 9 分钟。

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 2f-------f---f-----f---------------f---s---t----f-3 4 1t--f--s-----2 2 1stf-2 2 2stf-0 0 0

Sample Output

4ImpossibleImpossible1

Hint

 

题解:双向bfs即可,预处理

 

1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7 using namespace std; 8 9 struct Node{ 10 int x,y,time; 11 }; 12 13 queue
pq; 14 int vis[110][110],fire[110][110]; 15 int bfsx[8]={ 1,-1,0,0,1,1,-1,-1}; 16 int bfsy[8]={ 0,0,1,-1,1,-1,1,-1}; 17 int n,m,k,min_time; 18 char Map[110][110]; 19 20 void bfsa() 21 { 22 Node now,net; 23 while(!pq.empty()) 24 { 25 now=pq.front(); 26 pq.pop(); 27 for(int i=0;i<8;i++) 28 { 29 net.x=now.x +bfsx[i]; 30 net.y=now.y+bfsy[i]; 31 net.time=now.time+k; 32 if(net.x>=0&&net.x
=0&&net.y
=0&&net.x
=0&&net.y
View Code

 

 

  

 

转载于:https://www.cnblogs.com/songorz/p/9386523.html

你可能感兴趣的文章
Laravel Homestead: 403 forbidden on nginx, http://homestead.app访问不了
查看>>
SQLServer 的case when语句使用实现统计
查看>>
如何得到GridView中某行某列的值?
查看>>
Java 代码性能优化
查看>>
【一维RMQ】HDU-3183
查看>>
ConcurrentHashMap原理分析
查看>>
CountDownLatch、信号量
查看>>
在IE8及以下的浏览器中,不支持placeholder属性的解决办法
查看>>
C++内存管理的缩影
查看>>
OAuth 及 移动端鉴权调研
查看>>
C# 虹软SDK视频人脸识别和注册
查看>>
ajax传递数组到后台,js传递数组到后台
查看>>
[数据结构]之顺序表
查看>>
提取hdfs文件名的方法
查看>>
Ubuntu 17.04 upgrade to 17.10
查看>>
Windows access Linux / Ubuntu via Remote Desktop via xrdp
查看>>
程序员都应该知道的福利
查看>>
反射-------通过反射跳过泛型编译器运行报异常的问题答案
查看>>
二叉链表(双叉链表)实现二叉树
查看>>
javascript保留字趣史
查看>>