zoukankan      html  css  js  c++  java
  • CF242C King's Path(思维+bfs)

    link

    思路:

    由于It is guaranteed that the total length of all given segments doesn't exceed (10^{5}).所以暴力的标记每个能够通过的点跑bfs就好了。
    注意标记的时候借助(pair和map),如果映射到一维的话会爆(ll)吧。

    代码:

    const int maxn=1e5+100;
    
    ll sx,sy,ex,ey,n;
    struct node{
    	ll x,y,val,step;
    };
    
    
    int nx[8]={-1,-1,-1,0,0,1,1,1};
    int ny[8]={-1,0,1,-1,1,-1,0,1};
    
    map<PLL,ll>mp;
    
    int main(){
    	sx=read,sy=read,ex=read,ey=read;
    	n=read;	
    	for(ll i=1;i<=n;i++){
    		ll r=read,a=read,b=read;
    		for(ll j=a;j<=b;j++){
    			mp[{r,j}]=-1;
    		}
    	}
    	queue<PLL>q;
    	q.push({sx,sy});
    	mp[{sx,sy}]=0;
    	while(!q.empty()){
    		PLL t=q.front();q.pop();
    		if(t.first==ex&&t.second==ey){
    			cout<<mp[t]<<"
    ";return 0;
    		}
    		for(int i=0;i<8;i++){
    			PLL tmp={t.first+nx[i],t.second+ny[i]};
    			if(mp[tmp]==-1&&mp.count(tmp))
    				mp[tmp]=mp[t]+1,q.push(tmp);
    		}
    	}
    	cout<<mp[{ex,ey}];
    	return 0;
    }
    
    
  • 相关阅读:
    二进制显示整数
    测试intel 8.0
    实用的免费网络空间
    tcp/ip入门(转)
    桶排序
    在talking bar的留言(够无聊)
    重装系统手记
    几个宏
    组合算法<转>
    记深圳慧通面试
  • 原文地址:https://www.cnblogs.com/OvOq/p/15036871.html
Copyright © 2011-2022 走看看