bzoj3383[Usaco2004 Open]Cave Cows 4 洞穴里的牛之四*

bzoj3383[Usaco2004 Open]Cave Cows 4 洞穴里的牛之四

题意:

平面直角坐标系有n个点,从(0,0)出发,从一个点上可以跳到所有与它横纵坐标距离都≤2的点上,求最少步数使得纵坐标为T。

题解:

先用set存下所有的点。在做dp的时候把所有横纵坐标与当前节点距离≤2的节点都在set中查找,如果可以查到则可以转移到那个节点。

代码:

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <set>
 5 #include <queue>
 6 #define maxn 50010
 7 #define inc(i,j,k) for(int i=j;i<=k;i++)
 8 using namespace std;
 9 
10 inline int read(){
11     char ch=getchar(); int f=1,x=0;
12     while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();}
13     while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
14     return f*x;
15 }
16 int x[maxn],y[maxn],n,t,dis[maxn]; queue<int>q;
17 struct nd{
18     int x,y,id;
19     bool operator < (const nd &a)const{return x!=a.x?x<a.x:y<a.y;}
20 };
21 set<nd>st;
22 int main(){
23     n=read(); t=read(); inc(i,1,n)x[i]=read(),y[i]=read(),st.insert((nd){x[i],y[i],i});
24     q.push(0); dis[0]=0;
25     while(!q.empty()){
26         int z=q.front(); q.pop();
27         inc(i,-2,2)inc(j,-2,2){
28             set<nd>::iterator a=st.find((nd){x[z]+i,y[z]+j,0});
29             if(a!=st.end()){
30                 dis[a->id]=dis[z]+1; q.push(a->id);
31                 if(a->y==t){printf("%d",dis[a->id]); return 0;} st.erase(a);
32             }
33         }
34     }
35     printf("-1"); return 0;
36 }

 

20160912

posted @ 2016-09-16 15:29  YuanZiming  阅读(323)  评论(0编辑  收藏  举报