E.Barareh on Fire(暴力bfs)

Barareh on Fire

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 Output
4
Impossible
Impossible
1


题目大意是这样的,给你一个起点和终点,并给你很多火,你从起点出发到终点,你只能上下左右走,一步一秒,而每把火是k秒蔓延一次,每一次蔓延会使得周围8个格子无法走,问你能否安全到达终点,若能就输出最小步数,否则输出Impossible。
emmm,当时想太多了,一直以为暴力会超时,然后就一直用逆向bfs求出到每个火的最短时间。。。这个想法的漏洞太大就不多说了,最后实在没有办法了,用暴力枚举了一遍每一个顶点500ms+,(本来应该是一血的,结果被推后了2小时才A了,中间还WA了3发,真的是坑队友。。。)
这道题跑出来的时间各有差异,慢点的有2秒的,快一点的有12ms的,这里说一下中等的算法,bfs每一个火的时候记录该火到每一个点的时间,下一次bfs时如果另一个火到改点的时间大于等于已经放入的值,则该状态不加入队列(这样就大大缩短了时间),最后对人进行单独的bfs,在此过程中若到达一个点的时间大于等于该店已存在的值,不加入队列。
另外12ms的做法就是将所有的火先压入队列进行一次bfs操作就好了,500ms+的代码如下:

#include <cstdio>
#include <cstring>
#include <queue>
#define BYJ(a,b,n,m) (a>=1&&a<=n&&b>=1&&b<=m)
using namespace std;
char s[150][150];
int a[150][150],v[150][150],dis[150][150],k,n,m;
int dx[]= {-1,-1,-1,0,0,1,1,1},dy[]= {-1,0,1,-1,1,-1,0,1};
int dx1[]= {-1,0,0,1},dy1[]= {0,-1,1,0};
void bfs(int x,int y) {
	queue<int>qx,qy,fp;
	memset(v,0,sizeof(v));
	qx.push(x),qy.push(y),fp.push(0);
	v[x][y]=1;
	dis[x][y]=0;
	int bu,mark,bf=0;
	while (!qx.empty()) {
		for (int i=0; i<8; i++) {
			int xx=qx.front()+dx[i],yy=qy.front()+dy[i],sk2=fp.front()+k;
			if (BYJ(xx,yy,n,m) && !v[xx][yy] && sk2<dis[xx][yy]) {
				dis[xx][yy]=sk2;
				qx.push(xx),qy.push(yy),fp.push(sk2);
			}
			v[xx][yy]=1;
		}
		qx.pop(),qy.pop(),fp.pop();
	}
}
int main() {
	int sx,sy,fx,fy;
	while (scanf ("%d%d%d",&n,&m,&k)) {
		if (!n && !m && !k) break;
		int num=0;
		memset(v,0,sizeof(v));
		memset(a,0,sizeof(a));
		for (int i=1; i<=n; i++)
			for (int j=1; j<=m; j++)
				dis[i][j]=99999999;
		for (int i=1; i<=n; i++)
			scanf ("%s",s[i]+1);
		for (int i=1; i<=n; i++)
			for (int j=1; j<=m; j++) {
				if (s[i][j]=='f') a[i][j]=1,num++;
				else if (s[i][j]=='s') sx=i,sy=j;
				else if (s[i][j]=='t') fx=i,fy=j;
			}
		if (a[fx][fy] || a[sx][sy]) {
			printf ("Impossible\n");
			continue;
		}
		if (num) {
			for (int i=1; i<=n; i++)
				for (int j=1; j<=m; j++) {
					if (a[i][j]) {
						bfs(i,j);
					}
				}
		}
		queue<int>qqx,qqy,qstp;
		qqx.push(sx),qqy.push(sy),qstp.push(0);
		memset(v,0,sizeof(v));
		v[sx][sy]=1;
		int bu=0,mark=0;
		while (!qqx.empty()) {
			for (int i=0; i<4; i++) {
				int xx=qqx.front()+dx1[i],yy=qqy.front()+dy1[i],sk=qstp.front()+1;
				if (BYJ(xx,yy,n,m) && !v[xx][yy] && sk<dis[xx][yy]) {
					v[xx][yy]=1;
					if (xx==fx && yy==fy) {
						bu=sk;
						mark=1;
						break;
					}
					qqx.push(xx),qqy.push(yy),qstp.push(sk);
				}
			}
			if (mark) break;
			qqx.pop(),qqy.pop(),qstp.pop();
		}
		if (!num) printf ("%d\n",bu);
		else {
			if (dis[fx][fy]<=bu || !mark) printf ("Impossible\n");
			else printf ("%d\n",bu);
		}
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值