好久没写博客了,冒个泡 敲BFS失败

http://poj.org/problem?id=3669

Meteor Shower
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 30527 Accepted: 7859
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 (Xi, Yi) (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: Xi, Yi, 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
Source

USACO 2008 February Silver

中文版
Bessie听说有场史无前例的流星雨即将来临;有谶言:陨星将落,徒留灰烬。为保生机,她誓将找寻安全之所(永避星坠之地)。目前她正在平面坐标系的原点放牧,打算在群星断其生路前转移至安全地点。

此次共有M (1 ≤ M ≤ 50,000)颗流星来袭,流星i将在时间点Ti (0 ≤ Ti ≤ 1,000) 袭击点 (Xi, Yi) (0 ≤ Xi ≤ 300; 0 ≤ Yi ≤ 300)。每颗流星都将摧毁落点及其相邻四点的区域。

Bessie在0时刻时处于原点,且只能行于第一象限,以平行与坐标轴每秒一个单位长度的速度奔走于未被毁坏的相邻(通常为4)点上。在某点被摧毁的刹那及其往后的时刻,她都无法进入该点。

寻找Bessie到达安全地点所需的最短时间。

Input - 输入

  • 第1行: 一个整数: M
  • 第2…M+1行: 第i+1行包含由空格分隔的三个整数: Xi, Yi, and Ti
    Output - 输出
  • 仅一行: Bessie寻得安全点所花费的最短时间,无解则为-1。

Sample Input - 输入样例
4
0 0 2
2 1 2
1 1 2
0 3 5
Sample Output - 输出样例
5

写搓了
第一个bug是主函数memset没用???,改for初始化的图
样例过了,但是wa
然后,改大空间,改多组,还是wa
最后神仙加了个等于/???/ 就过了???

贴出来,现在没搞懂啥子鬼,以后看看。

wa:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<queue>
#include<cmath>
#include<algorithm>
using namespace std;
const int MAXN = 400;
const int time = 100000000;
int m,x,y,t,ans;
struct N{
int x,y,t;
};
queue<N> Q;
int tu[MAXN][MAXN];
bool vis[MAXN][MAXN];
bool check(int x,int y,int t){
if(x<1||x>MAXN||y<1||y>MAXN||t>=tu[x][y])return false;
return true;
}
int bfs(){
N now,next;
now.x=1,now.y=1,now.t=0;
Q.push(now);
while(!Q.empty()){
	now = Q.front();
	if(tu[now.x][now.y]==time){
		return now.t;
		}
	Q.pop();
	for(int i = -1;i <= 1;i++){
		for(int j = -1;j <= 1;j++){
			next.x=now.x+i,next.y=now.y+j,next.t=now.t+1;
			if(i*i+j*j==1&&!vis[now.x+i][now.y+j]&&check(now.x+i,now.y+j,now.t+1)){
				Q.push(next),vis[next.x][next.y]=1;
			}
			}
		}
	}
	return -1;
}
int main(){
std::ios::sync_with_stdio(false);
while(cin >> m){
	memset(vis,0,sizeof(vis));
	while(!Q.empty())Q.pop();
for(int i = 0;i<400;i++){
	for(int j = 0;j < 400;j++)
	tu[i][j] = time;
	}
for(int i = 1;i <= m;i++){
	cin >> x >> y  >> t;
	tu[x+1][y+1] = min(tu[x+1][y+1],t);
	for(int i = -1;i <= 1;i++){
		for(int j = -1;j <= 1;j++){
			if(i*i+j*j==1&&x+1+i>0&&y+1+i>0)tu[x+1+i][y+1+j]=min(tu[x+1+i][y+1+j],t);
			}
		}
			}/*
for(int i = 1;i <= 7;i++){
	printf("\n");
	for(int j = 1;j <= 7;j++){
		printf("%d ",tu[i][j]);
		}
	}
	*/
ans = bfs();
cout << ans << endl;
}
return 0;
}

改主函数里面

if(i*i+j*j==1&&x+1+i>0&&y+1+i>0)tu[x+1+i][y+1+j]=min(tu[x+1+i][y+1+j],t);

加个“=” 就行

if(i*i+j*j==1&&x+1+i>=0&&y+1+i>=0)tu[x+1+i][y+1+j]=min(tu[x+1+i][y+1+j],t);

或者删掉后面

if(i*i+j*j==1)tu[x+1+i][y+1+j]=min(tu[x+1+i][y+1+j],t);

BUG真难找,还是队友找到的。。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值