2015 寒假搜索专题 I - Meteor Shower(BFS)

I - Meteor Shower
Time Limit:1000MS    Memory Limit:65536KB    64bit IO Format:%I64d & %I64u

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 

--------
前一天看的题目,今天写的代码。
再次看题目时难免有点烦躁,漏掉了一个关键条件,同一个地方被陨石砸中的话取时间较小那个值。
还有一个 坏习惯,标记全部用-1或者0表示,导致取较小值的时候整个代码复杂了好多。

在WA了好多次后,加上那个关键条件后就AC了。
一开始听到前面一桌的谈论这题什么超过范围,然后一直以为是自己数组开小了,试了好多次。
以后做题还是要一个人静静地做。

--------
code:

   
   
#include
      
      
       
       
#include
       
       
        
        
#include
        
        
         
         
using namespace std;
typedef pair
         
         
          
           P;

const int MAXN = 300 + 10;
const int SAFE = -1;
const int inf = 1000000;

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

int m_time[50010];
int maze[MAXN][MAXN];
int m_dx[50010];
int m_dy[50010];
int vis[MAXN][MAXN];

int minx;
int miny;
int max_x;
int max_y;

int ok(int x, int y)
{
    if(0 <= x && x <= 400 && 0 <= y && y <= 400 &&
       vis[x][y] == -1)
        return 1;
    return 0;
}

void destory(int x, int y, int time)
{

    if(maze[x][y] > time || maze[x][y] == SAFE) maze[x][y] = time;    //由于用习惯了-1,0等标记,当遇到要取更小值的时候遇到了麻烦
    if(0 <= x-1 && (maze[x-1][y] > time || maze[x-1][y] == SAFE)) {maze[x-1][y] = time;}
    if(x+1 <= 400 && (maze[x+1][y] > time || maze[x+1][y] == SAFE)) {maze[x+1][y] = time; }
    if(0 <= y-1 && (maze[x][y-1] > time || maze[x][y-1] == SAFE)) {maze[x][y-1] = time; }
    if(y+1 <= 400 && (maze[x][y+1] > time || maze[x][y+1] == SAFE)) {maze[x][y+1] = time; }
}

int bfs(int x,int y)
{
    queue 
         
         
        
        
       
       
      
      

que; que.push(P(x,y)); vis[x][y] = 0; while(que.size()) { P p = que.front(); if(maze[p.first][p.second] == -1) return vis[p.first][p.second]; for(int i = 0; i < 4; i++) { int nx = p.first + dx[i]; int ny = p.second + dy[i]; if(ok(nx,ny) && (vis[p.first][p.second] + 1 < maze[nx][ny] || maze[nx][ny] == -1)) { vis[nx][ny] = vis[p.first][p.second] + 1; que.push(P(nx,ny)); } } que.pop(); } return -1; } int main() { memset(maze,SAFE,sizeof(maze)); memset(vis,-1,sizeof(vis)); int M; int step = 1000000; scanf("%d",&M); for(int i = 0; i < M; i++) { scanf("%d%d%d",&m_dx[i],&m_dy[i],&m_time[i]); destory(m_dx[i],m_dy[i],m_time[i]); } int right = bfs(0,0); /*for(int i = 0; i <= 5; i++) //测试代码 { for(int j = 0; j <= 5; j++) { //if(maze[i][j] == -1 && vis[i][j] != -1 && step > vis[i][j]) step = vis[i][j]; //一开始使用的错误的全盘搜索比较 printf("%2d",maze[i][j]); } printf("\n"); }*/ if(right != -1) printf("%d\n",right); else printf("-1\n"); return 0; }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值