poj 3669 BFS

思路还是不明朗,其实这题和迷宫的最短路径思路差不多,但是这题没有给出明显的边界条件,导致半天没有想明白,写的程序还是半天调试不过去,基础不扎实......keep moving .....

粗略的想法:

        预处理,把每个会被陨石雨破坏的坐标都用破坏时间表示出来,类似迷宫的最短路径的障碍,方便接下来的bfs判断,比如,在原点不需要动的情况或往回走的情况。

注意:

          在oj上  cin流输入 比scanf输入效率差几倍,用cin就会TLE。

/*
 * =====================================================================================
 *
 *       Filename:  poj_3669.cpp
 *
 *    Description:  poj 3669 Meteor shower
 *
 *        Version:  1.0
 *        Created:  03/29/2015 03:38:35 AM
 *       Revision:  none
 *       Compiler:  gcc
 *
 *		   Author:  Kart, 786900722@qq.com
 *           Blog:  http://blog.youkuaiyun.com/linux_kernel_fan/
 *
 * =====================================================================================
 */
#include<cstdio>
#include<iostream>
#include<queue>
#include<utility>
#include<cstring>

using namespace std;
const int maxsize=405;
int map[maxsize][maxsize];
int M;
pair<int, int> dir[5] = {{0, 0}, {-1, 0}, {0, 1}, {1, 0}, {0, -1}};

typedef struct 
{
	int x;
	int y;
	int time;
}Node;

int solve()
{
	if(map[0][0] == 0) return -1;
	if(map[0][0] == -1) return 0;
	
	queue<Node> que;
	Node now, tmp;
	tmp.x = tmp.y = tmp.time = 0;
	
	que.push(tmp);
	while(que.size())
	{
		now = que.front();
		que.pop();
		for(int i = 1; i < 5; i++)
		{
			tmp.x = now.x + dir[i].first;
			tmp.y = now.y + dir[i].second;
			tmp.time = now.time + 1;
			if(tmp.x < 0 || tmp.x >= maxsize || tmp.y < 0 || tmp.y >= maxsize)
				continue;
			if(map[tmp.x][tmp.y] == -1) return tmp.time;
			if(tmp.time >= map[tmp.x][tmp.y]) continue;
			map[tmp.x][tmp.y] = tmp.time;
			que.push(tmp);
		}
	}
	return -1;
}
int main()
{
	while(scanf("%d", &M) != EOF)
	{
		memset(map, -1, sizeof(map));
		for(int i = 0; i < M; i++)
		{
			int x, y,t;
			scanf("%d%d%d", &x, &y, &t);
			for(int k = 0; k < 5; k++)
			{
				int nx = x + dir[k].first;
				int ny = y + dir[k].second;
				if(nx >= 0 && nx < maxsize && ny >= 0 && ny < maxsize)
				{
					if(map[nx][ny] == -1)
						map[nx][ny] = t;
					else
						map[nx][ny] = min(t, map[nx][ny]);
				}
			}
		}
		printf("%d\n", solve());
	}
	return 0;
}



参考引用:http://www.cnblogs.com/justforgl/archive/2012/12/19/2825422.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值