博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
2153: 2018湖南多校第二场-20180407(网络同步赛)
阅读量:6193 次
发布时间:2019-06-21

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

E(): Barareh on Fire

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
#include 
#include
#include
#include
#define MAX 1100using namespace std;#define ll long longchar mp[150][150];int fx[10]= {-1,0,1,0,-1,1,-1,1};int fy[10]= {
0,1,0,-1,1,1,-1,-1};int t,n,m, k;int x,y;int x2,y2;int x3,y3;bool flag;int tim[150][150];int vis[150][150];int ans[150][150];struct node{ int x,y;}p;queue
q;void bfs1(){ while(!q.empty()) { p=q.front(); q.pop(); vis[p.x][p.y]=1; for(int i=0; i<8; i++) //火势八方向蔓延 { int xx=p.x+fx[i]; int yy=p.y+fy[i]; if(xx>=1&&xx<=n&&yy>=1&&yy<=m) { if(vis[xx][yy]!=1)q.push((node){xx,yy}); if(tim[xx][yy]>tim[p.x][p.y]+k) tim[xx][yy]=tim[p.x][p.y]+k; //all its 8 vertex-neighboring cells will catch fire at time x + k vis[xx][yy]=1; } } }}void bfs2(int x,int y){ memset(vis,0,sizeof(vis)); memset(ans,0,sizeof(ans)); queue
q; vis[x][y]=1; q.push((node){x,y}); while(!q.empty()) { p=q.front(); q.pop(); for(int i=0; i<4; i++) { int xx=p.x+fx[i]; int yy=p.y+fy[i]; if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&vis[xx][yy]==0)//left, right, top, or bottom of its current cell if that cell is not on fire at time x + 1 { vis[xx][yy]=1; if(mp[xx][yy]=='-') //空地 { if(ans[p.x][p.y]+1 < tim[xx][yy]) \ { q.push((node){xx,yy}); ans[xx][yy]=ans[p.x][p.y]+1; } } if(mp[xx][yy]=='t') { if(ans[p.x][p.y]+1 < tim[xx][yy])//当人和火同时到门的时候,算火胜利 { ans[xx][yy]=ans[p.x][p.y]+1; flag=true; } return ; } } } }}int main(){ while(~scanf("%d%d%d",&n,&m,&k),n,m,k) { for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) tim[i][j] = 9999999; //printf("\n"); } memset(vis,0,sizeof(vis)); memset(ans,0,sizeof(ans)); flag=false; for(int i=1; i<=n; i++) { scanf("%s",mp[i]+1); for(int j=1; j<=m; j++) { if(mp[i][j]=='s') { x=i; y=j; } if(mp[i][j]=='f') { tim[i][j]=0; x2=i; y2=j; q.push(node{x2,y2}); } if(mp[i][j]=='t') { x3=i; y3=j; } } } bfs1(); /* for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) cout<
<<" "; printf("\n"); }*/ bfs2(x,y); if(flag) printf("%d\n",ans[x3][y3]); else printf("Impossible\n"); }}
两次BFS

 

转载于:https://www.cnblogs.com/Roni-i/p/8761677.html

你可能感兴趣的文章
用jmeter进行接口压力测试的步骤
查看>>
Delphi Listveiw用法大全
查看>>
C#的预处理指令的全局设计
查看>>
Dynamips结合VMware搭建站点到站点×××环境
查看>>
arm指令中mov和ldr有什么区别
查看>>
写Java程序的三十个基本规则
查看>>
我的友情链接
查看>>
004 查看表结构命令
查看>>
Exchange 2016 CU9 已发布
查看>>
java jackson json序列化
查看>>
CP(1)
查看>>
redhat7.2升级openssl、openssh
查看>>
Gson自动解析json
查看>>
[备忘]如何接收向shell脚本传入的参数
查看>>
Wine里的中文程序出现方块字的解决方法以及Wine快捷方式的命令格式
查看>>
xendesktop配置DDC连接vcenter,添加vcenter证书步骤。
查看>>
12月流量入口占比动态:搜索引擎季军 份额破20%
查看>>
2月中国域名服务商TOP20市场份额解析(图)
查看>>
mybatis 对枚举类型的自动转换
查看>>
代码中banner文字
查看>>