【搜索】POJ-3669 BFS

一、题目

Description

Bessie hears that an extraordinary meteor shower is coming; reports say that these meteors will crash into earth and destroy anything they hit. Anxious for her safety, she vows to find her way to a safe location (one that is never destroyed by a meteor) . She is currently grazing at the origin in the coordinate plane and wants to move to a new, safer location while avoiding being destroyed by meteors along her way.

The reports say that M meteors (1 ≤ M ≤ 50,000) will strike, with meteor i will striking point (XiYi) (0 ≤ *Xi *≤ 300; 0 ≤ *Yi *≤ 300) at time Ti (0 ≤ Ti  ≤ 1,000). Each meteor destroys the point that it strikes and also the four rectilinearly adjacent lattice points.

Bessie leaves the origin at time 0 and can travel in the first quadrant and parallel to the axes at the rate of one distance unit per second to any of the (often 4) adjacent rectilinear points that are not yet destroyed by a meteor. She cannot be located on a point at any time greater than or equal to the time it is destroyed).

Determine the minimum time it takes Bessie to get to a safe place.

Input

* Line 1: A single integer: M
* Lines 2..M+1: Line i+1 contains three space-separated integers: XiYi, and Ti

Output

* Line 1: The minimum time it takes Bessie to get to a safe place or -1 if it is impossible.

Sample Input

4
0 0 2
2 1 2
1 1 2
0 3 5

Sample Output

5

二、思路

  • 安全区可以到301
  • 同一个点可以炸两次
  • 必须在第一象限
  • t可以等于0
  • 可以一开始就死,也可以一开始就是安全区

二、思路&心得

  • 要注意数据的范围
  • 广搜常用来求解某个问题的最小值,求解过程中,利用dist数组更新最小值

三、代码


int dist[MAX_SIZE][MAX_SIZE];

int dirction[5][2] = {0, -1, -1, 0, 0, 1, 1, 0, 0, 0};

typedef pair<int, int> P;

struct Meteor {
	int X;
	int Y;
	int T;
} meteors[MAX_M];

bool cmp (Meteor a, Meteor b) {
	if (a.T < b.T) return true;
	else return false;
}

bool isLegal(int x, int y) {
	if (x >= 0 && x < MAX_SIZE && y >= 0 && y < MAX_SIZE) return true;
	else return false;
}

int bfs() {
	queue<P> que;
	if (map[0][0] == 0) return -1;
	else if (map[0][0] == MAX_TIME) return 0;
	que.push(P(0, 0));
	while (que.size()) {
		P p = que.front();
		que.pop();
		int px = p.first, py = p.second;
		if (px >= MAX_SIZE || py >= MAX_SIZE) continue;
		for(int i = 0; i < 4; i ++) {
			int tx = px + dirction[i][0], ty = py + dirction[i][1];
			if (isLegal(tx, ty)) {
				if (map[tx][ty]  == MAX_TIME) {
					return ++ dist[px][py];
				}
				if (dist[px][py] + 1 < map[tx][ty] && !dist[tx][ty]) {
					que.push(P(tx, ty));
					dist[tx][ty] = dist[px][py] + 1;
				}
			}
		}
	}
	return -1;
}

void solve() {
	for (int i = 0; i < MAX_SIZE; i ++) {
		for (int j = 0; j < MAX_SIZE; j ++) {
			map[i][j] = MAX_TIME;
		}
	} 
	for (int i = 0; i < M; i ++) {
		scanf("%d %d %d", &meteors[i].X, &meteors[i].Y, &meteors[i].T);
	}
	sort(meteors, meteors + M, cmp);
	for (int i = 0; i < M; i ++) {
		for (int j = 0; j < 5; j ++) {
			int tx = meteors[i].X, ty = meteors[i].Y;
			tx += dirction[j][0];
			ty += dirction[j][1];
			if (isLegal(tx, ty) && map[tx][ty] > meteors[i].T) {
				map[tx][ty] = meteors[i].T;
			}
		}
	}
	printf("%d\n", bfs());
}

int main() {
	while (~scanf("%d", &M)) {
		solve();
	}
	return 0;
}
posted @ 2017-08-03 18:08  天涯惟笑  阅读(164)  评论(0编辑  收藏  举报