[kuangbin带你飞]专题一 简单搜索-J - Fire!

分析:先确定火的扩散速度,当J走到该格,若时间小于扩散到该格的时间则无事,否则烧死


#include <cstdio>
#include <queue>
#include <iostream>
#include <cstring>

using namespace std;
typedef long long ll;
typedef pair <int, int > P;
typedef struct {
	int a;
	int b;
	int wal;
}dat;
dat p;
queue <dat> f;
char temp[1005][1005];
int maze[1005][1005];
int n, m, ans, vis[1005][1005], dir[][2] = { 0,1, 1,0, 0,-1, -1,0 };
dat star;

void init()
{
	cin >> n >> m;
	//scanf("%d %d", &n, &m);
	ans = 100000; star.a = 0, star.b = 0;
	memset(vis, 0, sizeof vis);
	memset(maze, 0, sizeof maze);
	for (int i = 1; i <= n; i++)
		cin >> temp[i] + 1;
		//scanf("%s", temp[i] + 1);

	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= m; j++)
		{
			if (temp[i][j] == 'J')
			{
				star.a = i;
				star.b = j;
				maze[i][j] = 0;
			}
			if (temp[i][j] == '.')
				maze[i][j] = 0;
			if (temp[i][j] == '#')
				maze[i][j] = -1;
			if (temp[i][j] == 'F')
			{
				p.a = i, p.b = j;
				maze[i][j] = 0;
				vis[i][j] = 1;
				p.wal = 0;
				f.push(p);
			}
		}

	//vis[star.a][star.b] = 1;
}
void fire()
{
	dat now, nex;
	while (f.size())
	{
		now = f.front();
		f.pop();

		for (int i = 0; i<4; i++)
		{
			int a, b;
			a = now.a + dir[i][0], b = now.b + dir[i][1];
			nex.a = a, nex.b = b;
			nex.wal = now.wal + 1;
			if (1 <= a&&a <= n && 1 <= b&&b <= m)
				if (maze[a][b] == 0&&vis[a][b]==0)
				{
					maze[a][b] = now.wal + 1;
					f.push(nex);
					vis[a][b] = 1;
				}
		}
	}
}

void bfs()
{
	memset(vis, 0, sizeof vis);
	vis[star.a][star.b] = 1;
	queue <dat> q;
	star.wal = 0;
	q.push(star);
	// cout<<maze[1][1]<<endl;

	while (q.size())
	{
		dat now = q.front();
		q.pop();

		if (now.a == 0 || now.a>n || now.b == 0 || now.b>m)
		{
			if (ans > now.wal)
			{
				ans = now.wal;
				return;
			}
		}
		if (maze[now.a][now.b] <= now.wal&&maze[now.a][now.b]!=0 )
		{
			continue;
		}

		for (int i = 0; i<4; i++)
		{
			int a = now.a + dir[i][0], b = now.b + dir[i][1];
			dat nex;
			nex.a = a, nex.b = b;
			nex.wal = now.wal + 1;

			if (vis[nex.a][nex.b] == 0 && maze[nex.a][nex.b] != -1)
			{
				q.push(nex);
				vis[nex.a][nex.b] = 1;
			}
		}
	}
}

void solve()
{
	init();
	fire();
	bfs();
	if (ans != 100000)
		printf("%d\n", ans );
	else
		printf("IMPOSSIBLE\n");
}

int main(void)
{
	int t;
	cin >> t;

	while (t--)
	{
		solve();
	}
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值