博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Barareh on Fire
阅读量:6260 次
发布时间:2019-06-22

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

5431: Barareh on Fire

时间限制: 1 Sec  内存限制: 128 MB
提交: 321  解决: 62
[] [] [] [命题人:]

题目描述

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.

 

输入

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.

 

输出

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.

 

样例输入

7 7 2f-------f---f-----f---------------f---s---t----f-3 4 1t--f--s-----2 2 1stf-2 2 2stf-0 0 0

 

样例输出

4ImpossibleImpossible1

 

来源/分类

    

 

1 #include 
2 #include
3 #include
4 #include
5 #include
6 #include
7 using namespace std; 8 const int inf=0x3f3f3f3f; 9 const int maxn=110;10 char mat[maxn][maxn];11 int fire[maxn][maxn];12 int n,m,k,go[8][2]={ {-1,0},{ 1,0},{ 0,-1},{ 0,1},{-1,-1},{-1,1},{ 1,-1},{ 1,1}};13 struct node14 {15 int x,y,step;16 node()17 {18 step=0;19 }20 }now,nx;21 22 bool check(int x,int y){23 if(x>=0&&x
=0&&y
q;30 q.push(now);31 while(!q.empty()){32 now=q.front();33 q.pop();34 // if(now.step/k>max(n,m)) return ;35 for (int i=0;i<8;i++){36 int fx=now.x+go[i][0],fy=now.y+go[i][1];37 if(check(fx,fy)&&mat[fx][fy]!='f'&&fire[fx][fy]>now.step+k){38 fire[fx][fy]=now.step+k;39 nx.x=fx,nx.y=fy,nx.step=now.step+k;40 q.push(nx);41 42 }43 }44 }45 }46 47 int bfs(int x,int y)48 {49 now.x=x,now.y=y,now.step=0;50 queue
q;51 q.push(now);52 if(now.step>=fire[now.x][now.y]&&fire[now.x][now.y]>0) return -1;53 while(!q.empty()){54 now=q.front();55 q.pop();56 if(mat[now.x][now.y]=='t') return now.step;57 for (int i=0;i<4;i++){58 int x=now.x+go[i][0],y=now.y+go[i][1];59 if(check(x,y)&&mat[x][y]!='f'&&now.step+1
View Code

 

转载于:https://www.cnblogs.com/Spacious/p/9366467.html

你可能感兴趣的文章
iphone配置实用工具iPhone Configuration Utility
查看>>
Centos搭建开发环境,PHP7+ Nginx1.12+ Mysql5.7
查看>>
RSA的密钥把JAVA格式转换成C#的格式
查看>>
转载 HTTPS 之fiddler抓包、jmeter请求
查看>>
Android常用查询网站
查看>>
wifi diplasy流程介绍
查看>>
使用浏览器做编辑器
查看>>
【20181030T1】排列树【树形结构+组合数】
查看>>
windows&linux双系统时间相差8小时
查看>>
史上最详细的linux网卡ifcfg-eth0配置详解
查看>>
iphone-common-codes-ccteam源代码 CCUIScreen.m
查看>>
SDO_Geometry相关学习(转载)
查看>>
二叉查找(排序)树的分析与实现
查看>>
LeetCode-230. Kth Smallest Element in a BST
查看>>
js之隔行换色
查看>>
使用EMQ搭建MQTT服务器
查看>>
P3379 【模板】最近公共祖先(LCA)(树链剖分)版
查看>>
利用GCC编译器生成动态链接库和静态链接库
查看>>
time模块
查看>>
[Weekly] 2014.03.01-2014.03.08
查看>>