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

  1. 7 7 2
  2. f------
  3. -f---f-
  4. ----f--
  5. -------
  6. ------f
  7. ---s---
  8. t----f-
  9. 3 4 1
  10. t--f
  11. --s-
  12. ----
  13. 2 2 1
  14. st
  15. f-
  16. 2 2 2
  17. st
  18. f-
  19. 0 0 0

Sample Output

  1. 4
  2. Impossible
  3. Impossible
  4. 1

Hint


两遍bfs,第一遍预处理每个点火烧到的时间,第二遍判断人是否能安全逃生
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<iostream>
  4. #include<algorithm>
  5. #include<queue>
  6. using namespace std;
  7. struct point{
  8. int x, y,time;
  9. };
  10. int vis[110][110],vis1[110][110], time_[110][110], dir[8][2] = {1,0, 0,1, -1,0, 0,-1, 1,1, 1,-1, -1,1, -1,-1};
  11. char map[110][110];
  12. int m, n, k,time1;
  13. queue<point>q;
  14.  
  15. bool check(int x,int y)
  16. {
  17. if (x >= 0 && y >= 0 && x < n&&y < m)
  18. return true;
  19. else
  20. return false;
  21. }
  22.  
  23. void bfs1()
  24. {
  25. point u, v;
  26. while (!q.empty())
  27. {
  28. u = q.front();
  29. q.pop();
  30. for (int i = 0; i < 8; i++)
  31. {
  32. v.x = u.x + dir[i][0];
  33. v.y = u.y + dir[i][1];
  34. v.time = u.time + k;
  35. if (check(v.x, v.y) && !vis[v.x][v.y])
  36. {
  37. q.push(v);
  38. vis[v.x][v.y] = 1;
  39. time_[v.x][v.y] = v.time;
  40. }
  41. }
  42. }
  43. }
  44.  
  45. bool bfs2()
  46. {
  47. point u, v;
  48. while (!q.empty())
  49. {
  50. u = q.front();
  51. q.pop();
  52. for (int i = 0; i < 4; i++)
  53. {
  54. v.x = u.x + dir[i][0];
  55. v.y = u.y + dir[i][1];
  56. v.time = u.time + 1;
  57. if (check(v.x, v.y) && v.time < time_[v.x][v.y]&&!vis1[v.x][v.y])
  58. {
  59. if (map[v.x][v.y] == 't')
  60. {
  61. time1 = v.time;
  62. return true;
  63. }
  64. vis1[v.x][v.y] = 1;
  65. q.push(v);
  66. }
  67. }
  68. }
  69. return false;
  70. }
  71. int main()
  72. {
  73. while (~scanf("%d%d%d", &n, &m, &k))
  74. {
  75. int num = 0;
  76. if (!n&&!m&&!k)
  77. break;
  78. while (!q.empty())
  79. q.pop();
  80. memset(vis, 0, sizeof(vis));
  81. memset(vis1, 0, sizeof(vis1));
  82. for (int i = 0; i < n; i++)
  83. {
  84. scanf("%s", map[i]);
  85. }
  86.  
  87. point u,v;
  88. for (int i = 0; i < n; i++)
  89. {
  90. for (int j = 0; j < m; j++)
  91. {
  92. if (map[i][j] == 'f')
  93. {
  94. time_[i][j] = 0;
  95. vis[i][j] = 1;
  96. q.push(point{ i, j,0 });
  97. num++;
  98. }
  99. if (map[i][j] == 's')
  100. {
  101. u.x = i; u.y = j; u.time = 0;
  102. vis1[u.x][u.y] = 1;
  103. }
  104. if (map[i][j] == 't')
  105. {
  106. v.x = i; v.y = j;
  107. }
  108. }
  109. }
  110. if (num == 0)//注意可能没有火!!!!之前比赛的时候死在了这里
  111. {
  112. time1 = abs(v.x - u.x) + abs(v.y - u.y);
  113. printf("%d\n", time1);
  114. continue;
  115. }
  116.  
  117. bfs1();
  118. while (!q.empty())
  119. q.pop();
  120.  
  121. q.push(u);
  122. if (bfs2())
  123. printf("%d\n", time1);
  124. else
  125. printf("Impossible\n");
  126. }
  127. return 0;
  128. }
  129.  
  130. /**********************************************************************
  131. Problem: 2031
  132. User: leo6033
  133. Language: C++
  134. Result: AC
  135. Time:12 ms
  136. Memory:2308 kb
  137. **********************************************************************/

CSUOJ 2031 Barareh on Fire的更多相关文章

  1. CSU - 2031 Barareh on Fire (两层bfs)

    传送门: http://acm.csu.edu.cn/csuoj/problemset/problem?pid=2031 Description The Barareh village is on f ...

  2. CSU-2031 Barareh on Fire

    CSU-2031 Barareh on Fire Description The Barareh village is on fire due to the attack of the virtual ...

  3. 2018湖南多校第二场-20180407 Barareh on Fire

    Description The Barareh village is on fire due to the attack of the virtual enemy. Several places ar ...

  4. CSU 2031

    2031: Barareh on Fire Submit Page   Summary   Time Limit: 3 Sec     Memory Limit: 512 Mb     Submitt ...

  5. CSU-ACM2018暑假集训6—BFS

    可以吃饭啦!!! A:连通块 ZOJ 1709 Oil Deposits(dfs,连通块个数) B:素数变换 打表+bfs POJ 3216 Prime Path(打表+bfs) C:水bfs HDU ...

  6. CSUOJ2031-Barareh on Fire(双向BFS)

    Barareh on Fire Submit Page Description The Barareh village is on fire due to the attack of the virt ...

  7. 关于SequeezeNet中的Fire Module

    在论文<SQUEEZENET: ALEXNET-LEVEL ACCURACY WITH 50X FEWER PARAMETERS AND <0.5MB MODEL SIZE>中,作者 ...

  8. FZU 2150 Fire Game

    Fire Game Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit St ...

  9. Fire

    Fire 分析: 首先,明确题意:b1,b2,--,bn 交换为b2,--,bn,b1,但这并不是意味着只能从b1开始交换,(这点从样例中可以看出),并且也不意味着交换的必须是连续的一串,可以是几个单 ...

随机推荐

  1. Python 豆瓣顶帖

    由于在豆瓣发了个租房帖子,发现很快就被其他的帖子淹没,但是手动顶帖实在太累,

  2. [hadoop]mapreduce原理简述

    1.用于map的输入,先将输入数据切分成相等的分片,为每一个分片创建一个map worker,这里的切片大小不是随意订的,一般是与HDFS块大小一致,默认是64MB,一个节点上存储输入数据切片的最大s ...

  3. Centos7网络配置(VMware)

    在VM虚拟机上装了Centos7,想要用xshell5连接操作,配置网络花了整整一个上午的时间,真是心酸. 登陆后,使用命令 ip addr查看了本机的网络 可以看到我的网络配置文件是ens33, 使 ...

  4. 弗罗贝尼乌斯範数(Frobenius norm)

    弗罗贝尼乌斯範数 对 p = 2,这称为弗罗贝尼乌斯範数(Frobenius norm)或希尔伯特-施密特範数( Hilbert–Schmidt norm),不过后面这个术语通常只用于希尔伯特空间.这 ...

  5. 关于项目中根据当前数据库中最大ID生成下一个ID问题——(五)

    1.关于部门管理时候根据上级产生下级部门ID的问题(传入一个参数是上级部门id)

  6. LINUX vim 修改文件 退出

    vim 保存退出, 先按ESC ,然后:wq(保存退出)W:write,写入 Q:quit,退出, 也可以直接输入X,代表WQ,也是保存退出 或者 先按ESC,再按shift+ZZ 也是保存退出 正常 ...

  7. Java数据类型以及变量的定义

    1130136248   Java的基本数据类型 变量就是申请内存来存储值.也就是说,当创建变量的时候,需要在内存中申请空间. 内存管理系统根据变量的类型为变量分配存储空间,分配的空间只能用来储存该类 ...

  8. centos7联网

    一般centos安装(在虚拟机上安装)完成后需要自己配置服务,下面我就讲下如何配置 配置联网步骤 首先,打开虚拟机的两个服务,右击我的电脑-->管理--->找到服务-->右击启动 两 ...

  9. mysql自增id归0

    mysql自增id归0 ALTER TABLE table_name AUTO_INCREMENT=1;

  10. linux tomcat 突然验证码出不来

    情况描述 虚拟机上用tomcat部署的web应用,本来都还可以的.后来打了一个快照进行过压缩后,重新起虚拟机发现应用登录界面的验证码出不来了,具体报的是500错误. 参见http://www.blog ...