zoukankan      html  css  js  c++  java
  • CF242CKing's Path

    神一般的STL题,给跪了!!

    题意求一个点是否能到另一个点,到另一个点的最小步数

    因为给的不是连续的坐标,而是一段一段的坐标,又因为坐标范围比较大(1-10^9).

    觉得要用离散的方法,弱菜不会,看大神们都用STL,看了下,实在太神了,膜拜之!!

    View Code
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <vector>
    #include <queue>
    #include <set>
    #define MAXN 100050
    using namespace std;
    int x0,y0,x1,y1;
    int n;
    typedef pair<int,int> PII;
    set<PII> s;
    queue < pair<PII,int> > q;
    int stepx[10]={0,0,-1,1,-1,1,-1,1};
    int stepy[10]={-1,1,0,0,-1,-1,1,1};
    int main()
    {
        while(~scanf("%d%d%d%d",&x0,&y0,&x1,&y1)){
            scanf("%d",&n);
            int r,a,b;
            for(int i=0;i<n;i++){
                scanf("%d%d%d",&r,&a,&b);
                for(int j=a;j<=b;j++){
                    s.insert(PII(r,j));
                }
            }
            int step,xx,yy;
            q.push(make_pair(PII(x0,y0), 0));
            while(!q.empty()){
                int x=q.front().first.first;
                int y=q.front().first.second;
                step=q.front().second+1;
                q.pop();
                for(int i=0;i<8;i++){
                    xx=x+stepx[i];
                    yy=y+stepy[i];
                    if(s.find(PII(xx,yy))==s.end())continue;
                    s.erase(PII(xx,yy));
                    if(xx==x1&&yy==y1){
                        printf("%d\n",step);
                        return 0;
                    }
    
                    q.push(make_pair(PII(xx,yy),step));
                }
            }
            printf("-1\n");
        }
        return 0;
    }
  • 相关阅读:
    Shell 函数
    Shell 流程控制
    Shell test 命令
    Shell echo命令
    python 类、模块、包的区别
    postgresql vacuum table
    ssh连接断开后 shell进程退出
    ubuntu 搭建 svn服务器,使用http方式访问
    如何查看apache加载了哪些模块
    maven 的使用
  • 原文地址:https://www.cnblogs.com/arbitrary/p/2776009.html
Copyright © 2011-2022 走看看