UVA - 11624 Fire!

1.题面

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2671

2.题意

在一个迷宫里,Joe希望走出去,但是某几个位置上有火种,火种和Joe一样,可以往上下左右四个方向蔓延,问Joe有没有逃生的方法

3.思路

把火种和Joe放在同一个队列里面BFS,注意区分就好

4.代码

/*****************************************************************
    > File Name: acm.cpp
    > Author: Uncle_Sugar
    > Mail: uncle_sugar@qq.com
    > Created Time: 2016/11/4 12:51:44
*****************************************************************/
# include <cstdio>
# include <cstring>
# include <cctype>
# include <cmath>
# include <cstdlib>
# include <climits>
# include <iostream>
# include <iomanip>
# include <set>
# include <map>
# include <vector>
# include <stack>
# include <queue>
# include <algorithm>
using namespace std;

const int debug = 1;
const int size  = 1000 + 10; 
const int INF = INT_MAX>>1;
typedef long long ll;
struct coord{
	int x, y, type;
	coord(){}
	coord(int _x, int _y, int _type):x(_x), y(_y), type(_type){}
};

char mp[size][size];
int dist[size][size];

int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};

int r, c;

bool inrange(int x, int y){
	return x>=0 && x<r && y>=0 && y<c; 
}

int main(){
	int T;
	scanf("%d", &T);
	queue<coord> que;
	while (T--){
		while (!que.empty()) que.pop();
		memset(dist, 0, sizeof(dist));
		int sx, sy;
		scanf("%d%d", &r, &c);
		for (int i = 0; i < r; i++) {
			scanf("%s", mp[i]);
			for (int j = 0; j < c; j++){
				if (mp[i][j] == 'F'){
					que.push(coord(i, j, 1));
					dist[i][j] = 1;
				}else if (mp[i][j] == 'J'){
					sx = i, sy = j;
					
				}
			}
		}
		que.push(coord(sx, sy, -1));
		dist[sx][sy] = -1;
		int flag = 0;
		while (!que.empty()){
			coord T = que.front(); que.pop();
			int x = T.x, y = T.y;
			if (dist[x][y] < 0 && (x == r-1 || y == c-1 || x == 0 || y == 0)) flag = dist[x][y];
			if (flag) break;
			for (int i = 0; i < 4; i++){
				int nx = x + dx[i];
				int ny = y + dy[i];
				if (inrange(nx, ny) && dist[nx][ny] == 0 && mp[nx][ny] != '#'){
					dist[nx][ny] = dist[x][y] + T.type;
					que.push(coord(nx, ny, T.type));
				}
			}
		}
		if (flag)
			printf("%d\n", -flag);
		else 
			printf("IMPOSSIBLE\n");
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值