UVa Fire! 11624 (BFS)

6 篇文章 0 订阅

莫名其妙的 runtime error。

代码:

#include<stdio.h>
#include<string.h>
#include<queue>
#define _n printf("\n");
using namespace std;

struct T{
	int x;
	int y;
	int step;
};

int dir[5][5] = {{1 , 0} , {-1 , 0} , {0 , 1} , {0 , -1}};
int t , n , m , sx , sy;
char map[1005][1005];
int vis[1005][1005];
int time[1005][1005];
const int inf = 10000000;
bool flag ;

queue<T> q;

void bfs1(){

	T next , now;

	while(!q.empty()){
		now = q.front();
		q.pop();
		for(int i = 0 ; i < 4 ; i ++){
			next.x = now.x + dir[i][0];
			next.y = now.y + dir[i][1];
			next.step = now.step + 1;
			if(next.x >= 0 && next.x < n && next.y >= 0 && next.y < m && map[next.x][next.y] == '.' && time[next.x][next.y] > next.step){
				time[next.x][next.y] = next.step;
				q.push(next);
			}
		}
	}


	return ;
}

void bfs(){

	while(!q.empty())
            q.pop();

	T next , now;
	flag = false;

	now.x = sx;
	now.y = sy;
	now.step = 0;
	vis[sx][sy] = 1;
	q.push(now);

	while(!q.empty()){

		now = q.front();
		q.pop();

		if(now.x <= 0 || now.y <= 0 || now.x >= n - 1 || now.y >= m - 1){
			printf("%d\n" , now.step + 1);
			flag = true;
			return;
		}

		for(int i = 0 ; i < 4 ; i ++){
			next.x = now.x + dir[i][0];
			next.y = now.y + dir[i][1];
			next.step = now.step + 1;
			if(next.step < time[next.x][next.y] && map[next.x][next.y] == '.' && next.x >= 0 && next.x < n && next.y >= 0 && next.y < m && !vis[next.x][next.y]){

				q.push(next);
				vis[next.x][next.y] = 1;

			}
		}

	}

	return ;
}



int main(){

	T node;
	scanf("%d" , &t);

	while(t --){
		scanf("%d%d" , &n , &m);
		for(int i = 0 ; i < n ; i ++){
			scanf("%s" , map[i]);
		}
		for(int i = 0 ; i < n ; i ++){
			for(int j = 0 ; j < m ; j ++){
				vis[i][j] = 0;
				time[i][j] = inf;

			}
		}
		while(!q.empty())  q.pop();
		for(int i = 0 ; i < n ; i ++){
			for(int j = 0 ; j < m ; j ++){
				if(map[i][j] == 'F'){
					node.x = i;
					node.y = j;
					node.step = 0;
					time[i][j] = 0;
					vis[i][j] = 1;
					q.push(node);
				}
				if(map[i][j] == 'J'){
					sx = i;
					sy = j;

				}
			}
		}
		bfs1();
		memset(vis,0,sizeof(vis));
		bfs();
		if(flag == false){
			printf("IMPOSSIBLE\n");
		}

	}


	return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值