HDU1401 双广BFS

分别从s,e出发进行bfs

注意讨论的部分即可。

详见代码:

View Code
  1 #include<stdio.h>
  2 #include<string.h>
  3 #include<stdlib.h>
  4 #include<queue>
  5 #include<map>
  6 #include<algorithm>
  7 using namespace std;
  8 const int maxn = 8;
  9 const int inf = 123456789;
 10 struct node{
 11     int x,y;
 12 };
 13 struct node2{
 14     int t;
 15     node pos[4];
 16 }s,e;
 17 map<int,int>mp;
 18 map<int,int>::iterator it;
 19 const int dx[]={0,0,1,-1};
 20 const int dy[]={1,-1,0,0};
 21 
 22 bool out( int x,int y ){
 23     if( x>=0&&x<8&&y>=0&&y<8 )
 24         return false;//no out
 25     else
 26         return true;
 27 }
 28 
 29 bool out2( node2 now ){
 30     for( int i=0;i<4;i++ ){
 31         if( out(now.pos[i].x,now.pos[i].y)==true )
 32             return true;//is out
 33     }
 34     return false;
 35 }
 36 
 37 
 38 bool cmp( node n1,node n2 ){
 39     if( n1.x!=n2.x )
 40         return n1.x<n2.x;
 41     else
 42         return n1.y<n2.y;
 43 }
 44 
 45 bool exist( node2 p,int k ){
 46     for( int i=0;i<4;i++ ){
 47         if( i!=k ){
 48             if( p.pos[ i ].x==p.pos[ k ].x&&p.pos[ i ].y==p.pos[ k ].y )
 49                 return true;//has exist
 50         }
 51     }
 52     return false;
 53 }
 54 
 55 int get_hash(node *tmp){  
 56     int res=0;  
 57     sort(tmp,tmp+4,cmp);  
 58     for(int i=0;i<4;i++){  
 59         res|=(tmp[i].x<<(6*i));  
 60         res|=(tmp[i].y<<(6*i+3));  
 61     }  
 62     return res;  
 63 }  
 64 
 65 int bfs( int kind,node2 now ){
 66     queue<node2>q;
 67     node2 p,pp;
 68     p=now,p.t=0;
 69     if( kind==2 ){
 70         it=mp.find( get_hash(p.pos) );
 71         if( it!=mp.end() )
 72             return p.t;
 73     }
 74     mp[ get_hash(p.pos) ]=kind;
 75     q.push( p );
 76     while( !q.empty() ){
 77         p=q.front(),q.pop();
 78         if( p.t>=4 )
 79             continue;
 80         for( int i=0;i<4;i++ ){
 81             for( int j=0;j<4;j++ ){//j is dir
 82                 pp=p;//this is important
 83                 pp.pos[i].x=p.pos[i].x+dx[j];
 84                 pp.pos[i].y=p.pos[i].y+dy[j];
 85                 pp.t=p.t+1;
 86                 if( out2( pp )==true )
 87                     continue;
 88                 if( exist( pp,i )==true ){
 89                     pp.pos[i].x+=dx[j];
 90                     pp.pos[i].y+=dy[j];
 91                     if( out2(pp)==true )
 92                         continue;
 93                     if( exist( pp,i )==false ){
 94                         int my_hash=get_hash( pp.pos );
 95                         it=mp.find( my_hash );
 96                         if( kind==1 ){
 97                             if( it==mp.end() ){
 98                                 mp[ my_hash ]=kind;
 99                                 q.push( pp );
100                             }
101                         }
102                         else{
103                             if( it==mp.end() ){
104                                 mp[ my_hash ]=kind;
105                                 q.push( pp );
106                             }
107                             else if( (*it).second==1 )
108                                 return pp.t;
109                         }
110                     }//第二步 不存在某个点
111                 }//第一步 已经存在某个点
112                 else{
113                     int my_hash=get_hash( pp.pos );
114                     it=mp.find( my_hash );
115                     if( it==mp.end() ){
116                         mp[ my_hash ]=kind;
117                         q.push( pp );
118                     }//1 2 all should put into the queue
119                     if( kind==2 ){
120                         if( it!=mp.end() )
121                             if( (*it).second==1 )
122                                 return pp.t;
123                     }
124                 }
125             }
126         }
127     }
128     return -1;
129 }
130                         
131 int main(){
132     while( scanf("%d%d",&s.pos[0].x,&s.pos[0].y )==2 ){
133         s.pos[0].x--,s.pos[0].y--;
134         for( int i=1;i<4;i++ ){
135             scanf("%d%d",&s.pos[i].x,&s.pos[i].y);
136             s.pos[i].x--,s.pos[i].y--;
137         }
138         for( int i=0;i<4;i++ ){
139             scanf("%d%d",&e.pos[i].x,&e.pos[i].y);
140             e.pos[i].x--,e.pos[i].y--;
141         }
142         mp.clear();
143         bfs( 1,s );//kind ,now node2;
144         int flag=bfs( 2,e );
145         if( flag==-1 )
146             printf("NO\n");
147         else
148             printf("YES\n");
149     }
150     return 0;
151 }
keep moving...
原文地址:https://www.cnblogs.com/xxx0624/p/2912618.html