• poi 3669 meteor shower (bfs)


    题目链接:http://poj.org/problem?id=3669

    很基础的一道bfs的题,然而,我却mle了好多次,并且第二天才发现错在了哪里_(:з)∠)_

    写bfs或者dfs一定要记得对走过的地点进行记录,这题我本以为没必要记录了,结果就一直Memory Limit Exceeded

     1 #include <cstdio>
     2 #include <iostream>
     3 #include <algorithm>
     4 #include <cstring>
     5 #include <string>
     6 #include <stack>
     7 #include <queue>
     8 #include <cmath>
     9 #define ll long long
    10 #define pi 3.1415927
    11 #define inf 0x3f3f3f3f
    12 using namespace std;
    13 struct node {
    14     int x,y,t;
    15 };
    16 int cost[310][310],x[]={0,0,0,1,-1},y[]={0,1,-1,0,0},mapp[310][310];
    17 queue <node> p;
    18 int bfs()
    19 {
    20     int i;
    21     if(cost[0][0]==0)
    22         return -1;
    23     node aa,ex;
    24     aa.t=0; aa.x=0; aa.y=0;
    25     p.push(aa);
    26     while(!p.empty())
    27     {
    28         aa=p.front();
    29         p.pop();
    30         for(i=0;i<5;++i)
    31         {
    32             ex.t=aa.t+1; ex.x=aa.x+x[i]; ex.y=aa.y+y[i];
    33             if(ex.x>=0 && ex.y>=0 &&mapp[ex.x][ex.y]==0)
    34                 if(cost[ex.x][ex.y]==-1)
    35                     return ex.t;
    36                 else if (cost[ex.x][ex.y]>ex.t){
    37                     mapp[ex.x][ex.y]=1;          ///一开始没有记录走过的点就一直空间超限
    38                     p.push(ex);
    39                 }
    40         }
    41     }
    42     return -1;
    43 }
    44 int main ()
    45 {
    46     int n,m,i,t,j,k,jj,kk;
    47     memset(cost,-1,sizeof(cost));
    48     scanf("%d",&n);
    49     for(i=0;i<n;++i)
    50     {
    51         scanf("%d %d %d",&j,&k,&m);
    52         for(t=0;t<5;++t){
    53                 jj=j+x[t]; kk=k+y[t];
    54                 if(jj<0 || kk<0)
    55                     continue;
    56             if(cost[jj][kk]==-1)
    57                 cost[jj][kk]=m;
    58             else
    59                 cost[jj][kk]=min(cost[jj][kk],m);
    60         }
    61     }
    62     mapp[0][0]=1;
    63     cout<<bfs()<<endl;
    64 
    65     return 0;
    66 }
  • 相关阅读:
    Java8 Time
    Java8 Stream
    Java8 Lambda
    Thinking in java 阅读
    String 中的 split 进行字符串分割
    Kubernetes 学习(九)Kubernetes 源码阅读之正式篇------核心组件之 Scheduler
    Kubernetes 学习(八)Kubernetes 源码阅读之初级篇------源码及依赖下载
    Golang(八)go modules 学习
    SQLAIchemy(二)ORM 相关
    SQLAIchemy 学习(一)Session 相关
  • 原文地址:https://www.cnblogs.com/blowhail/p/11961407.html
Copyright © 2020-2023  润新知